-
Notifications
You must be signed in to change notification settings - Fork 765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Adapter: AdUp Tech #4076
New Adapter: AdUp Tech #4076
Changes from 1 commit
2ad858d
48deb93
4f3580c
1dc4b16
ec665c1
2a38edc
55f2048
8315a50
37adf42
09f4c2c
842d4e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package aduptech | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/prebid/openrtb/v20/openrtb2" | ||
"github.com/prebid/prebid-server/v3/adapters" | ||
"github.com/prebid/prebid-server/v3/config" | ||
"github.com/prebid/prebid-server/v3/currency" | ||
"github.com/prebid/prebid-server/v3/errortypes" | ||
"github.com/prebid/prebid-server/v3/openrtb_ext" | ||
"github.com/prebid/prebid-server/v3/util/jsonutil" | ||
) | ||
|
||
type adapter struct { | ||
endpoint string | ||
extraInfo ExtraInfo | ||
} | ||
|
||
type ExtraInfo struct { | ||
TargetCurrency string `json:"target_currency,omitempty"` | ||
} | ||
|
||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { | ||
var extraInfo ExtraInfo | ||
if err := jsonutil.Unmarshal([]byte(config.ExtraAdapterInfo), &extraInfo); err != nil { | ||
return nil, fmt.Errorf("invalid extra info: %v", err) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make sure the AdUp Tech adapter is configured with a known currency string, can we further the validation by checking whether or not the config value represents a valid three-digit currency code? The
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, it is not the same |
||
|
||
bidder := &adapter{ | ||
endpoint: config.Endpoint, | ||
extraInfo: extraInfo, | ||
} | ||
|
||
return bidder, nil | ||
} | ||
|
||
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
for i := range request.Imp { | ||
imp := &request.Imp[i] | ||
// Check if imp comes with bid floor amount defined in a foreign currency | ||
if imp.BidFloor > 0 && imp.BidFloorCur != "" && strings.ToUpper(imp.BidFloorCur) != a.extraInfo.TargetCurrency { | ||
|
||
convertedValue, err := a.convertCurrency(imp.BidFloor, imp.BidFloorCur, reqInfo) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
imp.BidFloorCur = a.extraInfo.TargetCurrency | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug?: There seems like a bug in here somewhere… the convertCurrency has a path that can handle errors and, instead, convert to "USD" - how is that choice made here if you always set the BidFloorCur to a.extraInfo.TargetCurrency - should you pass in a pointer to the extraInfo and set the chosen currency in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, we try to be ready for the eventuality of conversion rates to EUR not being there for every currency. The process is:
There are already special currency conversion JSON tests. See exemplary/simple-banner-convert-currency.json and exemplary/simple-banner-convert-currency-intermediate.json. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh… so it won't convert to USD and return no error - it would try to go from USD to EUR and return that result - therefore, the |
||
imp.BidFloor = convertedValue | ||
} | ||
} | ||
|
||
requestJSON, err := jsonutil.Marshal(request) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
requestData := &adapters.RequestData{ | ||
Method: http.MethodPost, | ||
Uri: a.endpoint, | ||
Body: requestJSON, | ||
ImpIDs: openrtb_ext.GetImpIDs(request.Imp), | ||
} | ||
|
||
return []*adapters.RequestData{requestData}, nil | ||
} | ||
|
||
func (a *adapter) convertCurrency(value float64, cur string, reqInfo *adapters.ExtraRequestInfo) (float64, error) { | ||
convertedValue, err := reqInfo.ConvertCurrency(value, cur, a.extraInfo.TargetCurrency) | ||
|
||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add supplemental JSONs for currency conversion failures |
||
var convErr currency.ConversionNotFoundError | ||
if errors.As(err, &convErr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick/suggestion: In go, there's a pattern that you try to take the "terminal" case first - return/break/panic/exit so that you don't need an else clause - it saves "cognitive overload" because you can just reason that everything after is the "happy path" (not having your brain full with remembering what the condition was when you get to the else way down the screen or having to scroll back up to understand it) - and also saves precious indentation 😄 (so make this !errors.As and pull the else clause into the block and remove the curly braces There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed, to have the terminal case first. |
||
|
||
// try again by first converting to USD | ||
// then convert to target_currency | ||
convertedValue, err = reqInfo.ConvertCurrency(value, cur, "USD") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. observation we should have constants or an enum of valid currencies… you can ignore for this PR but just making the note; filed #4134 |
||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
convertedValue, err = reqInfo.ConvertCurrency(convertedValue, "USD", a.extraInfo.TargetCurrency) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
} else { | ||
return 0, err | ||
} | ||
} | ||
return convertedValue, nil | ||
} | ||
|
||
func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
switch responseData.StatusCode { | ||
case http.StatusNoContent: | ||
return nil, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a supplemental JSON test where your mock response returns a 204 status code. |
||
case http.StatusBadRequest: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a supplemental JSON test where your mock response returns a 400 status code. |
||
return nil, []error{&errortypes.BadInput{ | ||
Message: fmt.Sprintf("Unexpected status code: 400. Run with request.debug = 1 for more info."), | ||
}} | ||
case http.StatusOK: | ||
// we don't need to do anything here and this is just so we can use the default case for the unexpected status code | ||
break | ||
default: | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode), | ||
}} | ||
|
||
} | ||
|
||
var response openrtb2.BidResponse | ||
if err := jsonutil.Unmarshal(responseData.Body, &response); err != nil { | ||
return nil, []error{err} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a supplemental JSON test where your mock response returns a bid response that fails to unmarshal successfully. This can be achieved by setting one of the fields to an incorrect data type. |
||
} | ||
|
||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) | ||
bidResponse.Currency = response.Cur | ||
|
||
var errs []error | ||
for i := range response.SeatBid { | ||
seatBid := &response.SeatBid[i] | ||
for j := range seatBid.Bid { | ||
bid := &seatBid.Bid[j] | ||
bidType, err := getBidType(bid.MType) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
Bid: bid, | ||
BidType: bidType, | ||
}) | ||
} | ||
} | ||
|
||
return bidResponse, errs | ||
} | ||
|
||
func getBidType(markupType openrtb2.MarkupType) (openrtb_ext.BidType, error) { | ||
switch markupType { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider this as a suggestion. The current implementation follows an anti-pattern, assumes that if there is a multi-format request, the media type defaults to openrtb_ext.BidTypeBanner. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, we strongly recommend implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider this as a suggestion. The current implementation follows an anti-pattern, assumes that if there is a multi-format request, the media type defaults to openrtb_ext.BidTypeNative. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, we strongly recommend implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider this as a suggestion. The current implementation follows an anti-pattern, assumes that if there is a multi-format request, the media type defaults to openrtb_ext.BidTypeBanner. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, we strongly recommend implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider this as a suggestion. The current implementation follows an anti-pattern, assumes that if there is a multi-format request, the media type defaults to openrtb_ext.BidTypeNative. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, we strongly recommend implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should be changed here? I assume this check is wrong, because we do exactly what the comment suggests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 this suggestion (x3?) is not very helpful There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why this is triggering but you can ignore it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider this as a suggestion. The current implementation follows an anti-pattern, assumes that if there is a multi-format request, the media type defaults to openrtb_ext.BidTypeBanner. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, we strongly recommend implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider this as a suggestion. The current implementation follows an anti-pattern, assumes that if there is a multi-format request, the media type defaults to openrtb_ext.BidTypeNative. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, we strongly recommend implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression. |
||
case openrtb2.MarkupNative: | ||
return openrtb_ext.BidTypeNative, nil | ||
case openrtb2.MarkupBanner: | ||
return openrtb_ext.BidTypeBanner, nil | ||
default: | ||
return "", &errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("Unknown markup type: %d", markupType), | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||||||||||||||||||||||||||
package aduptech | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||||||||
"github.com/stretchr/testify/assert" | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
"testing" | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
"github.com/prebid/prebid-server/v3/adapters/adapterstest" | ||||||||||||||||||||||||||||||
"github.com/prebid/prebid-server/v3/config" | ||||||||||||||||||||||||||||||
"github.com/prebid/prebid-server/v3/openrtb_ext" | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
func TestJsonSamples(t *testing.T) { | ||||||||||||||||||||||||||||||
bidder, buildErr := Builder(openrtb_ext.BidderAdUpTech, config.Adapter{ | ||||||||||||||||||||||||||||||
Endpoint: "https://example.com/rtb/bid", ExtraAdapterInfo: "{\"target_currency\": \"EUR\"}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: json can be easier to read/maintain if using the backtick string literal as long as the data doesn't contain backtick, which should be uncommon. (See https://go.dev/ref/spec#String_literals). Also… a few newlines couldn't hurt here to line up the fields so you can tell what goes with what at a glance:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are other places where this pattern can improve readability; I've only exemplified this one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, sorry, i thought gofmt takes care of everything. I didn't even try to format anything. |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
assert.NoError(t, buildErr, "Builder returned unexpected error") | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
adapterstest.RunJSONBidderTest(t, "aduptechtest", bidder) | ||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"site": { | ||
"page": "https://good.site/url" | ||
}, | ||
"imp": [{ | ||
"id": "test-imp-id", | ||
"bidfloor": 1.0, | ||
"bidfloorcur": "AUD", | ||
"banner": { | ||
"format": [{ | ||
"w": 300, | ||
"h": 250 | ||
}] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"publisher": "123456", | ||
"placement": "234567" | ||
} | ||
} | ||
}], | ||
"ext": { | ||
"prebid": { | ||
"currency": { | ||
"rates": { | ||
"EUR": { | ||
"USD": 0.05 | ||
}, | ||
"USD": { | ||
"AUD": 2.0 | ||
} | ||
}, | ||
"usepbsrates": false | ||
} | ||
} | ||
} | ||
}, | ||
"httpCalls": [{ | ||
"expectedRequest": { | ||
"uri": "https://example.com/rtb/bid", | ||
"body": { | ||
"id": "test-request-id", | ||
"site": { | ||
"page": "https://good.site/url" | ||
}, | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"bidfloor": 10, | ||
"bidfloorcur": "EUR", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"publisher": "123456", | ||
"placement": "234567" | ||
} | ||
} | ||
} | ||
], | ||
"ext": { | ||
"prebid": { | ||
"currency": { | ||
"rates": { | ||
"EUR": { | ||
"USD": 0.05 | ||
}, | ||
"USD": { | ||
"AUD": 2.0 | ||
} | ||
}, | ||
"usepbsrates": false | ||
} | ||
} | ||
} | ||
}, | ||
"impIDs":["test-imp-id"] | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"seatbid": [ | ||
{ | ||
"seat": "958", | ||
"bid": [{ | ||
"id": "7706636740145184841", | ||
"impid": "test-imp-id", | ||
"price": 0.500000, | ||
"adm": "some-test-ad", | ||
"adomain": ["example.com"], | ||
"crid": "29681110", | ||
"h": 250, | ||
"w": 300, | ||
"mtype": 1 | ||
}] | ||
} | ||
], | ||
"cur": "EUR" | ||
} | ||
} | ||
}], | ||
|
||
"expectedBidResponses": [{ | ||
"currency": "EUR", | ||
"bids": [{ | ||
"bid": { | ||
"id": "7706636740145184841", | ||
"impid": "test-imp-id", | ||
"price": 0.500000, | ||
"adm": "some-test-ad", | ||
"adomain": ["example.com"], | ||
"crid": "29681110", | ||
"h": 250, | ||
"w": 300, | ||
"mtype": 1 | ||
}, | ||
"type": "banner" | ||
}] | ||
}] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Use
%w
instead - see https://pkg.go.dev/fmt#ErrorfThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to
%w