Skip to content

Commit

Permalink
New Adapter: Kobler (#3904)
Browse files Browse the repository at this point in the history
Co-authored-by: acsbendi <[email protected]>
  • Loading branch information
TommyHPettersen and acsbendi authored Jan 8, 2025
1 parent 6e3a0d5 commit 950b76f
Show file tree
Hide file tree
Showing 17 changed files with 1,112 additions and 0 deletions.
159 changes: 159 additions & 0 deletions adapters/kobler/kobler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package kobler

import (
"fmt"
"net/http"
"slices"
"strings"

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

type adapter struct {
endpoint string
devEndpoint string
}

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

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

return bidder, nil
}

func (a adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var requestData []*adapters.RequestData
var errors []error

testMode := false

if !slices.Contains(request.Cur, supportedCurrency) {
request.Cur = append(request.Cur, supportedCurrency)
}

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

// Check the first Imp for test mode, which decides the endpoint.
if i == 0 && request.Imp[i].Ext != nil {
var bidderExt adapters.ExtImpBidder
if err := jsonutil.Unmarshal(request.Imp[i].Ext, &bidderExt); err != nil {
errors = append(errors, &errortypes.BadInput{
Message: "Error parsing bidderExt object",
})
continue
}

var impExt openrtb_ext.ExtImpKobler
if err := jsonutil.Unmarshal(bidderExt.Bidder, &impExt); err != nil {
errors = append(errors, &errortypes.BadInput{
Message: "Error parsing impExt object",
})
continue
}

testMode = impExt.Test
}
}

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

// Use a separate endpoint for testing purposes in the DEV environment.
// Required due to Kobler's internal test campaign setup.
endpoint := a.endpoint
if testMode {
endpoint = a.devEndpoint
}

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

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

return requestData, nil
}

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 := jsonutil.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 getMediaTypeForBid(bid openrtb2.Bid) openrtb_ext.BidType {
if bid.Ext != nil {
var bidExt openrtb_ext.ExtBid
err := jsonutil.Unmarshal(bid.Ext, &bidExt)
if err == nil && bidExt.Prebid != nil {
mediaType, err := openrtb_ext.ParseBidType(string(bidExt.Prebid.Type))
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) != supportedCurrency {
convertedValue, err := reqInfo.ConvertCurrency(imp.BidFloor, imp.BidFloorCur, supportedCurrency)
if err != nil {
return err
}

imp.BidFloor = convertedValue
imp.BidFloorCur = supportedCurrency
}

return nil
}
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/v3/adapters/adapterstest"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/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

0 comments on commit 950b76f

Please sign in to comment.