github.com/prebid/prebid-server/v2@v2.18.0/adapters/tpmn/tpmn.go (about) 1 package tpmn 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "strings" 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 // TpmnAdapter struct 16 type adapter struct { 17 uri string 18 } 19 20 // MakeRequests makes the HTTP requests which should be made to fetch bids from TpmnBidder. 21 func (rcv *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { 22 validImps, errs := getValidImpressions(request, reqInfo) 23 if len(validImps) == 0 { 24 return nil, errs 25 } 26 27 request.Imp = validImps 28 29 requestBodyJSON, err := json.Marshal(request) 30 if err != nil { 31 errs = append(errs, err) 32 return nil, errs 33 } 34 35 headers := http.Header{} 36 headers.Add("Content-Type", "application/json;charset=utf-8") 37 headers.Add("Accept", "application/json") 38 39 return []*adapters.RequestData{{ 40 Method: http.MethodPost, 41 Uri: rcv.uri, 42 Body: requestBodyJSON, 43 Headers: headers, 44 ImpIDs: openrtb_ext.GetImpIDs(request.Imp), 45 }}, errs 46 } 47 48 // getValidImpressions validate imps and check for bid floor currency. Convert to EUR if necessary 49 func getValidImpressions(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]openrtb2.Imp, []error) { 50 var errs []error 51 var validImps []openrtb2.Imp 52 53 for _, imp := range request.Imp { 54 if err := preprocessBidFloorCurrency(&imp, reqInfo); err != nil { 55 errs = append(errs, err) 56 continue 57 } 58 validImps = append(validImps, imp) 59 } 60 return validImps, errs 61 } 62 63 func preprocessBidFloorCurrency(imp *openrtb2.Imp, reqInfo *adapters.ExtraRequestInfo) error { 64 // we expect every currency related data to be EUR 65 if imp.BidFloor > 0 && strings.ToUpper(imp.BidFloorCur) != "USD" && imp.BidFloorCur != "" { 66 if convertedValue, err := reqInfo.ConvertCurrency(imp.BidFloor, imp.BidFloorCur, "USD"); err != nil { 67 return err 68 } else { 69 imp.BidFloor = convertedValue 70 } 71 } 72 imp.BidFloorCur = "USD" 73 return nil 74 } 75 76 func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { 77 if adapters.IsResponseStatusCodeNoContent(responseData) { 78 return nil, nil 79 } 80 81 if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil { 82 return nil, []error{err} 83 } 84 85 var response openrtb2.BidResponse 86 if err := json.Unmarshal(responseData.Body, &response); err != nil { 87 return nil, []error{fmt.Errorf("bid response unmarshal: %v", err)} 88 } 89 90 bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) 91 bidResponse.Currency = response.Cur 92 for _, seatBid := range response.SeatBid { 93 for i, bid := range seatBid.Bid { 94 bidType, err := getMediaTypeForImp(bid) 95 if err != nil { 96 return nil, []error{err} 97 } 98 b := &adapters.TypedBid{ 99 Bid: &seatBid.Bid[i], 100 BidType: bidType, 101 } 102 bidResponse.Bids = append(bidResponse.Bids, b) 103 } 104 } 105 return bidResponse, nil 106 } 107 108 func getMediaTypeForImp(bid openrtb2.Bid) (openrtb_ext.BidType, error) { 109 switch bid.MType { 110 case openrtb2.MarkupBanner: 111 return openrtb_ext.BidTypeBanner, nil 112 case openrtb2.MarkupVideo: 113 return openrtb_ext.BidTypeVideo, nil 114 case openrtb2.MarkupNative: 115 return openrtb_ext.BidTypeNative, nil 116 default: 117 return "", fmt.Errorf("unsupported MType %d", bid.MType) 118 } 119 } 120 121 // Builder builds a new instance of the TpmnBidder adapter for the given bidder with the given config. 122 func Builder(_ openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { 123 bidder := &adapter{ 124 uri: config.Endpoint, 125 } 126 return bidder, nil 127 }