github.com/prebid/prebid-server/v2@v2.18.0/adapters/decenterads/decenterads.go (about) 1 package decenterads 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "net/http" 8 "strconv" 9 10 "github.com/prebid/openrtb/v20/openrtb2" 11 "github.com/prebid/prebid-server/v2/adapters" 12 "github.com/prebid/prebid-server/v2/config" 13 "github.com/prebid/prebid-server/v2/errortypes" 14 "github.com/prebid/prebid-server/v2/openrtb_ext" 15 ) 16 17 type adapter struct { 18 endpoint string 19 } 20 21 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 22 headers := http.Header{} 23 headers.Add("Content-Type", "application/json;charset=utf-8") 24 headers.Add("Accept", "application/json") 25 impressions := request.Imp 26 result := make([]*adapters.RequestData, 0, len(impressions)) 27 errs := make([]error, 0, len(impressions)) 28 29 for _, impression := range impressions { 30 var impExt map[string]json.RawMessage 31 if err := json.Unmarshal(impression.Ext, &impExt); err != nil { 32 errs = append(errs, fmt.Errorf("unable to parse bidder parameers: %s", err)) 33 continue 34 } 35 36 bidderExt, bidderExtExists := impExt["bidder"] 37 if !bidderExtExists || len(bidderExt) == 0 { 38 errs = append(errs, errors.New("bidder parameters required")) 39 continue 40 } 41 42 impression.Ext = bidderExt 43 request.Imp = []openrtb2.Imp{impression} 44 body, err := json.Marshal(request) 45 if err != nil { 46 errs = append(errs, err) 47 continue 48 } 49 result = append(result, &adapters.RequestData{ 50 Method: "POST", 51 Uri: a.endpoint, 52 Body: body, 53 Headers: headers, 54 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 55 }) 56 } 57 58 request.Imp = impressions 59 return result, errs 60 } 61 62 func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 63 var errs []error 64 65 switch responseData.StatusCode { 66 case http.StatusNoContent: 67 return nil, nil 68 case http.StatusBadRequest: 69 return nil, []error{&errortypes.BadInput{ 70 Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode), 71 }} 72 case http.StatusOK: 73 break 74 default: 75 return nil, []error{&errortypes.BadServerResponse{ 76 Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode), 77 }} 78 } 79 80 var bidResponse openrtb2.BidResponse 81 err := json.Unmarshal(responseData.Body, &bidResponse) 82 if err != nil { 83 return nil, []error{&errortypes.BadServerResponse{ 84 Message: err.Error(), 85 }} 86 } 87 88 response := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 89 90 for _, seatBid := range bidResponse.SeatBid { 91 for i := range seatBid.Bid { 92 response.Bids = append(response.Bids, &adapters.TypedBid{ 93 Bid: &seatBid.Bid[i], 94 BidType: getMediaTypeForImp(seatBid.Bid[i].ImpID, request.Imp), 95 }) 96 } 97 } 98 99 return response, errs 100 } 101 102 func getMediaTypeForImp(impID string, imps []openrtb2.Imp) openrtb_ext.BidType { 103 for _, imp := range imps { 104 if imp.ID == impID { 105 if imp.Banner != nil { 106 return openrtb_ext.BidTypeBanner 107 } else if imp.Video != nil { 108 return openrtb_ext.BidTypeVideo 109 } else if imp.Native != nil { 110 return openrtb_ext.BidTypeNative 111 } else if imp.Audio != nil { 112 return openrtb_ext.BidTypeAudio 113 } 114 } 115 } 116 return openrtb_ext.BidTypeBanner 117 } 118 119 // Builder builds a new instance of the DecenterAds adapter for the given bidder with the given config. 120 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 121 bidder := &adapter{ 122 endpoint: config.Endpoint, 123 } 124 return bidder, nil 125 }