github.com/prebid/prebid-server/v2@v2.18.0/adapters/adelement/adelement.go (about) 1 package adelement 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "text/template" 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/macros" 13 "github.com/prebid/prebid-server/v2/openrtb_ext" 14 ) 15 16 type adapter struct { 17 endpoint *template.Template 18 } 19 20 // Builder builds a new instance of the Adelement adapter for the given bidder with the given config. 21 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 22 template, err := template.New("endpointTemplate").Parse(config.Endpoint) 23 if err != nil { 24 return nil, fmt.Errorf("unable to parse endpoint url template: %v", err) 25 } 26 27 bidder := &adapter{ 28 endpoint: template, 29 } 30 return bidder, nil 31 } 32 33 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 34 adelementExt, err := getImpressionExt(&request.Imp[0]) 35 if err != nil { 36 return nil, []error{err} 37 } 38 39 url, err := a.buildEndpointURL(adelementExt) 40 if err != nil { 41 return nil, []error{err} 42 } 43 44 requestJSON, err := json.Marshal(request) 45 if err != nil { 46 return nil, []error{err} 47 } 48 49 requestData := &adapters.RequestData{ 50 Method: "POST", 51 Uri: url, 52 Body: requestJSON, 53 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 54 } 55 56 return []*adapters.RequestData{requestData}, nil 57 } 58 59 func getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtAdelement, error) { 60 var bidderExt adapters.ExtImpBidder 61 if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { 62 return nil, &errortypes.BadInput{ 63 Message: "ext.bidder not provided", 64 } 65 } 66 var adelementExt openrtb_ext.ExtAdelement 67 if err := json.Unmarshal(bidderExt.Bidder, &adelementExt); err != nil { 68 return nil, &errortypes.BadInput{ 69 Message: "ext.bidder not provided", 70 } 71 } 72 imp.Ext = nil 73 return &adelementExt, nil 74 } 75 76 func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtAdelement) (string, error) { 77 endpointParams := macros.EndpointTemplateParams{SupplyId: params.SupplyId} 78 return macros.ResolveMacros(a.endpoint, endpointParams) 79 } 80 81 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 82 if adapters.IsResponseStatusCodeNoContent(responseData) { 83 return nil, nil 84 } 85 86 if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil { 87 return nil, []error{err} 88 } 89 90 var response openrtb2.BidResponse 91 if err := json.Unmarshal(responseData.Body, &response); err != nil { 92 return nil, []error{&errortypes.BadServerResponse{ 93 Message: "Bad Server Response", 94 }} 95 } 96 97 if len(response.SeatBid) == 0 { 98 return nil, []error{&errortypes.BadServerResponse{ 99 Message: "Empty SeatBid array", 100 }} 101 } 102 103 var bidErrs []error 104 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 105 bidResponse.Currency = response.Cur 106 for _, seatBid := range response.SeatBid { 107 for i := range seatBid.Bid { 108 bid := seatBid.Bid[i] 109 bidType, err := getBidType(bid) 110 if err != nil { 111 // could not determinate media type, append an error and continue with the next bid. 112 bidErrs = append(bidErrs, err) 113 continue 114 } 115 bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ 116 Bid: &bid, 117 BidType: bidType, 118 }) 119 } 120 } 121 return bidResponse, bidErrs 122 } 123 124 func getBidType(bid openrtb2.Bid) (openrtb_ext.BidType, error) { 125 // determinate media type by bid response field mtype 126 switch bid.MType { 127 case openrtb2.MarkupBanner: 128 return openrtb_ext.BidTypeBanner, nil 129 case openrtb2.MarkupVideo: 130 return openrtb_ext.BidTypeVideo, nil 131 case openrtb2.MarkupAudio: 132 return openrtb_ext.BidTypeAudio, nil 133 case openrtb2.MarkupNative: 134 return openrtb_ext.BidTypeNative, nil 135 } 136 137 return "", &errortypes.BadInput{ 138 Message: fmt.Sprintf("Could not define media type for impression: %s", bid.ImpID), 139 } 140 }