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

Add AJA adapter #1269

Merged
merged 9 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
131 changes: 131 additions & 0 deletions adapters/aja/aja.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package aja

import (
"encoding/json"
"fmt"
"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
"net/http"
)

type AJAAdapter struct {
endpoint string
}

func (a *AJAAdapter) MakeRequests(bidReq *openrtb.BidRequest, extraInfo *adapters.ExtraRequestInfo) (adapterReqs []*adapters.RequestData, errs []error) {
// split imps by tagid
tagIDs := []string{}
impsByTagID := map[string][]openrtb.Imp{}
for _, imp := range bidReq.Imp {
extAJA, err := parseExtAJA(imp)
if err != nil {
errs = append(errs, err)
continue
}
imp.TagID = extAJA.AdSpotID
imp.Ext = nil
guscarreon marked this conversation as resolved.
Show resolved Hide resolved
if _, ok := impsByTagID[imp.TagID]; !ok {
tagIDs = append(tagIDs, imp.TagID)
}
impsByTagID[imp.TagID] = append(impsByTagID[imp.TagID], imp)
}

req := *bidReq
for _, tagID := range tagIDs {
req.Imp = impsByTagID[tagID]
body, err := json.Marshal(req)
guscarreon marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
errs = append(errs, err)
continue
}
adapterReqs = append(adapterReqs, &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: body,
})
}

return
}

func parseExtAJA(imp openrtb.Imp) (openrtb_ext.ExtImpAJA, error) {
var (
extImp adapters.ExtImpBidder
extAJA openrtb_ext.ExtImpAJA
)

if err := json.Unmarshal(imp.Ext, &extImp); err != nil {
return extAJA, &errortypes.BadInput{
Message: fmt.Sprintf("Failed to unmarshal ext impID: %s err: %s", imp.ID, err),
}
}

if err := json.Unmarshal(extImp.Bidder, &extAJA); err != nil {
return extAJA, &errortypes.BadInput{
Message: fmt.Sprintf("Failed to unmarshal ext.bidder impID: %s err: %s", imp.ID, err),
}
}

return extAJA, nil
}

func (a *AJAAdapter) MakeBids(bidReq *openrtb.BidRequest, adapterReq *adapters.RequestData, adapterResp *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if adapterResp.StatusCode != http.StatusOK {
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
if adapterResp.StatusCode == http.StatusNoContent {
return nil, nil
}
if adapterResp.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d", adapterResp.StatusCode),
}}
}
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d", adapterResp.StatusCode),
}}
}

var bidResp openrtb.BidResponse
if err := json.Unmarshal(adapterResp.Body, &bidResp); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Failed to unmarshal bid response: %s", err.Error()),
}}
}

bidderResp := adapters.NewBidderResponseWithBidsCapacity(len(bidReq.Imp))
var errors []error

for _, seatbid := range bidResp.SeatBid {
for _, bid := range seatbid.Bid {
impID:
for _, imp := range bidReq.Imp {
sambaiz marked this conversation as resolved.
Show resolved Hide resolved
if imp.ID == bid.ImpID {
var bidType openrtb_ext.BidType
if imp.Banner != nil {
bidType = openrtb_ext.BidTypeBanner
} else if imp.Video != nil {
bidType = openrtb_ext.BidTypeVideo
} else {
errors = append(errors, &errortypes.BadServerResponse{
Message: fmt.Sprintf("Response received for unexpected type of bid bidID: %s", bid.ID),
})
continue
}
bidderResp.Bids = append(bidderResp.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
})
break impID
}
}
}
}
return bidderResp, errors
}

func NewAJABidder(endpoint string) adapters.Bidder {
return &AJAAdapter{
endpoint: endpoint,
}
}
13 changes: 13 additions & 0 deletions adapters/aja/aja_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package aja

import (
"testing"

"github.com/prebid/prebid-server/adapters/adapterstest"
)

const testsBidderEndpoint = "https://localhost/bid/4"

func TestJsonSamples(t *testing.T) {
adapterstest.RunJSONBidderTest(t, "ajatest", NewAJABidder(testsBidderEndpoint))
}
159 changes: 159 additions & 0 deletions adapters/aja/ajatest/exemplary/banner-multiple-imps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{
"mockBidRequest": {
"id": "test-req-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"w": 300,
"h": 250
},
"ext": {
"bidder": {
"asi": "test-asi"
}
}
},
{
"id": "test-imp-id2",
"banner": {
"w": 300,
"h": 250
},
"ext": {
"bidder": {
"asi": "test-asi2"
}
}
}
],
"user": {
"buyeruid": "test-uid"
},
"tmax": 500
},

"httpcalls": [
{
"expectedRequest": {
"uri": "https://localhost/bid/4",
"headers": {},
"body": {
"id": "test-req-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"w": 300,
"h": 250
},
"tagid": "test-asi"
}
],
"user": {
"buyeruid": "test-uid"
},
"tmax": 500
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-req-id",
"seatbid": [
{
"bid": [
{
"id": "test-bid-id",
"impid": "test-imp-id",
"price": 1,
"adm": "<div>test</div>",
"crid": "test-creative-id"
}
]
}
],
"bidid": "test-seatbid-id",
"cur": "USD"
}
}
},
{
"expectedRequest": {
"uri": "https://localhost/bid/4",
"headers": {},
"body": {
"id": "test-req-id",
"imp": [
{
"id": "test-imp-id2",
"banner": {
"w": 300,
"h": 250
},
"tagid": "test-asi2"
}
],
"user": {
"buyeruid": "test-uid"
},
"tmax": 500
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-req-id",
"seatbid": [
{
"bid": [
{
"id": "test-bid-id2",
"impid": "test-imp-id2",
"price": 1,
"adm": "<div>test2</div>",
"crid": "test-creative-id2"
}
]
}
],
"bidid": "test-seatbid-id",
"cur": "USD"
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test-bid-id",
"impid": "test-imp-id",
"price": 1,
"adm": "<div>test</div>",
"crid": "test-creative-id"
},
"type": "banner"
}
]
},
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test-bid-id2",
"impid": "test-imp-id2",
"price": 1,
"adm": "<div>test2</div>",
"crid": "test-creative-id2"
},
"type": "banner"
}
]
}
]
}
90 changes: 90 additions & 0 deletions adapters/aja/ajatest/exemplary/video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"mockBidRequest": {
"id": "test-req-id",
"imp": [
{
"id": "test-imp-id",
"video": {
"mimes": ["video/mp4"],
"w": 640,
"h": 480
},
"ext": {
"bidder": {
"asi": "test-asi"
}
}
}
],
"user": {
"buyeruid": "test-uid"
},
"tmax": 500
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://localhost/bid/4",
"headers": {},
"body": {
"id": "test-req-id",
"imp": [
{
"id": "test-imp-id",
"video": {
"mimes": ["video/mp4"],
"w": 640,
"h": 480
},
"tagid": "test-asi"
}
],
"user": {
"buyeruid": "test-uid"
},
"tmax": 500
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-req-id",
"seatbid": [
{
"bid": [
{
"id": "test-bid-id",
"impid": "test-imp-id",
"price": 1,
"adm": "<VAST></VAST>",
"crid": "test-creative-id"
}
]
}
],
"bidid": "test-seatbid-id",
"cur": "USD"
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test-bid-id",
"impid": "test-imp-id",
"price": 1,
"adm": "<VAST></VAST>",
"crid": "test-creative-id"
},
"type": "video"
}
]
}
]
}
Loading