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 1 commit
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
121 changes: 121 additions & 0 deletions adapters/aax/aax.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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
}

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 _, sb := range bidResp.SeatBid {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: could you rename sb to be seatBid

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

for i := range sb.Bid {
bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, 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: &sb.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(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We discussed this PR with a team. There is one thing for you to consider. I case if imp has multi format this function may return incorrect bid type. We recommend to extract mediaType from response. Here is the example for reference:

func getBidType(bid openrtb2.Bid, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {

Copy link
Contributor

Choose a reason for hiding this comment

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

Other than this this PR loos good to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Want to second what @VeronikaSolovei9 stated above. This PR also looks good to me, but just want to bring up this update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, Thanks !

Copy link
Contributor

Choose a reason for hiding this comment

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

This is not really what is needed. You need to pass bid openrtb2.Bid and take media type from bidExtention.MediaType if you support this. If you modified this function to verify impression had multi format and throw error - then this should be done in MakeRequests function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted, Let me fix it.

Copy link
Contributor

@VeronikaSolovei9 VeronikaSolovei9 Apr 19, 2022

Choose a reason for hiding this comment

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

I'm confused, did you change it? I'm looking for something like this:


type aaxResponseBidExt struct {  // Aax specific bid ext if yu know the format and return bid type in bid.ext
	MediaType string `json:"mediaType"`
}

//pass bid itself to the function, should be seatBid.Bid[i] 
func getMediaTypeForImp(bid openrtb2.Bid, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
	var bidExt aaxResponseBidExt 
	err := json.Unmarshal(bid.Ext, &bidExt) //unmarshall bid ext to struct and extract bid type from it
	if err == nil {
		switch bidExt.MediaType {
		case "banner":
			return openrtb_ext.BidTypeBanner, nil
		case "native":
			return openrtb_ext.BidTypeNative, nil
		case "video":
			return openrtb_ext.BidTypeVideo, nil
		}
	}
        // if type was not returned till this moment - extract it from imp, you have this part already
	var mediaType openrtb_ext.BidType
	var typeCnt = 0
	for _, imp := range imps {
		if imp.ID == bid.ImpID { //bid has imp id in it, you don't need to pass it to function separately
			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)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No No, Have not pushed anything yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now I have pushed the changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh right, it looks good now, thank you for the update!

mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner == nil && imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
}
return mediaType, nil
}
}

return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find impression \"%s\" ", 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