github.com/prebid/prebid-server/v2@v2.18.0/adapters/trustedstack/trustedstack.go (about) 1 package trustedstack 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/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 21 reqJson, err := json.Marshal(request) 22 if err != nil { 23 return nil, []error{err} 24 } 25 26 headers := http.Header{} 27 headers.Add("Content-Type", "application/json;charset=utf-8") 28 29 return []*adapters.RequestData{{ 30 Method: "POST", 31 Uri: a.endpoint, 32 Body: reqJson, 33 Headers: headers, 34 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 35 }}, nil 36 } 37 38 func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { 39 var errs []error 40 41 if response.StatusCode == http.StatusNoContent { 42 return nil, nil 43 } 44 45 if err := adapters.CheckResponseStatusCodeForErrors(response); err != nil { 46 return nil, []error{err} 47 } 48 49 var bidResp openrtb2.BidResponse 50 51 if err := json.Unmarshal(response.Body, &bidResp); err != nil { 52 return nil, []error{err} 53 } 54 55 bidResponse := adapters.NewBidderResponse() 56 57 for _, sb := range bidResp.SeatBid { 58 for i := range sb.Bid { 59 bidType, err := getBidMediaTypeFromMtype(&sb.Bid[i]) 60 if err != nil { 61 errs = append(errs, err) 62 } else { 63 b := &adapters.TypedBid{ 64 Bid: &sb.Bid[i], 65 BidType: bidType, 66 } 67 bidResponse.Bids = append(bidResponse.Bids, b) 68 } 69 } 70 } 71 return bidResponse, errs 72 } 73 74 // Builder builds a new instance of the Trustedstack adapter for the given bidder with the given config. 75 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 76 url := buildEndpoint(config.Endpoint, server.ExternalUrl) 77 return &adapter{ 78 endpoint: url, 79 }, nil 80 } 81 82 func getBidMediaTypeFromMtype(bid *openrtb2.Bid) (openrtb_ext.BidType, error) { 83 switch bid.MType { 84 case openrtb2.MarkupBanner: 85 return openrtb_ext.BidTypeBanner, nil 86 case openrtb2.MarkupVideo: 87 return openrtb_ext.BidTypeVideo, nil 88 case openrtb2.MarkupNative: 89 return openrtb_ext.BidTypeNative, nil 90 default: 91 return "", fmt.Errorf("unable to fetch mediaType for imp: %s", bid.ImpID) 92 } 93 } 94 95 func buildEndpoint(trustedstackUrl, hostUrl string) string { 96 97 if len(hostUrl) == 0 { 98 return trustedstackUrl 99 } 100 urlObject, err := url.Parse(trustedstackUrl) 101 if err != nil { 102 return trustedstackUrl 103 } 104 values := urlObject.Query() 105 values.Add("src", hostUrl) 106 urlObject.RawQuery = values.Encode() 107 return urlObject.String() 108 }