github.com/prebid/prebid-server/v2@v2.18.0/adapters/bmtm/brightmountainmedia.go (about) 1 package bmtm 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "strconv" 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/openrtb_ext" 14 ) 15 16 type adapter struct { 17 endpoint string 18 } 19 20 // Builder builds a new instance of the BrightMountainMedia adapter for the given bidder with the given config. 21 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 22 bidder := &adapter{ 23 endpoint: config.Endpoint, 24 } 25 return bidder, nil 26 } 27 28 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 29 var extRequests []*adapters.RequestData 30 var errs []error 31 32 for _, imp := range request.Imp { 33 extRequest, err := a.makeRequest(*request, imp) 34 if err != nil { 35 errs = append(errs, err) 36 } else { 37 extRequests = append(extRequests, extRequest) 38 } 39 } 40 return extRequests, errs 41 } 42 43 func (a *adapter) makeRequest(ortbRequest openrtb2.BidRequest, ortbImp openrtb2.Imp) (*adapters.RequestData, error) { 44 if ortbImp.Banner == nil && ortbImp.Video == nil { 45 return nil, &errortypes.BadInput{ 46 Message: fmt.Sprintf("For Imp ID %s Banner or Video is undefined", ortbImp.ID), 47 } 48 } 49 50 var bidderExt adapters.ExtImpBidder 51 if err := json.Unmarshal(ortbImp.Ext, &bidderExt); err != nil { 52 return nil, &errortypes.BadInput{ 53 Message: fmt.Sprintf("Error unmarshalling ExtImpBidder: %s", err.Error()), 54 } 55 } 56 57 var bmtmExt openrtb_ext.ImpExtBmtm 58 if err := json.Unmarshal(bidderExt.Bidder, &bmtmExt); err != nil { 59 return nil, &errortypes.BadInput{ 60 Message: fmt.Sprintf("Error unmarshalling ExtImpBmtm: %s", err.Error()), 61 } 62 } 63 64 ortbImp.TagID = strconv.Itoa(bmtmExt.PlacementID) 65 ortbImp.Ext = nil 66 ortbRequest.Imp = []openrtb2.Imp{ortbImp} 67 68 requestJSON, err := json.Marshal(ortbRequest) 69 if err != nil { 70 return nil, err 71 } 72 73 requestData := &adapters.RequestData{ 74 Method: http.MethodPost, 75 Uri: a.endpoint, 76 Body: requestJSON, 77 Headers: setHeaders(ortbRequest), 78 ImpIDs: openrtb_ext.GetImpIDs(ortbRequest.Imp), 79 } 80 return requestData, nil 81 } 82 83 func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { 84 if response.StatusCode == http.StatusNoContent { 85 return nil, nil 86 } 87 88 if response.StatusCode == http.StatusBadRequest { 89 return nil, []error{&errortypes.BadInput{ 90 Message: fmt.Sprintf("Unknown status code: %d.", response.StatusCode), 91 }} 92 } 93 94 if response.StatusCode != http.StatusOK { 95 return nil, []error{&errortypes.BadServerResponse{ 96 Message: fmt.Sprintf("Unknown status code: %d.", response.StatusCode), 97 }} 98 } 99 100 var bidResp openrtb2.BidResponse 101 102 if err := json.Unmarshal(response.Body, &bidResp); err != nil { 103 return nil, []error{err} 104 } 105 106 bidResponse := adapters.NewBidderResponseWithBidsCapacity(1) 107 108 for _, sb := range bidResp.SeatBid { 109 for i := range sb.Bid { 110 bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ 111 Bid: &sb.Bid[i], 112 BidType: getMediaTypeForBid(sb.Bid[i].ImpID, internalRequest.Imp), 113 }) 114 } 115 } 116 return bidResponse, nil 117 } 118 119 func setHeaders(request openrtb2.BidRequest) http.Header { 120 headers := http.Header{} 121 122 headers.Add("Content-Type", "application/json;charset=utf-8") 123 headers.Add("Accept", "application/json") 124 125 if request.Device != nil { 126 if request.Device.UA != "" { 127 headers.Add("User-Agent", request.Device.UA) 128 } 129 130 if request.Device.IP != "" { 131 headers.Add("X-Forwarded-For", request.Device.IP) 132 } else if request.Device.IPv6 != "" { 133 headers.Add("X-Forwarded-For", request.Device.IPv6) 134 } 135 } 136 137 if request.Site != nil && request.Site.Page != "" { 138 headers.Add("Referer", request.Site.Page) 139 } 140 return headers 141 } 142 143 func getMediaTypeForBid(impID string, imps []openrtb2.Imp) openrtb_ext.BidType { 144 for _, imp := range imps { 145 if imp.ID == impID { 146 if imp.Banner != nil { 147 return openrtb_ext.BidTypeBanner 148 } else if imp.Video != nil { 149 return openrtb_ext.BidTypeVideo 150 } 151 } 152 } 153 return openrtb_ext.BidTypeBanner 154 }