github.com/prebid/prebid-server/v2@v2.18.0/adapters/medianet/medianet.go (about) 1 package medianet 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "net/url" 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 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 21 var errs []error 22 23 reqJson, err := json.Marshal(request) 24 if err != nil { 25 errs = append(errs, err) 26 return nil, errs 27 } 28 29 headers := http.Header{} 30 headers.Add("Content-Type", "application/json;charset=utf-8") 31 32 return []*adapters.RequestData{{ 33 Method: "POST", 34 Uri: a.endpoint, 35 Body: reqJson, 36 Headers: headers, 37 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 38 }}, errs 39 } 40 41 func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { 42 var errs []error 43 44 if response.StatusCode == http.StatusNoContent { 45 return nil, nil 46 } 47 48 if response.StatusCode == http.StatusBadRequest { 49 return nil, []error{&errortypes.BadInput{ 50 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode), 51 }} 52 } 53 54 if response.StatusCode != http.StatusOK { 55 return nil, []error{&errortypes.BadServerResponse{ 56 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode), 57 }} 58 } 59 60 var bidResp openrtb2.BidResponse 61 62 if err := json.Unmarshal(response.Body, &bidResp); err != nil { 63 return nil, []error{err} 64 } 65 66 bidResponse := adapters.NewBidderResponse() 67 68 for _, sb := range bidResp.SeatBid { 69 for i := range sb.Bid { 70 bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp) 71 if err != nil { 72 errs = append(errs, err) 73 } else { 74 b := &adapters.TypedBid{ 75 Bid: &sb.Bid[i], 76 BidType: bidType, 77 } 78 bidResponse.Bids = append(bidResponse.Bids, b) 79 } 80 } 81 } 82 return bidResponse, errs 83 } 84 85 // Builder builds a new instance of the Medianet adapter for the given bidder with the given config. 86 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 87 url := buildEndpoint(config.Endpoint, config.ExtraAdapterInfo) 88 return &adapter{ 89 endpoint: url, 90 }, nil 91 } 92 93 func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) { 94 mediaType := openrtb_ext.BidTypeBanner 95 for _, imp := range imps { 96 if imp.ID == impID { 97 if imp.Banner == nil && imp.Video != nil { 98 mediaType = openrtb_ext.BidTypeVideo 99 } 100 return mediaType, nil 101 } 102 } 103 104 return "", &errortypes.BadInput{ 105 Message: fmt.Sprintf("Failed to find impression \"%s\" ", impID), 106 } 107 } 108 109 func buildEndpoint(mnetUrl, hostUrl string) string { 110 111 if len(hostUrl) == 0 { 112 return mnetUrl 113 } 114 urlObject, err := url.Parse(mnetUrl) 115 if err != nil { 116 return mnetUrl 117 } 118 values := urlObject.Query() 119 values.Add("src", hostUrl) 120 urlObject.RawQuery = values.Encode() 121 return urlObject.String() 122 }