Skip to content
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 6 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions adapters/aax/aax.go
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)
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, Thanks.

} 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()
}
30 changes: 30 additions & 0 deletions adapters/aax/aax_test.go
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)
}
80 changes: 80 additions & 0 deletions adapters/aax/aaxtest/exemplary/multi-format.json
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": []
}
129 changes: 129 additions & 0 deletions adapters/aax/aaxtest/exemplary/multi-imps.json
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"
}
]
}
]
}
Loading