github.com/prebid/prebid-server/v2@v2.18.0/adapters/loyal/loyal.go (about) 1 package loyal 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "net/http" 8 9 "github.com/prebid/openrtb/v20/openrtb2" 10 "github.com/prebid/prebid-server/v2/adapters" 11 "github.com/prebid/prebid-server/v2/config" 12 "github.com/prebid/prebid-server/v2/openrtb_ext" 13 ) 14 15 type adapter struct { 16 endpoint string 17 } 18 19 type reqBodyExt struct { 20 LoyalBidderExt reqBodyExtBidder `json:"bidder"` 21 } 22 23 type reqBodyExtBidder struct { 24 Type string `json:"type"` 25 PlacementID string `json:"placementId,omitempty"` 26 EndpointID string `json:"endpointId,omitempty"` 27 } 28 29 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 30 bidder := &adapter{ 31 endpoint: config.Endpoint, 32 } 33 return bidder, nil 34 } 35 36 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 37 var errs []error 38 var adapterRequests []*adapters.RequestData 39 40 reqCopy := *request 41 for _, imp := range request.Imp { 42 reqCopy.Imp = []openrtb2.Imp{imp} 43 44 var bidderExt adapters.ExtImpBidder 45 var loyalExt openrtb_ext.ImpExtLoyal 46 47 if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { 48 errs = append(errs, err) 49 continue 50 } 51 if err := json.Unmarshal(bidderExt.Bidder, &loyalExt); err != nil { 52 errs = append(errs, err) 53 continue 54 } 55 56 impExt := reqBodyExt{LoyalBidderExt: reqBodyExtBidder{}} 57 58 if loyalExt.PlacementID != "" { 59 impExt.LoyalBidderExt.PlacementID = loyalExt.PlacementID 60 impExt.LoyalBidderExt.Type = "publisher" 61 } else if loyalExt.EndpointID != "" { 62 impExt.LoyalBidderExt.EndpointID = loyalExt.EndpointID 63 impExt.LoyalBidderExt.Type = "network" 64 } 65 66 finalyImpExt, err := json.Marshal(impExt) 67 if err != nil { 68 errs = append(errs, err) 69 continue 70 } 71 72 reqCopy.Imp[0].Ext = finalyImpExt 73 74 adapterReq, err := a.makeRequest(&reqCopy) 75 if err != nil { 76 errs = append(errs, err) 77 continue 78 } 79 80 if adapterReq != nil { 81 adapterRequests = append(adapterRequests, adapterReq) 82 } 83 } 84 85 if len(adapterRequests) == 0 { 86 errs = append(errs, errors.New("found no valid impressions")) 87 return nil, errs 88 } 89 90 return adapterRequests, nil 91 } 92 93 func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) { 94 reqJSON, err := json.Marshal(request) 95 if err != nil { 96 return nil, err 97 } 98 99 headers := http.Header{} 100 headers.Add("Content-Type", "application/json;charset=utf-8") 101 headers.Add("Accept", "application/json") 102 return &adapters.RequestData{ 103 Method: "POST", 104 Uri: a.endpoint, 105 Body: reqJSON, 106 Headers: headers, 107 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 108 }, nil 109 } 110 111 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 112 if adapters.IsResponseStatusCodeNoContent(responseData) { 113 return nil, nil 114 } 115 116 if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil { 117 return nil, []error{err} 118 } 119 120 var response openrtb2.BidResponse 121 if err := json.Unmarshal(responseData.Body, &response); err != nil { 122 return nil, []error{err} 123 } 124 125 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 126 if len(response.Cur) != 0 { 127 bidResponse.Currency = response.Cur 128 } 129 130 for _, seatBid := range response.SeatBid { 131 for i := range seatBid.Bid { 132 bidType, err := getBidMediaType(&seatBid.Bid[i]) 133 if err != nil { 134 return nil, []error{err} 135 } 136 137 b := &adapters.TypedBid{ 138 Bid: &seatBid.Bid[i], 139 BidType: bidType, 140 } 141 bidResponse.Bids = append(bidResponse.Bids, b) 142 } 143 } 144 return bidResponse, nil 145 } 146 147 func getBidMediaType(bid *openrtb2.Bid) (openrtb_ext.BidType, error) { 148 var extBid openrtb_ext.ExtBid 149 err := json.Unmarshal(bid.Ext, &extBid) 150 if err != nil { 151 return "", fmt.Errorf("unable to deserialize imp %v bid.ext, error: %v", bid.ImpID, err) 152 } 153 154 if extBid.Prebid == nil { 155 return "", fmt.Errorf("imp %v with unknown media type", bid.ImpID) 156 } 157 158 switch extBid.Prebid.Type { 159 case openrtb_ext.BidTypeBanner, openrtb_ext.BidTypeVideo, openrtb_ext.BidTypeNative: 160 return extBid.Prebid.Type, nil 161 default: 162 return "", fmt.Errorf("invalid BidType: %s", extBid.Prebid.Type) 163 } 164 }