github.com/prebid/prebid-server/v2@v2.18.0/adapters/boldwin/boldwin.go (about) 1 package boldwin 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 BoldwinBidderExt reqBodyExtBidder `json:"bidder"` 21 } 22 23 type reqBodyExtBidder struct { 24 Type string `json:"type"` 25 PlacementID string `json:"placementId,omitempty"` 26 EndpointID string `json:"endpointId,omitempty"` 27 } 28 29 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 30 bidder := &adapter{ 31 endpoint: config.Endpoint, 32 } 33 return bidder, nil 34 } 35 36 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 37 var err error 38 var adapterRequests []*adapters.RequestData 39 40 reqCopy := *request 41 for _, imp := range request.Imp { 42 reqCopy.Imp = []openrtb2.Imp{imp} 43 44 var bidderExt adapters.ExtImpBidder 45 var boldwinExt openrtb_ext.ImpExtBoldwin 46 47 if err = json.Unmarshal(reqCopy.Imp[0].Ext, &bidderExt); err != nil { 48 return nil, []error{err} 49 } 50 if err = json.Unmarshal(bidderExt.Bidder, &boldwinExt); err != nil { 51 return nil, []error{err} 52 } 53 54 temp := reqBodyExt{BoldwinBidderExt: reqBodyExtBidder{}} 55 56 if boldwinExt.PlacementID != "" { 57 temp.BoldwinBidderExt.PlacementID = boldwinExt.PlacementID 58 temp.BoldwinBidderExt.Type = "publisher" 59 } else if boldwinExt.EndpointID != "" { 60 temp.BoldwinBidderExt.EndpointID = boldwinExt.EndpointID 61 temp.BoldwinBidderExt.Type = "network" 62 } 63 64 finalyImpExt, err := json.Marshal(temp) 65 if err != nil { 66 return nil, []error{err} 67 } 68 69 reqCopy.Imp[0].Ext = finalyImpExt 70 71 adapterReq, err := a.makeRequest(&reqCopy) 72 if err != nil { 73 return nil, []error{err} 74 } 75 76 if adapterReq != nil { 77 adapterRequests = append(adapterRequests, adapterReq) 78 } 79 } 80 return adapterRequests, nil 81 } 82 83 func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) { 84 reqJSON, err := json.Marshal(request) 85 if err != nil { 86 return nil, err 87 } 88 89 headers := http.Header{} 90 headers.Add("Content-Type", "application/json;charset=utf-8") 91 headers.Add("Accept", "application/json") 92 return &adapters.RequestData{ 93 Method: "POST", 94 Uri: a.endpoint, 95 Body: reqJSON, 96 Headers: headers, 97 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 98 }, err 99 } 100 101 func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 102 if responseData.StatusCode == http.StatusNoContent { 103 return nil, nil 104 } 105 106 if responseData.StatusCode != http.StatusOK { 107 err := &errortypes.BadServerResponse{ 108 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode), 109 } 110 return nil, []error{err} 111 } 112 113 var response openrtb2.BidResponse 114 if err := json.Unmarshal(responseData.Body, &response); err != nil { 115 return nil, []error{err} 116 } 117 118 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 119 bidResponse.Currency = response.Cur 120 for _, seatBid := range response.SeatBid { 121 for i := range seatBid.Bid { 122 bidType, err := getBidMediaType(&seatBid.Bid[i]) 123 if err != nil { 124 return nil, []error{err} 125 } 126 127 b := &adapters.TypedBid{ 128 Bid: &seatBid.Bid[i], 129 BidType: bidType, 130 } 131 bidResponse.Bids = append(bidResponse.Bids, b) 132 } 133 } 134 return bidResponse, nil 135 } 136 137 func getBidMediaType(bid *openrtb2.Bid) (openrtb_ext.BidType, error) { 138 switch bid.MType { 139 case openrtb2.MarkupBanner: 140 return openrtb_ext.BidTypeBanner, nil 141 case openrtb2.MarkupVideo: 142 return openrtb_ext.BidTypeVideo, nil 143 case openrtb2.MarkupNative: 144 return openrtb_ext.BidTypeNative, nil 145 default: 146 return "", fmt.Errorf("Unable to fetch mediaType in multi-format: %s", bid.ImpID) 147 } 148 }