github.com/prebid/prebid-server/v2@v2.18.0/adapters/interactiveoffers/interactiveoffers.go (about) 1 package interactiveoffers 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 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 22 var errors []error 23 var bidderExt adapters.ExtImpBidder 24 if err := json.Unmarshal(request.Imp[0].Ext, &bidderExt); err != nil { 25 errors = append(errors, &errortypes.BadInput{ 26 Message: err.Error(), 27 }) 28 return nil, errors 29 } 30 var ioExt *openrtb_ext.ExtImpInteractiveoffers 31 if err := json.Unmarshal(bidderExt.Bidder, &ioExt); err != nil { 32 errors = append(errors, &errortypes.BadInput{ 33 Message: err.Error(), 34 }) 35 return nil, errors 36 } 37 requestJSON, err := json.Marshal(request) 38 if err != nil { 39 return nil, []error{err} 40 } 41 42 url, err := a.buildEndpointURL(ioExt) 43 if err != nil { 44 return nil, []error{err} 45 } 46 headers := http.Header{} 47 headers.Add("Content-Type", "application/json;charset=utf-8") 48 headers.Add("Accept", "application/json") 49 requestData := &adapters.RequestData{ 50 Method: "POST", 51 Uri: url, 52 Body: requestJSON, 53 Headers: headers, 54 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 55 } 56 return []*adapters.RequestData{requestData}, nil 57 } 58 59 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 60 if responseData.StatusCode == http.StatusNoContent { 61 return nil, nil 62 } 63 64 if responseData.StatusCode == http.StatusBadRequest { 65 err := &errortypes.BadInput{ 66 Message: "Unexpected status code: 400. Bad request from publisher. Run with request.debug = 1 for more info.", 67 } 68 return nil, []error{err} 69 } 70 71 if responseData.StatusCode != http.StatusOK { 72 err := &errortypes.BadServerResponse{ 73 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode), 74 } 75 return nil, []error{err} 76 } 77 78 var response openrtb2.BidResponse 79 if err := json.Unmarshal(responseData.Body, &response); err != nil { 80 return nil, []error{err} 81 } 82 83 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 84 bidResponse.Currency = response.Cur 85 for _, seatBid := range response.SeatBid { 86 for _, bid := range seatBid.Bid { 87 bid := bid // pin https://github.com/kyoh86/scopelint#whats-this 88 b := &adapters.TypedBid{ 89 Bid: &bid, 90 BidType: openrtb_ext.BidTypeBanner, 91 } 92 bidResponse.Bids = append(bidResponse.Bids, b) 93 } 94 } 95 return bidResponse, nil 96 } 97 98 // Builder builds a new instance of the Interactiveoffers adapter for the given bidder with the given config. 99 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 100 template, err := template.New("endpointTemplate").Parse(config.Endpoint) 101 if err != nil { 102 return nil, fmt.Errorf("unable to parse endpoint url template: %v", err) 103 } 104 bidder := &adapter{ 105 endpoint: template, 106 } 107 return bidder, nil 108 } 109 110 func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtImpInteractiveoffers) (string, error) { 111 endpointParams := macros.EndpointTemplateParams{AccountID: params.PartnerId} 112 return macros.ResolveMacros(a.endpoint, endpointParams) 113 }