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: Kobler #3904

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
139 changes: 139 additions & 0 deletions adapters/kobler/kobler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package kobler

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

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/errortypes"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)

type adapter struct {
endpoint string
devEndpoint string
}

const (
DEV_BIDDER_ENDPOINT = "https://bid-service.dev.essrtb.com/bid/prebid_server_rtb_call"
SUPPORTED_CURRENCY = "USD"
)

func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
bidder := &adapter{
endpoint: config.Endpoint,
devEndpoint: DEV_BIDDER_ENDPOINT,
}

return bidder, nil
}

func (a adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) (requestData []*adapters.RequestData, errors []error) {
if !contains(request.Cur, SUPPORTED_CURRENCY) {
request.Cur = append(request.Cur, SUPPORTED_CURRENCY)
}

for i := range request.Imp {
if err := convertImpCurrency(&request.Imp[i], reqInfo); err != nil {
errors = append(errors, err)
return
}
}

requestJSON, err := json.Marshal(request)
if err != nil {
errors = append(errors, err)
return
}

requestData = append(requestData, &adapters.RequestData{
Method: "POST",
Uri: a.getEndpoint(request),
Body: requestJSON,
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
})

return
}

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

if responseData.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode),
}}
}

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 i, bid := range seatBid.Bid {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: getMediaTypeForBid(bid),
})
}
}

return bidResponse, nil
}

func (a adapter) getEndpoint(request *openrtb2.BidRequest) string {
if request.Test == 1 {
return a.devEndpoint
}

return a.endpoint
}

func getMediaTypeForBid(bid openrtb2.Bid) openrtb_ext.BidType {
if bid.Ext != nil {
var bidExt openrtb_ext.ExtBid
err := json.Unmarshal(bid.Ext, &bidExt)
if err == nil && bidExt.Prebid != nil {
mediaType, err := openrtb_ext.ParseBidType(string(bidExt.Prebid.Type))
Copy link

Choose a reason for hiding this comment

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

Consider this as a suggestion. Prebid server expects the media type to be explicitly set in the adapter response. Therefore, recommends implementing a pattern where the adapter server sets the MType field in the response to accurately determine the media type for the impression.

if err == nil {
return mediaType
}
}
}

return openrtb_ext.BidTypeBanner
}

func convertImpCurrency(imp *openrtb2.Imp, reqInfo *adapters.ExtraRequestInfo) error {
if imp.BidFloor > 0 && imp.BidFloorCur != "" && strings.ToUpper(imp.BidFloorCur) != SUPPORTED_CURRENCY {
convertedValue, err := reqInfo.ConvertCurrency(imp.BidFloor, imp.BidFloorCur, SUPPORTED_CURRENCY)
if err != nil {
return err
}

imp.BidFloor = convertedValue
imp.BidFloorCur = SUPPORTED_CURRENCY
}

return nil
}

func contains[T comparable](array []T, value T) bool {
for _, item := range array {
if item == value {
return true
}
}

return false
}
20 changes: 20 additions & 0 deletions adapters/kobler/kobler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kobler

import (
"testing"

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderKargo, config.Adapter{
Endpoint: "http://fake.endpoint"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

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

adapterstest.RunJSONBidderTest(t, "koblertest", bidder)
}
120 changes: 120 additions & 0 deletions adapters/kobler/koblertest/exemplary/simple_banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"mockBidRequest": {
"id": "test-request-id",
"device": {
"devicetype": 2
},
"site": {
"page": "http://example.com"
},
"imp": [
{
"id": "test-imp-id",
"tagid": "test",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 320,
"h": 100
}
]
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://fake.endpoint",
"body": {
"id": "test-request-id",
"device": {
"devicetype": 2
},
"site": {
"page": "http://example.com"
},
"imp": [
{
"id": "test-imp-id",
"tagid": "test",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 320,
"h": 100
}
]
}
}
],
"cur": ["USD"]
},
"impIDs": ["test-imp-id"]
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"http://example.com\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
}
}
],
"seat": "kobler"
}
],
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"bids": [
{
"bid": {
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"http://example.com\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
}
},
"type": "banner"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"mockBidRequest": {
"id": "test-request-id",
"device": {
"devicetype": 2
},
"site": {
"page": "http://example.com"
},
"imp": [
{
"id": "test-imp-id",
"tagid": "test",
"bidfloor": 1,
"bidfloorcur": "GBP",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 320,
"h": 100
}
]
}
}
],
"ext": {
"prebid": {
"currency": {
"rates": {
"EUR": {
"USD": 1.11
}
},
"usepbsrates": false
}
}
}
},
"httpCalls": [],
"expectedBidResponses": [],
"expectedMakeRequestsErrors": [
{
"value": "Currency conversion rate not found: 'GBP' => 'USD'",
"comparison": "literal"
}
]
}
Loading
Loading