github.com/prebid/prebid-server/v2@v2.18.0/adapters/rise/rise.go (about) 1 package rise 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "net/http" 8 "strings" 9 10 "github.com/prebid/openrtb/v20/openrtb2" 11 12 "github.com/prebid/prebid-server/v2/adapters" 13 "github.com/prebid/prebid-server/v2/config" 14 "github.com/prebid/prebid-server/v2/openrtb_ext" 15 ) 16 17 // adapter is a Rise implementation of the adapters.Bidder interface. 18 type adapter struct { 19 endpointURL string 20 } 21 22 func Builder(_ openrtb_ext.BidderName, config config.Adapter, _ config.Server) (adapters.Bidder, error) { 23 return &adapter{ 24 endpointURL: config.Endpoint, 25 }, nil 26 } 27 28 // MakeRequests prepares the HTTP requests which should be made to fetch bids. 29 func (a *adapter) MakeRequests(openRTBRequest *openrtb2.BidRequest, _ *adapters.ExtraRequestInfo) (requestsToBidder []*adapters.RequestData, errs []error) { 30 org, err := extractOrg(openRTBRequest) 31 if err != nil { 32 errs = append(errs, fmt.Errorf("extractOrg: %w", err)) 33 return nil, errs 34 } 35 36 openRTBRequestJSON, err := json.Marshal(openRTBRequest) 37 if err != nil { 38 errs = append(errs, fmt.Errorf("marshal bidRequest: %w", err)) 39 return nil, errs 40 } 41 42 headers := http.Header{} 43 headers.Add("Content-Type", "application/json;charset=utf-8") 44 45 return append(requestsToBidder, &adapters.RequestData{ 46 Method: http.MethodPost, 47 Uri: a.endpointURL + "?publisher_id=" + org, 48 Body: openRTBRequestJSON, 49 Headers: headers, 50 ImpIDs: openrtb_ext.GetImpIDs(openRTBRequest.Imp), 51 }), nil 52 } 53 54 // MakeBids unpacks the server's response into Bids. 55 func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 56 if adapters.IsResponseStatusCodeNoContent(responseData) { 57 return nil, nil 58 } 59 60 if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil { 61 return nil, []error{err} 62 } 63 64 var response openrtb2.BidResponse 65 if err := json.Unmarshal(responseData.Body, &response); err != nil { 66 return nil, []error{err} 67 } 68 69 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 70 bidResponse.Currency = response.Cur 71 72 var errs []error 73 74 for _, seatBid := range response.SeatBid { 75 for i, bid := range seatBid.Bid { 76 bidType, err := getMediaTypeForBid(bid) 77 if err != nil { 78 errs = append(errs, err) 79 continue 80 } 81 82 bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ 83 Bid: &seatBid.Bid[i], 84 BidType: bidType, 85 }) 86 } 87 } 88 89 return bidResponse, errs 90 } 91 92 func extractOrg(openRTBRequest *openrtb2.BidRequest) (string, error) { 93 var err error 94 for _, imp := range openRTBRequest.Imp { 95 var bidderExt adapters.ExtImpBidder 96 if err = json.Unmarshal(imp.Ext, &bidderExt); err != nil { 97 return "", fmt.Errorf("unmarshal bidderExt: %w", err) 98 } 99 100 var impExt openrtb_ext.ImpExtRise 101 if err = json.Unmarshal(bidderExt.Bidder, &impExt); err != nil { 102 return "", fmt.Errorf("unmarshal ImpExtRise: %w", err) 103 } 104 105 if impExt.Org != "" { 106 return strings.TrimSpace(impExt.Org), nil 107 } 108 if impExt.PublisherID != "" { 109 return strings.TrimSpace(impExt.PublisherID), nil 110 } 111 } 112 113 return "", errors.New("no org or publisher_id supplied") 114 } 115 116 func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) { 117 switch bid.MType { 118 case openrtb2.MarkupBanner: 119 return openrtb_ext.BidTypeBanner, nil 120 case openrtb2.MarkupVideo: 121 return openrtb_ext.BidTypeVideo, nil 122 default: 123 return "", fmt.Errorf("unsupported MType %d", bid.MType) 124 } 125 }