github.com/prebid/prebid-server/v2@v2.18.0/adapters/criteo/criteo.go (about) 1 package criteo 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 8 "github.com/prebid/openrtb/v20/openrtb2" 9 "github.com/prebid/prebid-server/v2/adapters" 10 "github.com/prebid/prebid-server/v2/config" 11 "github.com/prebid/prebid-server/v2/errortypes" 12 "github.com/prebid/prebid-server/v2/openrtb_ext" 13 ) 14 15 type adapter struct { 16 endpoint string 17 } 18 19 type BidExt struct { 20 Prebid ExtPrebid `json:"prebid"` 21 } 22 23 type ExtPrebid struct { 24 BidType openrtb_ext.BidType `json:"type"` 25 NetworkName string `json:"networkName"` 26 } 27 28 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 29 bidder := &adapter{ 30 endpoint: config.Endpoint, 31 } 32 return bidder, nil 33 } 34 35 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 36 requestJSON, err := json.Marshal(request) 37 if err != nil { 38 return nil, []error{err} 39 } 40 41 requestData := &adapters.RequestData{ 42 Method: "POST", 43 Uri: a.endpoint, 44 Body: requestJSON, 45 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 46 } 47 48 return []*adapters.RequestData{requestData}, nil 49 } 50 51 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 52 if responseData.StatusCode == http.StatusNoContent { 53 return nil, nil 54 } 55 56 if responseData.StatusCode == http.StatusBadRequest { 57 err := &errortypes.BadInput{ 58 Message: "Unexpected status code: 400. Run with request.debug = 1 for more info.", 59 } 60 return nil, []error{err} 61 } 62 63 if responseData.StatusCode != http.StatusOK { 64 err := &errortypes.BadServerResponse{ 65 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode), 66 } 67 return nil, []error{err} 68 } 69 70 var response openrtb2.BidResponse 71 if err := json.Unmarshal(responseData.Body, &response); err != nil { 72 return nil, []error{err} 73 } 74 75 bidResponse := adapters.NewBidderResponse() 76 bidResponse.Currency = response.Cur 77 78 for _, seatBid := range response.SeatBid { 79 for i := range seatBid.Bid { 80 var bidExt BidExt 81 if err := json.Unmarshal(seatBid.Bid[i].Ext, &bidExt); err != nil { 82 return nil, []error{&errortypes.BadServerResponse{ 83 Message: fmt.Sprintf("Missing ext.prebid.type in bid for impression : %s.", seatBid.Bid[i].ImpID), 84 }} 85 } 86 87 b := &adapters.TypedBid{ 88 Bid: &seatBid.Bid[i], 89 BidType: bidExt.Prebid.BidType, 90 BidMeta: getBidMeta(bidExt), 91 } 92 bidResponse.Bids = append(bidResponse.Bids, b) 93 } 94 } 95 96 return bidResponse, nil 97 } 98 99 func getBidMeta(ext BidExt) *openrtb_ext.ExtBidPrebidMeta { 100 var bidMeta *openrtb_ext.ExtBidPrebidMeta 101 if ext.Prebid.NetworkName != "" { 102 bidMeta = &openrtb_ext.ExtBidPrebidMeta{ 103 NetworkName: ext.Prebid.NetworkName, 104 } 105 } 106 return bidMeta 107 }