github.com/prebid/prebid-server/v2@v2.18.0/adapters/e_volution/evolution.go (about) 1 package evolution 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 URI string 18 } 19 20 type bidExt struct { 21 MediaType openrtb_ext.BidType `json:"mediaType"` 22 } 23 24 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 25 bidder := &adapter{ 26 URI: config.Endpoint, 27 } 28 return bidder, nil 29 } 30 31 func (a *adapter) MakeRequests( 32 openRTBRequest *openrtb2.BidRequest, 33 reqInfo *adapters.ExtraRequestInfo, 34 ) ( 35 requestsToBidder []*adapters.RequestData, 36 errs []error, 37 ) { 38 39 reqJSON, err := json.Marshal(openRTBRequest) 40 if err != nil { 41 return nil, []error{err} 42 } 43 44 headers := http.Header{} 45 headers.Add("Content-Type", "application/json;charset=utf-8") 46 headers.Add("Accept", "application/json") 47 48 return []*adapters.RequestData{{ 49 Method: http.MethodPost, 50 Body: reqJSON, 51 Uri: a.URI, 52 Headers: headers, 53 ImpIDs: openrtb_ext.GetImpIDs(openRTBRequest.Imp), 54 }}, nil 55 } 56 57 func (a *adapter) MakeBids( 58 openRTBRequest *openrtb2.BidRequest, 59 requestToBidder *adapters.RequestData, 60 bidderRawResponse *adapters.ResponseData, 61 ) ( 62 bidderResponse *adapters.BidderResponse, 63 errs []error, 64 ) { 65 if bidderRawResponse.StatusCode == http.StatusNoContent { 66 return nil, nil 67 } 68 69 if bidderRawResponse.StatusCode == http.StatusBadRequest { 70 return nil, []error{&errortypes.BadInput{ 71 Message: fmt.Sprintf("Bad Request. %s", string(bidderRawResponse.Body)), 72 }} 73 } 74 75 if bidderRawResponse.StatusCode == http.StatusServiceUnavailable { 76 return nil, nil 77 } 78 79 if bidderRawResponse.StatusCode != http.StatusOK { 80 return nil, []error{&errortypes.BadServerResponse{ 81 Message: fmt.Sprintf("Something went wrong, please contact your Account Manager. Status Code: [ %d ] ", bidderRawResponse.StatusCode), 82 }} 83 } 84 85 responseBody := bidderRawResponse.Body 86 var bidResp openrtb2.BidResponse 87 if err := json.Unmarshal(responseBody, &bidResp); err != nil { 88 return nil, []error{&errortypes.BadServerResponse{ 89 Message: fmt.Sprintf("Bad response, %s", err), 90 }} 91 } 92 93 if len(bidResp.SeatBid) == 0 { 94 return nil, []error{&errortypes.BadServerResponse{ 95 Message: fmt.Sprintf("Empty seatbid"), 96 }} 97 } 98 99 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(bidResp.SeatBid[0].Bid)) 100 sb := bidResp.SeatBid[0] 101 for i := range sb.Bid { 102 var bidType openrtb_ext.BidType 103 var bidExt bidExt 104 if err := json.Unmarshal(sb.Bid[i].Ext, &bidExt); err != nil { 105 bidType = openrtb_ext.BidTypeBanner 106 } else { 107 bidType = bidExt.MediaType 108 } 109 bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ 110 Bid: &sb.Bid[i], 111 BidType: bidType, 112 }) 113 } 114 return bidResponse, nil 115 }