Skip to content

Commit

Permalink
New Adapter: Adyoulike (#1700)
Browse files Browse the repository at this point in the history
Co-authored-by: Damien Dumas <damien.dumas@adyoulike.com>
  • Loading branch information
guiann and Damien Dumas authored Mar 7, 2021
1 parent a3cc810 commit c15c66c
Show file tree
Hide file tree
Showing 21 changed files with 842 additions and 0 deletions.
137 changes: 137 additions & 0 deletions adapters/adyoulike/adyoulike.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package adyoulike

import (
"encoding/json"
"errors"
"fmt"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"
"net/http"

"github.com/buger/jsonparser"
"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
)

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

type adapter struct {
endpoint string
}

func (a *adapter) MakeRequests(
openRTBRequest *openrtb.BidRequest,
reqInfo *adapters.ExtraRequestInfo,
) (
requestsToBidder []*adapters.RequestData,
errs []error,
) {
var err error
var tagID string

reqCopy := *openRTBRequest
reqCopy.Imp = []openrtb.Imp{}
for ind, imp := range openRTBRequest.Imp {
reqCopy.Imp = append(reqCopy.Imp, imp)

tagID, err = jsonparser.GetString(reqCopy.Imp[ind].Ext, "bidder", "placement")
if err != nil {
errs = append(errs, err)
continue
}

reqCopy.Imp[ind].TagID = tagID
}

openRTBRequestJSON, err := json.Marshal(reqCopy)
if err != nil {
errs = append(errs, err)
}

if len(errs) > 0 {
return nil, errs
}

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
headers.Add("x-openrtb-version", "2.5")

requestToBidder := &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: openRTBRequestJSON,
Headers: headers,
}
requestsToBidder = append(requestsToBidder, requestToBidder)

return requestsToBidder, errs
}

const unexpectedStatusCodeFormat = "" +
"Unexpected status code: %d. Run with request.debug = 1 for more info"

func (a *adapter) MakeBids(
openRTBRequest *openrtb.BidRequest,
requestToBidder *adapters.RequestData,
bidderRawResponse *adapters.ResponseData,
) (
bidderResponse *adapters.BidderResponse,
errs []error,
) {
switch bidderRawResponse.StatusCode {
case http.StatusOK:
break
case http.StatusNoContent:
return nil, []error{errors.New("MakeBids error: No Content")}
case http.StatusBadRequest:
err := &errortypes.BadInput{
Message: fmt.Sprintf(unexpectedStatusCodeFormat, bidderRawResponse.StatusCode),
}
return nil, []error{err}
default:
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf(unexpectedStatusCodeFormat, bidderRawResponse.StatusCode),
}
return nil, []error{err}
}

var openRTBBidderResponse openrtb.BidResponse
if err := json.Unmarshal(bidderRawResponse.Body, &openRTBBidderResponse); err != nil {
return nil, []error{err}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(openRTBRequest.Imp))
bidResponse.Currency = openRTBBidderResponse.Cur
for _, seatBid := range openRTBBidderResponse.SeatBid {
for idx := range seatBid.Bid {
b := &adapters.TypedBid{
Bid: &seatBid.Bid[idx],
BidType: getMediaTypeForImp(seatBid.Bid[idx].ImpID, openRTBRequest.Imp),
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
return bidResponse, nil
}

// getMediaTypeForBid determines which type of bid.
func getMediaTypeForImp(impID string, imps []openrtb.Imp) openrtb_ext.BidType {
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner == nil && imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
} else if imp.Banner == nil && imp.Native != nil {
mediaType = openrtb_ext.BidTypeNative
}
}
}

return mediaType
}
22 changes: 22 additions & 0 deletions adapters/adyoulike/adyoulike_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package adyoulike

import (
"testing"

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

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderAdyoulike, config.Adapter{
Endpoint: testsBidderEndpoint})

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

adapterstest.RunJSONBidderTest(t, "adyouliketest", bidder)
}
169 changes: 169 additions & 0 deletions adapters/adyoulike/adyouliketest/exemplary/multiformat-impression.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "banner-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"bidder": {
"placement": "12345"
}
}
},
{
"id": "video-imp-id",
"video": {
"w": 640,
"h": 480
},
"ext": {
"bidder": {
"placement": "54321"
}
}
},
{
"id": "native-imp-id",
"native": {
"title": "required"
},
"ext": {
"bidder": {
"placement": "123123"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://localhost/bid/4",
"body": {
"id": "test-request-id",
"imp": [
{
"banner": {
"format": [
{
"h": 250,
"w": 300
}
]
},
"ext": {
"bidder": {
"placement": "12345"
}
},
"id": "banner-imp-id",
"tagid": "12345"
},
{
"ext": {
"bidder": {
"placement": "54321"
}
},
"id": "video-imp-id",
"tagid": "54321",
"video": {
"h": 480,
"mimes": null,
"w": 640
}
},
{
"ext": {
"bidder": {
"placement": "123123"
}
},
"id": "native-imp-id",
"native": {
"request": ""
},
"tagid": "123123"
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-response-id",
"seatbid": [{
"seat": "1",
"bid": [{
"id": "12340",
"impid": "banner-imp-id",
"price": 300,
"adm": "%3C%3Fxml%20version%3D%221.0%22%20encod%2Fhtml%3E",
"nurl": "http://example.com/winnoticeurl0"
},
{
"id": "12341",
"impid": "video-imp-id",
"price": 301,
"adm": "%3C%3Fxml%20version%3D%221.0%22%20encod%2FVAST%3E",
"nurl": "http://example.com/winnoticeurl1"
},
{
"id": "12342",
"impid": "native-imp-id",
"price": 302,
"adm": "{'json':'response','for':'native'}",
"nurl": "http://example.com/winnoticeurl2"
}
]
}]
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "12340",
"impid": "banner-imp-id",
"price": 300,
"nurl": "http://example.com/winnoticeurl0",
"adm": "%3C%3Fxml%20version%3D%221.0%22%20encod%2Fhtml%3E"
},
"type": "banner"
},
{
"bid": {
"id": "12341",
"impid": "video-imp-id",
"price": 301,
"nurl": "http://example.com/winnoticeurl1",
"adm": "%3C%3Fxml%20version%3D%221.0%22%20encod%2FVAST%3E"
},
"type": "video"
},
{
"bid": {
"id": "12342",
"impid": "native-imp-id",
"price": 302,
"nurl": "http://example.com/winnoticeurl2",
"adm": "{'json':'response','for':'native'}"
},
"type": "native"
}
]
}
]
}
3 changes: 3 additions & 0 deletions adapters/adyoulike/adyouliketest/params/race/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"placement": "19f1b372c7548ec1fe734d2c9f8dc688"
}
3 changes: 3 additions & 0 deletions adapters/adyoulike/adyouliketest/params/race/video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"placement": "19f1b372c7548ec1fe734d2c9f8dc688"
}
Loading

0 comments on commit c15c66c

Please sign in to comment.