github.com/prebid/prebid-server/v2@v2.18.0/adapters/definemedia/definemedia.go (about) 1 package definemedia 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 // Builder builds a new instance of the Foo adapter for the given bidder with the given config. 20 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 21 bidder := &adapter{ 22 endpoint: config.Endpoint, 23 } 24 return bidder, nil 25 } 26 27 // MakeRequests makes the HTTP requests which should be made to fetch bids. 28 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 29 var errors []error 30 requestJSON, err := json.Marshal(request) 31 if err != nil { 32 errors = append(errors, err) 33 return nil, errors 34 } 35 36 requestData := &adapters.RequestData{ 37 Method: "POST", 38 Uri: a.endpoint, 39 Body: requestJSON, 40 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 41 } 42 43 return []*adapters.RequestData{requestData}, errors 44 45 } 46 47 func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { 48 if response.StatusCode == http.StatusNoContent { 49 return nil, nil 50 } 51 52 if response.StatusCode == http.StatusBadRequest { 53 return nil, []error{&errortypes.BadInput{ 54 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode), 55 }} 56 } 57 58 if response.StatusCode != http.StatusOK { 59 return nil, []error{&errortypes.BadServerResponse{ 60 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode), 61 }} 62 } 63 64 var bidResp openrtb2.BidResponse 65 if err := json.Unmarshal(response.Body, &bidResp); err != nil { 66 return nil, []error{err} 67 } 68 69 bidsCapacity := 1 70 if len(bidResp.SeatBid) > 0 { 71 bidsCapacity = len(bidResp.SeatBid[0].Bid) 72 } 73 bidResponse := adapters.NewBidderResponseWithBidsCapacity(bidsCapacity) 74 var errors []error 75 for _, sb := range bidResp.SeatBid { 76 for i, bid := range sb.Bid { 77 bidType, err := getMediaTypeForBid(bid) 78 if err != nil { 79 errors = append(errors, err) 80 continue 81 } 82 bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ 83 Bid: &sb.Bid[i], 84 BidType: bidType, 85 }) 86 87 } 88 } 89 90 return bidResponse, errors 91 } 92 93 func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) { 94 if bid.Ext != nil { 95 var bidExt openrtb_ext.ExtBid 96 err := json.Unmarshal(bid.Ext, &bidExt) 97 if err == nil && bidExt.Prebid != nil { 98 if (bidExt.Prebid.Type == openrtb_ext.BidTypeBanner) || (bidExt.Prebid.Type == openrtb_ext.BidTypeNative) { 99 return openrtb_ext.ParseBidType(string(bidExt.Prebid.Type)) 100 } 101 return "", &errortypes.BadServerResponse{ 102 Message: fmt.Sprintf("Invalid mediatype in the impression"), 103 } 104 } 105 } 106 107 return "", &errortypes.BadServerResponse{ 108 Message: fmt.Sprintf("Failed to parse impression \"%s\" mediatype", bid.ImpID), 109 } 110 }