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