github.com/prebid/prebid-server/v2@v2.18.0/adapters/axis/axis.go (about) 1 package axis 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 reqBodyExt struct { 20 AxisBidderExt openrtb_ext.ImpExtAxis `json:"bidder"` 21 } 22 23 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 24 bidder := &adapter{ 25 endpoint: config.Endpoint, 26 } 27 28 return bidder, nil 29 } 30 31 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 32 var adapterRequests []*adapters.RequestData 33 34 originalImpSlice := request.Imp 35 36 for i := range request.Imp { 37 currImp := originalImpSlice[i] 38 request.Imp = []openrtb2.Imp{currImp} 39 40 var bidderExt reqBodyExt 41 if err := json.Unmarshal(currImp.Ext, &bidderExt); err != nil { 42 continue 43 } 44 45 extJson, err := json.Marshal(bidderExt) 46 if err != nil { 47 return nil, []error{err} 48 } 49 50 request.Imp[0].Ext = extJson 51 52 adapterReq, err := a.buildRequest(request) 53 if err != nil { 54 return nil, []error{err} 55 } 56 57 if adapterReq != nil { 58 adapterRequests = append(adapterRequests, adapterReq) 59 } 60 } 61 request.Imp = originalImpSlice 62 return adapterRequests, nil 63 } 64 65 func (a *adapter) buildRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) { 66 reqJSON, err := json.Marshal(request) 67 if err != nil { 68 return nil, err 69 } 70 71 headers := http.Header{} 72 headers.Add("Content-Type", "application/json;charset=utf-8") 73 headers.Add("Accept", "application/json") 74 75 return &adapters.RequestData{ 76 Method: "POST", 77 Uri: a.endpoint, 78 Body: reqJSON, 79 Headers: headers, 80 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 81 }, nil 82 } 83 84 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 85 if responseData.StatusCode == http.StatusNoContent { 86 return nil, nil 87 } 88 89 if responseData.StatusCode != http.StatusOK { 90 err := &errortypes.BadServerResponse{ 91 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode), 92 } 93 return nil, []error{err} 94 } 95 96 var response openrtb2.BidResponse 97 if err := json.Unmarshal(responseData.Body, &response); err != nil { 98 return nil, []error{err} 99 } 100 101 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 102 bidResponse.Currency = response.Cur 103 104 impsMappedByID := make(map[string]openrtb2.Imp, len(request.Imp)) 105 for i, imp := range request.Imp { 106 impsMappedByID[request.Imp[i].ID] = imp 107 } 108 109 for _, seatBid := range response.SeatBid { 110 for i := range seatBid.Bid { 111 bidType, err := getMediaTypeForImp(seatBid.Bid[i].ImpID, impsMappedByID) 112 if err != nil { 113 return nil, []error{err} 114 } 115 116 b := &adapters.TypedBid{ 117 Bid: &seatBid.Bid[i], 118 BidType: bidType, 119 } 120 bidResponse.Bids = append(bidResponse.Bids, b) 121 } 122 } 123 return bidResponse, nil 124 } 125 126 func getMediaTypeForImp(impID string, impMap map[string]openrtb2.Imp) (openrtb_ext.BidType, error) { 127 if index, found := impMap[impID]; found { 128 if index.Banner != nil { 129 return openrtb_ext.BidTypeBanner, nil 130 } 131 if index.Video != nil { 132 return openrtb_ext.BidTypeVideo, nil 133 } 134 if index.Native != nil { 135 return openrtb_ext.BidTypeNative, nil 136 } 137 } 138 139 return "", &errortypes.BadInput{ 140 Message: fmt.Sprintf("Failed to find impression \"%s\"", impID), 141 } 142 }