github.com/prebid/prebid-server/v2@v2.18.0/adapters/bidmyadz/bidmyadz.go (about) 1 package bidmyadz 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 endpoint string 18 } 19 20 type bidExt struct { 21 MediaType string `json:"mediaType"` 22 } 23 24 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 25 bidder := &adapter{ 26 endpoint: 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 var errors []error 40 41 if len(openRTBRequest.Imp) > 1 { 42 errors = append(errors, &errortypes.BadInput{ 43 Message: "Bidder does not support multi impression", 44 }) 45 } 46 47 if openRTBRequest.Device.IP == "" && openRTBRequest.Device.IPv6 == "" { 48 errors = append(errors, &errortypes.BadInput{ 49 Message: "IP/IPv6 is a required field", 50 }) 51 } 52 53 if openRTBRequest.Device.UA == "" { 54 errors = append(errors, &errortypes.BadInput{ 55 Message: "User-Agent is a required field", 56 }) 57 } 58 59 if len(errors) != 0 { 60 return nil, errors 61 } 62 63 reqJSON, err := json.Marshal(openRTBRequest) 64 if err != nil { 65 return nil, []error{err} 66 } 67 68 headers := http.Header{} 69 headers.Add("Content-Type", "application/json;charset=utf-8") 70 headers.Add("Accept", "application/json") 71 headers.Add("X-Openrtb-Version", "2.5") 72 73 return []*adapters.RequestData{{ 74 Method: http.MethodPost, 75 Body: reqJSON, 76 Uri: a.endpoint, 77 Headers: headers, 78 ImpIDs: openrtb_ext.GetImpIDs(openRTBRequest.Imp), 79 }}, nil 80 } 81 82 func (a *adapter) MakeBids( 83 openRTBRequest *openrtb2.BidRequest, 84 requestToBidder *adapters.RequestData, 85 bidderRawResponse *adapters.ResponseData, 86 ) ( 87 bidderResponse *adapters.BidderResponse, 88 errs []error, 89 ) { 90 if bidderRawResponse.StatusCode == http.StatusNoContent { 91 return nil, nil 92 } 93 94 if bidderRawResponse.StatusCode == http.StatusBadRequest { 95 return nil, []error{&errortypes.BadInput{ 96 Message: fmt.Sprintf("Bad Request. %s", string(bidderRawResponse.Body)), 97 }} 98 } 99 100 if bidderRawResponse.StatusCode == http.StatusServiceUnavailable { 101 return nil, []error{&errortypes.BadServerResponse{ 102 Message: "Bidder is unavailable. Please contact your account manager.", 103 }} 104 } 105 106 if bidderRawResponse.StatusCode != http.StatusOK { 107 return nil, []error{&errortypes.BadServerResponse{ 108 Message: fmt.Sprintf("Something went wrong. Status Code: [ %d ] %s", bidderRawResponse.StatusCode, string(bidderRawResponse.Body)), 109 }} 110 } 111 112 responseBody := bidderRawResponse.Body 113 var bidResp openrtb2.BidResponse 114 if err := json.Unmarshal(responseBody, &bidResp); err != nil { 115 return nil, []error{err} 116 } 117 118 if len(bidResp.SeatBid) == 0 { 119 return nil, []error{&errortypes.BadServerResponse{ 120 Message: "Empty SeatBid", 121 }} 122 } 123 124 bidResponse := adapters.NewBidderResponseWithBidsCapacity(1) 125 126 bids := bidResp.SeatBid[0].Bid 127 128 if len(bids) == 0 { 129 return nil, []error{&errortypes.BadServerResponse{ 130 Message: "Empty SeatBid.Bids", 131 }} 132 } 133 134 bid := bids[0] 135 136 var bidExt bidExt 137 var bidType openrtb_ext.BidType 138 139 if err := json.Unmarshal(bid.Ext, &bidExt); err != nil { 140 return nil, []error{&errortypes.BadServerResponse{ 141 Message: fmt.Sprintf("BidExt parsing error. %s", err.Error()), 142 }} 143 } 144 145 bidType, err := getBidType(bidExt) 146 147 if err != nil { 148 return nil, []error{err} 149 } 150 151 bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ 152 Bid: &bid, 153 BidType: bidType, 154 }) 155 return bidResponse, nil 156 } 157 158 func getBidType(ext bidExt) (openrtb_ext.BidType, error) { 159 return openrtb_ext.ParseBidType(ext.MediaType) 160 }