github.com/prebid/prebid-server/v2@v2.18.0/adapters/kayzen/kayzen.go (about) 1 package kayzen 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "text/template" 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/errortypes" 13 "github.com/prebid/prebid-server/v2/macros" 14 "github.com/prebid/prebid-server/v2/openrtb_ext" 15 ) 16 17 type adapter struct { 18 endpoint *template.Template 19 } 20 21 // Builder builds a new instance of the Kayzen adapter for the given bidder with the given config. 22 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 23 template, err := template.New("endpointTemplate").Parse(config.Endpoint) 24 if err != nil { 25 return nil, fmt.Errorf("unable to parse endpoint url template: %v", err) 26 } 27 28 bidder := &adapter{ 29 endpoint: template, 30 } 31 return bidder, nil 32 } 33 34 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) (requests []*adapters.RequestData, errors []error) { 35 var kayzenExt *openrtb_ext.ExtKayzen 36 var err error 37 38 if len(request.Imp) > 0 { 39 kayzenExt, err = a.getImpressionExt(&(request.Imp[0])) 40 if err != nil { 41 errors = append(errors, err) 42 } 43 request.Imp[0].Ext = nil 44 } else { 45 errors = append(errors, &errortypes.BadInput{ 46 Message: "Missing Imp Object", 47 }) 48 } 49 50 if len(errors) > 0 { 51 return nil, errors 52 } 53 54 url, err := a.buildEndpointURL(kayzenExt) 55 if err != nil { 56 return nil, []error{err} 57 } 58 59 requestJSON, err := json.Marshal(request) 60 if err != nil { 61 return nil, []error{err} 62 } 63 64 headers := http.Header{} 65 headers.Add("Content-Type", "application/json;charset=utf-8") 66 headers.Add("Accept", "application/json") 67 68 return []*adapters.RequestData{{ 69 Method: http.MethodPost, 70 Body: requestJSON, 71 Uri: url, 72 Headers: headers, 73 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 74 }}, nil 75 } 76 77 func (a *adapter) getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtKayzen, error) { 78 var bidderExt adapters.ExtImpBidder 79 if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { 80 return nil, &errortypes.BadInput{ 81 Message: "Bidder extension not provided or can't be unmarshalled", 82 } 83 } 84 var kayzenExt openrtb_ext.ExtKayzen 85 if err := json.Unmarshal(bidderExt.Bidder, &kayzenExt); err != nil { 86 return nil, &errortypes.BadInput{ 87 Message: "Error while unmarshaling bidder extension", 88 } 89 } 90 return &kayzenExt, nil 91 } 92 93 func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtKayzen) (string, error) { 94 endpointParams := macros.EndpointTemplateParams{ 95 ZoneID: params.Zone, 96 AccountID: params.Exchange, 97 } 98 return macros.ResolveMacros(a.endpoint, endpointParams) 99 } 100 101 func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { 102 if response.StatusCode == http.StatusNoContent { 103 return nil, nil 104 } 105 106 if response.StatusCode == http.StatusBadRequest { 107 err := &errortypes.BadInput{ 108 Message: "Unexpected status code: 400. Bad request from publisher. Run with request.debug = 1 for more info.", 109 } 110 return nil, []error{err} 111 } 112 113 if response.StatusCode != http.StatusOK { 114 err := &errortypes.BadServerResponse{ 115 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", response.StatusCode), 116 } 117 return nil, []error{err} 118 } 119 120 var bidResp openrtb2.BidResponse 121 if err := json.Unmarshal(response.Body, &bidResp); err != nil { 122 return nil, []error{&errortypes.BadServerResponse{ 123 Message: "Bad Server Response", 124 }} 125 } 126 127 bidResponse := adapters.NewBidderResponseWithBidsCapacity(1) 128 129 var errs []error 130 for _, seatBid := range bidResp.SeatBid { 131 for _, bid := range seatBid.Bid { 132 b := &adapters.TypedBid{ 133 Bid: &bid, 134 BidType: getMediaTypeForImp(bid.ImpID, internalRequest.Imp), 135 } 136 bidResponse.Bids = append(bidResponse.Bids, b) 137 } 138 } 139 return bidResponse, errs 140 } 141 142 func getMediaTypeForImp(impID string, imps []openrtb2.Imp) openrtb_ext.BidType { 143 for _, imp := range imps { 144 if imp.ID == impID { 145 if imp.Banner != nil { 146 return openrtb_ext.BidTypeBanner 147 } else if imp.Video != nil { 148 return openrtb_ext.BidTypeVideo 149 } else if imp.Native != nil { 150 return openrtb_ext.BidTypeNative 151 } 152 } 153 } 154 return openrtb_ext.BidTypeBanner 155 }