Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
Add AJA adapter (prebid#1269)
Browse files Browse the repository at this point in the history
  • Loading branch information
sambaiz authored Apr 30, 2020
1 parent 2699bbb commit 31157b4
Show file tree
Hide file tree
Showing 22 changed files with 811 additions and 0 deletions.
132 changes: 132 additions & 0 deletions adapters/aja/aja.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
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
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)
if err != nil {
errs = append(errs, &errortypes.BadInput{
Message: fmt.Sprintf("Failed to unmarshal bidrequest ID: %s err: %s", bidReq.ID, 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 {
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 {
for _, imp := range bidReq.Imp {
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
}
}
}
}
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

0 comments on commit 31157b4

Please sign in to comment.