Skip to content

Commit

Permalink
New Adapter: Revcontent (prebid#1622)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcamp-revc authored and shunj-nb committed Nov 8, 2022
1 parent adb58ed commit 7258f20
Show file tree
Hide file tree
Showing 16 changed files with 553 additions and 1 deletion.
105 changes: 105 additions & 0 deletions adapters/revcontent/revcontent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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 {
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}
}

if err := checkRequest(request); 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
}

func checkRequest(request *openrtb.BidRequest) error {
if (request.App == nil || len(request.App.Name) == 0) && (request.Site == nil || len(request.Site.Domain) == 0) {
return &errortypes.BadInput{
Message: "Impression is missing app name or site domain, and must contain one.",
}
}

return 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 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
}
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)
}
45 changes: 45 additions & 0 deletions adapters/revcontent/revcontenttest/exemplary/no-bid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [{"w": 300, "h": 50}]
}
}
],
"site": {
"domain": "test.com"
}
},

"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": {
"domain": "test.com"
}
}
},
"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"
}
]
}
]
}
76 changes: 76 additions & 0 deletions adapters/revcontent/revcontenttest/exemplary/simple-native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"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"
}
}
],
"site": {
"domain": "test.com"
}
},
"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"
}
}
],
"site": {
"domain": "test.com"
}
}
},
"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"
}
]
}
]
}
49 changes: 49 additions & 0 deletions adapters/revcontent/revcontenttest/supplemental/bad_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [{"w": 300, "h": 50}]
}
}
],
"site": {
"domain": "test.com"
}
},

"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": {
"domain": "test.com"
}
}
},
"mockResponse": {
"status": 200,
"body": "{\"id\":test-request-id"
}
}
],

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

0 comments on commit 7258f20

Please sign in to comment.