-
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: Aax #2219
Merged
Merged
New Adapter: Aax #2219
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
57f2265
Adding Aax Adapter
product-aax 325b7b6
Adding test case for invalid imp id
product-aax d4bc060
changing muti format adtype logic
product-aax 6a06759
Resolving PR Comment
product-aax 05828be
Merge branch 'master' into adding-aax-adapter
product-aax a54803e
Updating userMacro
product-aax 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,147 @@ | ||
package aax | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/mxmCherry/openrtb/v15/openrtb2" | ||
"github.com/prebid/prebid-server/adapters" | ||
"github.com/prebid/prebid-server/config" | ||
"github.com/prebid/prebid-server/errortypes" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
) | ||
|
||
type adapter struct { | ||
endpoint string | ||
} | ||
|
||
type aaxResponseBidExt struct { | ||
AdCodeType string `json:"adCodeType"` | ||
} | ||
|
||
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
var errs []error | ||
|
||
reqJson, err := json.Marshal(request) | ||
if err != nil { | ||
errs = append(errs, err) | ||
return nil, errs | ||
} | ||
|
||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
|
||
return []*adapters.RequestData{{ | ||
Method: "POST", | ||
Uri: a.endpoint, | ||
Body: reqJson, | ||
Headers: headers, | ||
}}, errs | ||
} | ||
|
||
func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
var errs []error | ||
|
||
if response.StatusCode == http.StatusNoContent { | ||
return nil, nil | ||
} | ||
|
||
if response.StatusCode == http.StatusBadRequest { | ||
return nil, []error{&errortypes.BadInput{ | ||
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode), | ||
}} | ||
} | ||
|
||
if response.StatusCode != http.StatusOK { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode), | ||
}} | ||
} | ||
|
||
var bidResp openrtb2.BidResponse | ||
|
||
if err := json.Unmarshal(response.Body, &bidResp); err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
bidResponse := adapters.NewBidderResponse() | ||
|
||
for _, seatBid := range bidResp.SeatBid { | ||
for i := range seatBid.Bid { | ||
bidType, err := getMediaTypeForImp(seatBid.Bid[i], internalRequest.Imp) | ||
if err != nil { | ||
errs = append(errs, err) | ||
} else { | ||
b := &adapters.TypedBid{ | ||
Bid: &seatBid.Bid[i], | ||
BidType: bidType, | ||
} | ||
bidResponse.Bids = append(bidResponse.Bids, b) | ||
} | ||
} | ||
} | ||
return bidResponse, errs | ||
} | ||
|
||
// Builder builds a new instance of the Aax adapter for the given bidder with the given config. | ||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) { | ||
url := buildEndpoint(config.Endpoint, config.ExtraAdapterInfo) | ||
return &adapter{ | ||
endpoint: url, | ||
}, nil | ||
} | ||
|
||
func getMediaTypeForImp(bid openrtb2.Bid, imps []openrtb2.Imp) (openrtb_ext.BidType, error) { | ||
var bidExt aaxResponseBidExt | ||
err := json.Unmarshal(bid.Ext, &bidExt) | ||
if err == nil { | ||
switch bidExt.AdCodeType { | ||
case "banner": | ||
return openrtb_ext.BidTypeBanner, nil | ||
case "native": | ||
return openrtb_ext.BidTypeNative, nil | ||
case "video": | ||
return openrtb_ext.BidTypeVideo, nil | ||
} | ||
} | ||
|
||
var mediaType openrtb_ext.BidType | ||
var typeCnt = 0 | ||
for _, imp := range imps { | ||
if imp.ID == bid.ImpID { | ||
if imp.Banner != nil { | ||
typeCnt += 1 | ||
mediaType = openrtb_ext.BidTypeBanner | ||
} | ||
if imp.Native != nil { | ||
typeCnt += 1 | ||
mediaType = openrtb_ext.BidTypeNative | ||
} | ||
if imp.Video != nil { | ||
typeCnt += 1 | ||
mediaType = openrtb_ext.BidTypeVideo | ||
} | ||
} | ||
} | ||
if typeCnt == 1 { | ||
return mediaType, nil | ||
} | ||
return mediaType, fmt.Errorf("unable to fetch mediaType in multi-format: %s", bid.ImpID) | ||
} | ||
|
||
func buildEndpoint(aaxUrl, hostUrl string) string { | ||
|
||
if len(hostUrl) == 0 { | ||
return aaxUrl | ||
} | ||
urlObject, err := url.Parse(aaxUrl) | ||
if err != nil { | ||
return aaxUrl | ||
} | ||
values := urlObject.Query() | ||
values.Add("src", hostUrl) | ||
urlObject.RawQuery = values.Encode() | ||
return urlObject.String() | ||
} |
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,30 @@ | ||
package aax | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/adapters/adapterstest" | ||
"github.com/prebid/prebid-server/config" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
bidder, buildErr := Builder(openrtb_ext.BidderAax, config.Adapter{ | ||
Endpoint: "https://example.aax.media/rtb/prebid", | ||
ExtraAdapterInfo: "http://localhost:8080/extrnal_url", | ||
}) | ||
|
||
if buildErr != nil { | ||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
} | ||
|
||
adapterstest.RunJSONBidderTest(t, "aaxtest", bidder) | ||
} | ||
|
||
func TestEndpointTemplateMalformed(t *testing.T) { | ||
_, buildErr := Builder(openrtb_ext.BidderAax, config.Adapter{ | ||
Endpoint: "{{Malformed}}"}) | ||
|
||
assert.Nil(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,80 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "1", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 320, | ||
"h": 50 | ||
} | ||
] | ||
}, | ||
"video": { | ||
"mimes": [ | ||
"video/mp4" | ||
], | ||
"protocols": [ | ||
2, | ||
5 | ||
], | ||
"w": 320, | ||
"h": 480 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"cid": "TCID", | ||
"crid": "1234" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "https://example.aax.media/rtb/prebid?src=http%3A%2F%2Flocalhost%3A8080%2Fextrnal_url", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "1", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 320, | ||
"h": 50 | ||
} | ||
] | ||
}, | ||
"video": { | ||
"mimes": [ | ||
"video/mp4" | ||
], | ||
"protocols": [ | ||
2, | ||
5 | ||
], | ||
"w": 320, | ||
"h": 480 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"cid": "TCID", | ||
"crid": "1234" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 204, | ||
"body": "" | ||
} | ||
} | ||
], | ||
"expectedBidResponses": [] | ||
} |
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,129 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "1", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 320, | ||
"h": 50 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"cid": "TCID", | ||
"crid": "1234" | ||
} | ||
} | ||
}, | ||
{ | ||
"id": "2", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 50 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"cid": "TCID", | ||
"crid": "1234" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "https://example.aax.media/rtb/prebid?src=http%3A%2F%2Flocalhost%3A8080%2Fextrnal_url", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "1", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 320, | ||
"h": 50 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"cid": "TCID", | ||
"crid": "1234" | ||
} | ||
} | ||
}, | ||
{ | ||
"id": "2", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 50 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"cid": "TCID", | ||
"crid": "1234" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"seatbid": [ | ||
{ | ||
"seat": "aax", | ||
"bid": [ | ||
{ | ||
"id": "test-bid-id", | ||
"impid": "1", | ||
"price": 1.50, | ||
"adm": "some-test-ad", | ||
"crid": "test-crid", | ||
"h": 50, | ||
"w": 320 | ||
} | ||
] | ||
} | ||
], | ||
"cur": "USD" | ||
} | ||
} | ||
} | ||
], | ||
"expectedBidResponses": [ | ||
{ | ||
"currency": "USD", | ||
"bids": [ | ||
{ | ||
"bid": { | ||
"id": "test-bid-id", | ||
"impid": "1", | ||
"price": 1.50, | ||
"adm": "some-test-ad", | ||
"crid": "test-crid", | ||
"w": 320, | ||
"h": 50 | ||
}, | ||
"type": "banner" | ||
} | ||
] | ||
} | ||
] | ||
} |
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.
Could you add a test case for this error
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.
Added, Thanks.