github.com/prebid/prebid-server/v2@v2.18.0/adapters/smilewanted/smilewanted.go (about) 1 package smilewanted 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 8 "github.com/prebid/openrtb/v20/openrtb2" 9 "github.com/prebid/prebid-server/v2/adapters" 10 "github.com/prebid/prebid-server/v2/config" 11 "github.com/prebid/prebid-server/v2/errortypes" 12 "github.com/prebid/prebid-server/v2/openrtb_ext" 13 ) 14 15 type adapter struct { 16 URI string 17 } 18 19 func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 20 21 request.AT = 1 //Defaulting to first price auction for all prebid requests 22 23 reqJSON, err := json.Marshal(request) 24 if err != nil { 25 return nil, []error{&errortypes.BadInput{ 26 Message: fmt.Sprintf("Json not encoded. err: %s", err), 27 }} 28 } 29 30 headers := http.Header{} 31 headers.Add("Content-Type", "application/json;charset=utf-8") 32 headers.Add("Accept", "application/json") 33 headers.Add("x-openrtb-version", "2.5") 34 headers.Add("sw-integration-type", "prebid_server") 35 36 return []*adapters.RequestData{{ 37 Method: "POST", 38 Uri: a.URI, 39 Body: reqJSON, 40 Headers: headers, 41 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 42 }}, []error{} 43 } 44 45 func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { 46 47 if response.StatusCode == http.StatusNoContent { 48 return nil, nil 49 } 50 51 if response.StatusCode == http.StatusBadRequest { 52 return nil, []error{&errortypes.BadInput{ 53 Message: fmt.Sprintf("Unexpected status code: %d.", response.StatusCode), 54 }} 55 } 56 57 if response.StatusCode != http.StatusOK { 58 return nil, []error{&errortypes.BadServerResponse{ 59 Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", response.StatusCode), 60 }} 61 } 62 63 var bidResp openrtb2.BidResponse 64 if err := json.Unmarshal(response.Body, &bidResp); err != nil { 65 return nil, []error{&errortypes.BadServerResponse{ 66 Message: fmt.Sprintf("Bad server response: %s.", err), 67 }} 68 } 69 70 var bidReq openrtb2.BidRequest 71 if err := json.Unmarshal(externalRequest.Body, &bidReq); err != nil { 72 return nil, []error{err} 73 } 74 75 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(bidResp.SeatBid[0].Bid)) 76 sb := bidResp.SeatBid[0] 77 for i := 0; i < len(sb.Bid); i++ { 78 bid := sb.Bid[i] 79 bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ 80 Bid: &bid, 81 BidType: getMediaTypeForImp(bid.ImpID, internalRequest.Imp), 82 }) 83 } 84 return bidResponse, nil 85 } 86 87 // getMediaTypeForImp figures out which media type this bid is for. 88 func getMediaTypeForImp(impId string, imps []openrtb2.Imp) openrtb_ext.BidType { 89 mediaType := openrtb_ext.BidTypeBanner //default type 90 for _, imp := range imps { 91 if imp.ID == impId { 92 if imp.Video != nil { 93 mediaType = openrtb_ext.BidTypeVideo 94 } 95 return mediaType 96 } 97 } 98 return mediaType 99 } 100 101 // Builder builds a new instance of the SmileWanted adapter for the given bidder with the given config. 102 func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 103 bidder := &adapter{ 104 URI: config.Endpoint, 105 } 106 return bidder, nil 107 }