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: Revcontent #1622

Merged
merged 14 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
99 changes: 99 additions & 0 deletions adapters/revcontent/revcontent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package revcontent

import (
"encoding/json"
"fmt"
"github.com/mxmCherry/openrtb"
"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"
"net/http"
)

type Adapter struct {
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
endpoint string
}

// Builder builds a new instance of the Revcontent adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &Adapter{
endpoint: config.Endpoint,
}
return bidder, nil
}

func (a *Adapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
reqBody, err := json.Marshal(request)

if err != nil {
return nil, []error{err}
}

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

req := &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: reqBody,
Headers: headers,
}
return []*adapters.RequestData{req}, nil
}

// MakeBids make the bids for the bid response.
func (a *Adapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []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.", response.StatusCode),
}}
}

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

var bidResp openrtb.BidResponse

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

bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)

for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
var mediaType = getBidType(sb.Bid[i].AdM)
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: mediaType,
})
}
}
return bidResponse, nil

}

func getBidCount(bidResponse openrtb.BidResponse) int {
jcamp-revc marked this conversation as resolved.
Show resolved Hide resolved
c := 0
for _, sb := range bidResponse.SeatBid {
c = c + len(sb.Bid)
}
return c
}

func getBidType(bidAdm string) openrtb_ext.BidType {
// native: {"ver":"1.1","assets":...
// banner: <div id='rtb-widget...
if bidAdm != "" && bidAdm[:1] == "<" {
return openrtb_ext.BidTypeBanner
}
return openrtb_ext.BidTypeNative
jcamp-revc marked this conversation as resolved.
Show resolved Hide resolved
}
21 changes: 21 additions & 0 deletions adapters/revcontent/revcontent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package revcontent

import (
"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.BidderRevcontent, config.Adapter{
Endpoint: "https://trends.revcontent.com/rtb?userId=1234&apiKey=abcd",
})

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

adapterstest.RunJSONBidderTest(t, "revcontenttest", bidder)
}
39 changes: 39 additions & 0 deletions adapters/revcontent/revcontenttest/exemplary/no-bid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [{"w": 300, "h": 50}]
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://trends.revcontent.com/rtb?userId=1234&apiKey=abcd",
"body": {
"id": "test-request-id",
"imp": [
{
"id":"test-imp-id",
"banner": {
"format": [{"w": 300, "h": 50}]
}
}
]
}
},
"mockResponse": {
"status": 204,
"body": {}
}
}
],

"expectedBidResponses": []

}
85 changes: 85 additions & 0 deletions adapters/revcontent/revcontenttest/exemplary/simple-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [{"w": 300, "h": 50}]
}
}
],
"site": {
"id": "example",
"domain": "example.com",
"page": "example.com",
"publisher": {
"id": "example"
}
}
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://trends.revcontent.com/rtb?userId=1234&apiKey=abcd",
"body": {
"id": "test-request-id",
"imp": [
{
"id":"test-imp-id",
"banner": {
"format": [{"w": 300, "h": 50}]
}
}
],
"site": {
"id": "example",
"domain": "example.com",
"page": "example.com",
"publisher": {
"id": "example"
}
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "ttx",
"bid": [{
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 1.2,
"adm": "<div></div>",
"crid": "crid_testid"
}]
}
],
"cur": "USD"
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 1.2,
"adm": "<div></div>",
"crid": "crid_testid"
},
"type": "banner"
}
]
}
]
}
70 changes: 70 additions & 0 deletions adapters/revcontent/revcontenttest/exemplary/simple-native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"native": {
"request": "{\"ver\":\"1.2\",\"context\":1,\"plcmttype\":4,\"plcmtcnt\":1,\"assets\":[{\"id\":2,\"required\":1,\"title\":{\"len\":90}},{\"id\":6,\"required\":1,\"img\":{\"type\":3,\"wmin\":128,\"hmin\":128,\"mimes\":[\"image/jpg\",\"image/jpeg\",\"image/png\"]}},{\"id\":7,\"required\":1,\"data\":{\"type\":2,\"len\":120}}]}",
"ver": "1.2"
}
}
]
},
"httpcalls": [
{
"expectedRequest": {
"uri": "https://trends.revcontent.com/rtb?userId=1234&apiKey=abcd",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"native": {
"request": "{\"ver\":\"1.2\",\"context\":1,\"plcmttype\":4,\"plcmtcnt\":1,\"assets\":[{\"id\":2,\"required\":1,\"title\":{\"len\":90}},{\"id\":6,\"required\":1,\"img\":{\"type\":3,\"wmin\":128,\"hmin\":128,\"mimes\":[\"image/jpg\",\"image/jpeg\",\"image/png\"]}},{\"id\":7,\"required\":1,\"data\":{\"type\":2,\"len\":120}}]}",
"ver": "1.2"
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id": "8400d766-58b3-47d4-80d7-6658b337d403",
"impid": "test-imp-id",
"price": 1.2,
"adm": "{\\\"ver\\\":\\\"1.1\\\",\\\"assets\\\":[]}",
"crid": "crid_testid"
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "8400d766-58b3-47d4-80d7-6658b337d403",
"impid": "test-imp-id",
"price": 1.2,
"adm": "{\\\"ver\\\":\\\"1.1\\\",\\\"assets\\\":[]}",
"crid": "crid_testid"

},
"type": "native"
}
]
}
]
}
3 changes: 3 additions & 0 deletions adapters/revcontent/revcontenttest/params/race/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
}
jcamp-revc marked this conversation as resolved.
Show resolved Hide resolved

3 changes: 3 additions & 0 deletions adapters/revcontent/revcontenttest/params/race/native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
}

43 changes: 43 additions & 0 deletions adapters/revcontent/revcontenttest/supplemental/bad_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [{"w": 300, "h": 50}]
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://trends.revcontent.com/rtb?userId=1234&apiKey=abcd",
"body": {
"id": "test-request-id",
"imp": [
{
"id":"test-imp-id",
"banner": {
"format": [{"w": 300, "h": 50}]
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": "{\"id\":test-request-id"
}
}
],

"expectedMakeBidsErrors": [
{
"comparison": "literal",
"value": "json: cannot unmarshal string into Go value of type openrtb.BidResponse"
}
]
}
Loading