github.com/prebid/prebid-server/v2@v2.18.0/adapters/stroeerCore/stroeercore.go (about) 1 package stroeerCore 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 8 "github.com/prebid/openrtb/v20/openrtb2" 9 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 URL string `json:"url"` 18 Server config.Server 19 } 20 21 type response struct { 22 Bids []bidResponse `json:"bids"` 23 } 24 25 type bidResponse struct { 26 ID string `json:"id"` 27 BidID string `json:"bidId"` 28 CPM float64 `json:"cpm"` 29 Width int64 `json:"width"` 30 Height int64 `json:"height"` 31 Ad string `json:"ad"` 32 CrID string `json:"crid"` 33 Mtype string `json:"mtype"` 34 DSA json.RawMessage `json:"dsa"` 35 } 36 37 type bidExt struct { 38 DSA json.RawMessage `json:"dsa,omitempty"` 39 } 40 41 func (b *bidResponse) resolveMediaType() (mt openrtb2.MarkupType, bt openrtb_ext.BidType, err error) { 42 switch b.Mtype { 43 case "banner": 44 return openrtb2.MarkupBanner, openrtb_ext.BidTypeBanner, nil 45 case "video": 46 return openrtb2.MarkupVideo, openrtb_ext.BidTypeVideo, nil 47 default: 48 return mt, bt, fmt.Errorf("unable to determine media type for bid with id \"%s\"", b.BidID) 49 } 50 } 51 52 func (a *adapter) MakeBids(bidRequest *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 53 if responseData.StatusCode != http.StatusOK { 54 return nil, []error{&errortypes.BadServerResponse{ 55 Message: fmt.Sprintf("Unexpected http status code: %d.", responseData.StatusCode), 56 }} 57 } 58 59 var errors []error 60 stroeerResponse := response{} 61 62 if err := json.Unmarshal(responseData.Body, &stroeerResponse); err != nil { 63 errors = append(errors, err) 64 return nil, errors 65 } 66 67 bidderResponse := adapters.NewBidderResponseWithBidsCapacity(len(stroeerResponse.Bids)) 68 bidderResponse.Currency = "EUR" 69 70 for _, bid := range stroeerResponse.Bids { 71 markupType, bidType, err := bid.resolveMediaType() 72 if err != nil { 73 errors = append(errors, &errortypes.BadServerResponse{ 74 Message: fmt.Sprintf("Bid media type error: %s", err.Error()), 75 }) 76 continue 77 } 78 79 openRtbBid := openrtb2.Bid{ 80 ID: bid.ID, 81 ImpID: bid.BidID, 82 W: bid.Width, 83 H: bid.Height, 84 Price: bid.CPM, 85 AdM: bid.Ad, 86 CrID: bid.CrID, 87 MType: markupType, 88 } 89 90 if bid.DSA != nil { 91 dsaJson, err := json.Marshal(bidExt{bid.DSA}) 92 if err != nil { 93 errors = append(errors, err) 94 } else { 95 openRtbBid.Ext = dsaJson 96 } 97 } 98 99 bidderResponse.Bids = append(bidderResponse.Bids, &adapters.TypedBid{ 100 Bid: &openRtbBid, 101 BidType: bidType, 102 }) 103 } 104 105 return bidderResponse, errors 106 } 107 108 func (a *adapter) MakeRequests(bidRequest *openrtb2.BidRequest, extraRequestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 109 var errors []error 110 111 for idx := range bidRequest.Imp { 112 imp := &bidRequest.Imp[idx] 113 var bidderExt adapters.ExtImpBidder 114 if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { 115 errors = append(errors, err) 116 continue 117 } 118 119 var stroeerExt openrtb_ext.ExtImpStroeerCore 120 if err := json.Unmarshal(bidderExt.Bidder, &stroeerExt); err != nil { 121 errors = append(errors, err) 122 continue 123 } 124 125 imp.TagID = stroeerExt.Sid 126 } 127 128 reqJSON, err := json.Marshal(bidRequest) 129 if err != nil { 130 errors = append(errors, err) 131 return nil, errors 132 } 133 134 headers := http.Header{} 135 headers.Add("Content-Type", "application/json;charset=utf-8") 136 headers.Add("Accept", "application/json") 137 138 return []*adapters.RequestData{{ 139 Method: "POST", 140 Uri: a.URL, 141 Body: reqJSON, 142 Headers: headers, 143 ImpIDs: openrtb_ext.GetImpIDs(bidRequest.Imp), 144 }}, errors 145 } 146 147 // Builder builds a new instance of the StroeerCore adapter for the given bidder with the given config. 148 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 149 bidder := &adapter{ 150 URL: config.Endpoint, 151 } 152 return bidder, nil 153 }