-
Notifications
You must be signed in to change notification settings - Fork 753
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: TradPlus #3838
Closed
Closed
New Adapter: TradPlus #3838
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package tradplus | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
) | ||
|
||
func TestValidParams(t *testing.T) { | ||
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params") | ||
if err != nil { | ||
t.Fatalf("Failed to fetch the json-schemas. %v", err) | ||
} | ||
|
||
for _, validParam := range validParams { | ||
if err := validator.Validate(openrtb_ext.BidderTradPlus, json.RawMessage(validParam)); err != nil { | ||
t.Errorf("Schema rejected tradplus params: %s", validParam) | ||
} | ||
} | ||
} | ||
|
||
// TestInvalidParams makes sure that the tradplus schema rejects all the imp.ext fields we don't support. | ||
func TestInvalidParams(t *testing.T) { | ||
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params") | ||
if err != nil { | ||
t.Fatalf("Failed to fetch the json-schemas. %v", err) | ||
} | ||
|
||
for _, invalidParam := range invalidParams { | ||
if err := validator.Validate(openrtb_ext.BidderTradPlus, json.RawMessage(invalidParam)); err == nil { | ||
t.Errorf("Schema allowed unexpected params: %s", invalidParam) | ||
} | ||
} | ||
} | ||
|
||
var validParams = []string{ | ||
`{"accountId": "11233", "zoneId": ""}`, | ||
`{"accountId": "aaa", "accountId": "us"}`, | ||
`{"accountId": "aa", "accountId": "sin"}`, | ||
} | ||
|
||
var invalidParams = []string{ | ||
`{"accountId": ""}`, | ||
`{"accountId": "", "zoneId": ""}`, | ||
`{"accountId": "", "zoneId": "sin"}`, | ||
`{"zoneId": "aaa"}`, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package tradplus | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"text/template" | ||
|
||
"github.com/prebid/openrtb/v20/openrtb2" | ||
"github.com/prebid/prebid-server/v2/adapters" | ||
"github.com/prebid/prebid-server/v2/config" | ||
"github.com/prebid/prebid-server/v2/errortypes" | ||
"github.com/prebid/prebid-server/v2/macros" | ||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
) | ||
|
||
type adapter struct { | ||
endpoint *template.Template | ||
} | ||
|
||
// Builder builds a new instance of the tradplus adapter for the given bidder with the given config. | ||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { | ||
template, err := template.New("endpointTemplate").Parse(config.Endpoint) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse endpoint url template: %v", err) | ||
} | ||
|
||
return &adapter{ | ||
endpoint: template, | ||
}, nil | ||
} | ||
|
||
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
adapterRequest, errs := a.makeRequest(request) | ||
if errs != nil { | ||
return nil, errs | ||
} | ||
return []*adapters.RequestData{adapterRequest}, nil | ||
} | ||
|
||
func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, []error) { | ||
errs := validateTradPlusExt(request) | ||
if errs != nil { | ||
return nil, errs | ||
} | ||
|
||
tradplusExt, err := getImpressionExt(&request.Imp[0]) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
request.Imp[0].Ext = nil | ||
|
||
url, err := a.buildEndpointURL(tradplusExt) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
err = transform(request) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
reqBody, err := json.Marshal(request) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
return &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: url, | ||
Body: reqBody, | ||
Headers: headers, | ||
ImpIDs: openrtb_ext.GetImpIDs(request.Imp), | ||
}, nil | ||
} | ||
|
||
func getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtImpTradPlus, error) { | ||
var bidderExt adapters.ExtImpBidder | ||
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { | ||
return nil, &errortypes.BadInput{ | ||
Message: "Error parsing tradplusExt - " + err.Error(), | ||
} | ||
} | ||
|
||
var tradplusExt openrtb_ext.ExtImpTradPlus | ||
if err := json.Unmarshal(bidderExt.Bidder, &tradplusExt); err != nil { | ||
return nil, &errortypes.BadInput{ | ||
Message: "Error parsing bidderExt - " + err.Error(), | ||
} | ||
} | ||
|
||
return &tradplusExt, nil | ||
} | ||
|
||
func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtImpTradPlus) (string, error) { | ||
endpointParams := macros.EndpointTemplateParams{ | ||
AccountID: params.AccountID, | ||
ZoneID: params.ZoneID, | ||
} | ||
return macros.ResolveMacros(a.endpoint, endpointParams) | ||
} | ||
|
||
func transform(request *openrtb2.BidRequest) error { | ||
for i, imp := range request.Imp { | ||
if imp.Native != nil { | ||
var nativeRequest map[string]interface{} | ||
nativeCopyRequest := make(map[string]interface{}) | ||
if err := json.Unmarshal([]byte(request.Imp[i].Native.Request), &nativeRequest); err != nil { | ||
return err | ||
} | ||
_, exists := nativeRequest["native"] | ||
if exists { | ||
continue | ||
} | ||
nativeCopyRequest["native"] = nativeRequest | ||
nativeReqByte, err := json.Marshal(nativeCopyRequest) | ||
if err != nil { | ||
return err | ||
} | ||
nativeCopy := *request.Imp[i].Native | ||
nativeCopy.Request = string(nativeReqByte) | ||
request.Imp[i].Native = &nativeCopy | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func validateTradPlusExt(request *openrtb2.BidRequest) []error { | ||
var extImpTradPlus openrtb_ext.ExtImpTradPlus | ||
var errs []error | ||
for _, imp := range request.Imp { | ||
var extBidder adapters.ExtImpBidder | ||
err := json.Unmarshal(imp.Ext, &extBidder) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
err = json.Unmarshal(extBidder.Bidder, &extImpTradPlus) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
if extImpTradPlus.AccountID == "" { | ||
errs = append(errs, fmt.Errorf("imp.ext.accountId required")) | ||
continue | ||
} | ||
} | ||
return errs | ||
} | ||
|
||
// MakeBids make the bids for the bid response. | ||
func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
if adapters.IsResponseStatusCodeNoContent(response) { | ||
return nil, nil | ||
} | ||
if err := adapters.CheckResponseStatusCodeForErrors(response); err != nil { | ||
return nil, []error{&errortypes.BadInput{ | ||
Message: fmt.Sprintf("Unexpected status code: %d.", response.StatusCode), | ||
}} | ||
} | ||
var bidResp openrtb2.BidResponse | ||
if err := json.Unmarshal(response.Body, &bidResp); err != nil { | ||
return nil, []error{err} | ||
} | ||
var errs []error | ||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(internalRequest.Imp)) | ||
for _, sb := range bidResp.SeatBid { | ||
for i := range sb.Bid { | ||
mediaType, err := getMediaTypeForBid(sb.Bid[i]) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
Bid: &sb.Bid[i], | ||
BidType: mediaType, | ||
}) | ||
} | ||
} | ||
return bidResponse, errs | ||
} | ||
|
||
func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) { | ||
switch bid.MType { | ||
case openrtb2.MarkupBanner: | ||
return openrtb_ext.BidTypeBanner, nil | ||
case openrtb2.MarkupNative: | ||
return openrtb_ext.BidTypeNative, nil | ||
case openrtb2.MarkupVideo: | ||
return openrtb_ext.BidTypeVideo, nil | ||
default: | ||
return "", fmt.Errorf("unrecognized bid type in response from tradplus for bid %s", bid.ImpID) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tradplus | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/prebid/prebid-server/v2/adapters/adapterstest" | ||
"github.com/prebid/prebid-server/v2/config" | ||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
bidder, buildErr := Builder(openrtb_ext.BidderTradPlus, config.Adapter{ | ||
Endpoint: "https://{{.ZoneID}}adx.tradplusad.com/{{.AccountID}}/pserver"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||
|
||
if buildErr != nil { | ||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
} | ||
|
||
adapterstest.RunJSONBidderTest(t, "tradplustest", bidder) | ||
} | ||
|
||
func TestEndpointTemplateMalformed(t *testing.T) { | ||
_, buildErr := Builder(openrtb_ext.BidderTradPlus, config.Adapter{ | ||
Endpoint: "{{Malformed}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||
|
||
assert.Error(t, buildErr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 50 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"accountId": "fake-account-id", | ||
"zoneId": "" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "https://adx.tradplusad.com/fake-account-id/pserver", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 50 | ||
} | ||
] | ||
} | ||
} | ||
] | ||
}, | ||
"impIDs":["test-imp-id"] | ||
}, | ||
"mockResponse": { | ||
"status": 204, | ||
"body": {} | ||
} | ||
} | ||
], | ||
"expectedBidResponses": [] | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@tradplus Can you please confirm if you want to use ext of first impression even in case of multiple imps? If that is the case then
validateTradPlusExt()
function can be only used for first imp rather than looping over all the imps or validation for accountId can be done ingetImpressionExt()
function itself and return error fromgetImpressionExt()
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.
the
validateTradPlusExt
method has been deleted.