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: Axonix #1912

Merged
merged 5 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
135 changes: 135 additions & 0 deletions adapters/axonix/axonix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package axonix

import (
"encoding/json"
"fmt"
"net/http"

"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 {
URI string
}

func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &adapter{
URI: config.Endpoint,
}
return bidder, nil
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
errs := make([]error, 0, len(request.Imp))
if len(request.Imp) == 0 {
err := &errortypes.BadInput{
Message: "No impressions in the bid request",
}
errs = append(errs, err)
return nil, errs
}
guscarreon marked this conversation as resolved.
Show resolved Hide resolved

requestJSON, err := json.Marshal(request)
if err != nil {
return nil, []error{err}
}

errors := make([]error, 0, 1)
guscarreon marked this conversation as resolved.
Show resolved Hide resolved

var bidderExt adapters.ExtImpBidder
err = json.Unmarshal(request.Imp[0].Ext, &bidderExt)

if err != nil {
err = &errortypes.BadInput{
Message: "ext.bidder not provided",
}
errors = append(errors, err)
return nil, errors
}

var axonixExt openrtb_ext.ExtImpAxonix
err = json.Unmarshal(bidderExt.Bidder, &axonixExt)
if err != nil {
err = &errortypes.BadInput{
Message: "ext.bidder.supplyId not provided",
guscarreon marked this conversation as resolved.
Show resolved Hide resolved
}
errors = append(errors, err)
return nil, errors
}

if axonixExt.SupplyId == "" {
err = &errortypes.BadInput{
Message: "supplyId is empty",
}
errors = append(errors, err)
return nil, errors
}
guscarreon marked this conversation as resolved.
Show resolved Hide resolved

thisURI := a.URI
if len(thisURI) == 0 {
thisURI = "https://openrtb-us-east-1.axonix.com/supply/prebid-server/" + axonixExt.SupplyId
guscarreon marked this conversation as resolved.
Show resolved Hide resolved
}

headers := http.Header{}
headers.Add("Content-Type", "application/json")

requestData := &adapters.RequestData{
Method: "POST",
Uri: thisURI,
Body: requestJSON,
Headers: headers,
}

return []*adapters.RequestData{requestData}, nil
}

func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if responseData.StatusCode == http.StatusNoContent {
return nil, nil
}

if responseData.StatusCode != http.StatusOK {
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d.", responseData.StatusCode),
}
return nil, []error{err}
}

var response openrtb2.BidResponse
if err := json.Unmarshal(responseData.Body, &response); err != nil {
return nil, []error{err}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
bidResponse.Currency = response.Cur
for _, seatBid := range response.SeatBid {
for _, bid := range seatBid.Bid {
bid := bid
b := &adapters.TypedBid{
Bid: &bid,
BidType: getMediaType(bid.ImpID, request.Imp),
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}

return bidResponse, nil
}

func getMediaType(impId string, imps []openrtb2.Imp) openrtb_ext.BidType {
hhhjort marked this conversation as resolved.
Show resolved Hide resolved
for _, imp := range imps {
if imp.ID == impId {
if imp.Native != nil {
return openrtb_ext.BidTypeNative
} else if imp.Video != nil {
return openrtb_ext.BidTypeVideo
}
return openrtb_ext.BidTypeBanner
}
}
return openrtb_ext.BidTypeBanner
}
30 changes: 30 additions & 0 deletions adapters/axonix/axonix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package axonix

import (
"testing"

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

func TestJsonSamplesWithConfiguredURI(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The /exemplary directory is intended to hold just a few reference examples a publisher can look at for a reference as to how to use your adapter. Additional tests to catch corner cases where there are no errors go into /supplemental. Are you sure all your positive tests belong in /exemplary? too many examples may confuse publishers as to what they should try to follow.

bidder, buildErr := Builder(openrtb_ext.BidderAxonix, config.Adapter{
Endpoint: "https://openrtb-us-east-1.axonix.com/supply/prebid-server/24cc9034-f861-47b8-a6a8-b7e0968c00b8"})

if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}

adapterstest.RunJSONBidderTest(t, "axonixtest", bidder)
}

func TestJsonSamplesWithHardcodedURI(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderAxonix, config.Adapter{})

if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}

adapterstest.RunJSONBidderTest(t, "axonixtest", bidder)
}
133 changes: 133 additions & 0 deletions adapters/axonix/axonixtest/exemplary/banner-and-video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"supplyId": "24cc9034-f861-47b8-a6a8-b7e0968c00b8"
}
}
},
{
"id": "test-imp-video-id",
"video": {
"mimes": ["video/mp4"],
"protocols": [2, 5],
"w": 1024,
"h": 576
},
"ext": {
"bidder": {
"supplyId": "24cc9034-f861-47b8-a6a8-b7e0968c00b8"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://openrtb-us-east-1.axonix.com/supply/prebid-server/24cc9034-f861-47b8-a6a8-b7e0968c00b8",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"supplyId": "24cc9034-f861-47b8-a6a8-b7e0968c00b8"
}
}
},
{
"id": "test-imp-video-id",
"video": {
"mimes": ["video/mp4"],
"protocols": [2, 5],
"w": 1024,
"h": 576
},
"ext": {
"bidder": {
"supplyId": "24cc9034-f861-47b8-a6a8-b7e0968c00b8"
}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "958",
"bid": [{
"id": "7706636740145184841",
"impid": "test-imp-video-id",
"price": 0.500000,
"adid": "29681110",
"adm": "some-test-ad",
"adomain": ["axonix.com"],
"cid": "958",
"crid": "29681110",
"h": 576,
"w": 1024
}]
}
],
"bidid": "5778926625248726496",
"cur": "USD"
}
}
}
],

"expectedBidResponses": [
{
"bids": [{
"bid": {
"id": "7706636740145184841",
"impid": "test-imp-video-id",
"price": 0.5,
"adm": "some-test-ad",
"adid": "29681110",
"adomain": ["axonix.com"],
"cid": "958",
"crid": "29681110",
"w": 1024,
"h": 576
},
"type": "video"
}]
}
]

}
Loading