github.com/prebid/prebid-server/v2@v2.18.0/adapters/smartx/smartx.go (about) 1 package smartx 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "net/http" 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/openrtb_ext" 13 ) 14 15 type adapter struct { 16 endpointURL string 17 } 18 19 func Builder(_ openrtb_ext.BidderName, config config.Adapter, _ config.Server) (adapters.Bidder, error) { 20 return &adapter{ 21 endpointURL: config.Endpoint, 22 }, nil 23 } 24 25 func (a *adapter) MakeRequests(openRTBRequest *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) (requestsToBidder []*adapters.RequestData, errs []error) { 26 openRTBRequestJSON, err := json.Marshal(openRTBRequest) 27 if err != nil { 28 errs = append(errs, fmt.Errorf("marshal bidRequest: %w", err)) 29 return nil, errs 30 } 31 32 headers := http.Header{} 33 headers.Add("Content-Type", "application/json;charset=utf-8") 34 headers.Add("x-openrtb-version", "2.5") 35 36 if openRTBRequest.Device != nil { 37 if openRTBRequest.Device.UA != "" { 38 headers.Set("User-Agent", openRTBRequest.Device.UA) 39 } 40 41 if openRTBRequest.Device.IP != "" { 42 headers.Set("Forwarded", "for="+openRTBRequest.Device.IP) 43 headers.Set("X-Forwarded-For", openRTBRequest.Device.IP) 44 } 45 } 46 47 return append(requestsToBidder, &adapters.RequestData{ 48 Method: http.MethodPost, 49 Uri: a.endpointURL, 50 Body: openRTBRequestJSON, 51 Headers: headers, 52 ImpIDs: openrtb_ext.GetImpIDs(openRTBRequest.Imp), 53 }), nil 54 } 55 56 func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 57 if adapters.IsResponseStatusCodeNoContent(responseData) { 58 return nil, nil 59 } 60 61 if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil { 62 return nil, []error{err} 63 } 64 65 var response openrtb2.BidResponse 66 if err := json.Unmarshal(responseData.Body, &response); err != nil { 67 return nil, []error{err} 68 } 69 70 if len(response.SeatBid) == 0 { 71 return nil, []error{errors.New("no bidders found in JSON response")} 72 } 73 74 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 75 if response.Cur != "" { 76 bidResponse.Currency = response.Cur 77 } 78 79 var errs []error 80 81 for _, seatBid := range response.SeatBid { 82 for i := range seatBid.Bid { 83 bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ 84 Bid: &seatBid.Bid[i], 85 BidType: openrtb_ext.BidTypeVideo, 86 }) 87 } 88 } 89 90 return bidResponse, errs 91 }