github.com/prebid/prebid-server/v2@v2.18.0/adapters/freewheelssp/freewheelssp.go (about) 1 package freewheelssp 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 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 20 for i := 0; i < len(request.Imp); i++ { 21 imp := &request.Imp[i] 22 var bidderExt adapters.ExtImpBidder 23 if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { 24 return nil, []error{&errortypes.BadInput{ 25 Message: fmt.Sprintf("Invalid imp.ext for impression index %d. Error Infomation: %s", i, err.Error()), 26 }} 27 } 28 29 var impExt openrtb_ext.ImpExtFreewheelSSP 30 if err := json.Unmarshal(bidderExt.Bidder, &impExt); err != nil { 31 return nil, []error{&errortypes.BadInput{ 32 Message: fmt.Sprintf("Invalid imp.ext for impression index %d. Error Infomation: %s", i, err.Error()), 33 }} 34 } 35 36 var err error 37 if imp.Ext, err = json.Marshal(impExt); err != nil { 38 return nil, []error{&errortypes.BadInput{ 39 Message: fmt.Sprintf("Unable to transfer requestImpExt to Json fomat, %s", err.Error()), 40 }} 41 } 42 } 43 44 requestJSON, err := json.Marshal(request) 45 if err != nil { 46 return nil, []error{&errortypes.BadInput{ 47 Message: fmt.Sprintf("Unable to transfer request to Json fomat, %s", err.Error()), 48 }} 49 } 50 51 headers := http.Header{} 52 headers.Add("Componentid", "prebid-go") 53 54 requestData := &adapters.RequestData{ 55 Method: "POST", 56 Uri: a.endpoint, 57 Body: requestJSON, 58 Headers: headers, 59 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 60 } 61 return []*adapters.RequestData{requestData}, nil 62 } 63 64 func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { 65 if response.StatusCode == http.StatusNoContent { 66 return nil, nil 67 } 68 69 if response.StatusCode != http.StatusOK { 70 return nil, []error{&errortypes.BadServerResponse{ 71 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode), 72 }} 73 } 74 75 var bidResp openrtb2.BidResponse 76 if err := json.Unmarshal(response.Body, &bidResp); err != nil { 77 return nil, []error{err} 78 } 79 80 cur := bidResp.Cur 81 bidResponse := &adapters.BidderResponse{ 82 Currency: cur, 83 Bids: []*adapters.TypedBid{}, 84 } 85 86 bidType := openrtb_ext.BidTypeVideo 87 88 for _, seatBid := range bidResp.SeatBid { 89 for i := range seatBid.Bid { 90 bid := seatBid.Bid[i] 91 bidVideo := openrtb_ext.ExtBidPrebidVideo{} 92 if len(bid.Cat) > 0 { 93 bidVideo.PrimaryCategory = bid.Cat[0] 94 } 95 if bid.Dur > 0 { 96 bidVideo.Duration = int(bid.Dur) 97 } 98 adTypeBid := &adapters.TypedBid{ 99 Bid: &bid, 100 BidType: bidType, 101 BidVideo: &bidVideo, 102 } 103 bidResponse.Bids = append(bidResponse.Bids, adTypeBid) 104 } 105 } 106 return bidResponse, nil 107 } 108 109 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 110 bidder := &adapter{ 111 config.Endpoint, 112 } 113 return bidder, nil 114 }