Skip to content

Commit

Permalink
kueezrtb adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
saar120 committed Nov 4, 2024
1 parent 6e150f3 commit bf7ab42
Show file tree
Hide file tree
Showing 15 changed files with 1,091 additions and 0 deletions.
134 changes: 134 additions & 0 deletions adapters/kueezrtb/kueezrtb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package kueezrtb

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"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
}

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

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

requestCopy := *request

for _, imp := range request.Imp {
requestCopy.Imp = []openrtb2.Imp{imp}

requestJSON, err := json.Marshal(&requestCopy)
if err != nil {
errors = append(errors, fmt.Errorf("marshal bidRequest: %w", err))
continue
}

cId, err := extractCid(&imp)
if err != nil {
errors = append(errors, fmt.Errorf("extract cId: %w", err))
continue
}

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

requestData := &adapters.RequestData{
Method: "POST",
Uri: fmt.Sprintf("%s%s", a.endpoint, url.QueryEscape(cId)),
Body: requestJSON,
Headers: headers,
ImpIDs: []string{imp.ID},
}

requests = append(requests, requestData)
}

return requests, errors
}

func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
switch bid.MType {
case openrtb2.MarkupBanner:
return openrtb_ext.BidTypeBanner, nil
case openrtb2.MarkupVideo:
return openrtb_ext.BidTypeVideo, nil
}
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Could not define bid type for imp: %s", bid.ImpID),
}
}

func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var errs []error

if adapters.IsResponseStatusCodeNoContent(responseData) {
return nil, nil
}

if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
return nil, []error{&errortypes.BadInput{
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{&errortypes.BadServerResponse{
Message: fmt.Sprintf("bad server response: %d. ", err),
}}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(response.SeatBid))

if response.Cur != "" {
bidResponse.Currency = response.Cur
}

for _, seatBid := range response.SeatBid {
for i, bid := range seatBid.Bid {
bidType, err := getMediaTypeForBid(bid)
if err != nil {
errs = append(errs, err)
continue
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: bidType,
})
}
}

return bidResponse, errs
}

func extractCid(imp *openrtb2.Imp) (string, error) {
var bidderExt adapters.ExtImpBidder
if err := jsonutil.Unmarshal(imp.Ext, &bidderExt); err != nil {
return "", fmt.Errorf("unmarshal bidderExt: %w", err)
}

var impExt openrtb_ext.ImpExtKueez
if err := jsonutil.Unmarshal(bidderExt.Bidder, &impExt); err != nil {
return "", fmt.Errorf("unmarshal ImpExtkueez: %w", err)
}
return strings.TrimSpace(impExt.ConnectionId), nil
}
24 changes: 24 additions & 0 deletions adapters/kueezrtb/kueezrtb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kueezrtb

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.BidderKueezRTB, config.Adapter{
Endpoint: "http://prebidsrvr.kueezrtb.com/openrtb/",
},
config.Server{
ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2",
})

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

adapterstest.RunJSONBidderTest(t, "kueezrtbtest", bidder)
}
127 changes: 127 additions & 0 deletions adapters/kueezrtb/kueezrtbtest/exemplary/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"mockBidRequest": {
"id": "some-request-id",
"site": {
"page": "prebid.org"
},
"imp": [
{
"id": "some-impression-id",
"banner": {
"w": 240,
"h": 400
},
"ext": {
"bidder": {
"cId": "test_cid_123"
}
}
}
],
"tmax": 5000
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://prebidsrvr.kueezrtb.com/openrtb/test_cid_123",
"body": {
"id": "some-request-id",
"site": {
"page": "prebid.org"
},
"imp": [
{
"id": "some-impression-id",
"banner": {
"w": 240,
"h": 400
},
"ext": {
"bidder": {
"cId": "test_cid_123"
}
}
}
],
"tmax": 5000
},
"impIDs": [
"some-impression-id"
]
},
"mockResponse": {
"status": 200,
"body": {
"id": "some-request-id",
"cur": "",
"bidid": "some-bid-id",
"seatbid": [
{
"bid": [
{
"exp": 60,
"adm": "<div>Some creative</div>",
"burl": "",
"iurl": "http://prebidsrvr.kueezrtb.com/creative.jpg",
"lurl": "",
"nurl": "",
"id": "some-bid-id",
"impid": "some-impression-id",
"h": 400,
"w": 240,
"price": 1,
"dealid": "deal123",
"adomain": [
"test.com"
],
"adid": "some-ad-id",
"cid": "test",
"attr": [],
"cat": [],
"crid": "some-creative-id",
"ext": {},
"hratio": 0,
"language": "",
"protocol": 0,
"qagmediarating": 0,
"tactic": "",
"wratio": 0,
"mtype": 1
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"bids": [
{
"bid": {
"id": "some-bid-id",
"impid": "some-impression-id",
"price": 1,
"adm": "<div>Some creative</div>",
"adid": "some-ad-id",
"cid": "test",
"iurl": "http://prebidsrvr.kueezrtb.com/creative.jpg",
"crid": "some-creative-id",
"adomain": [
"test.com"
],
"dealid": "deal123",
"w": 240,
"h": 400,
"exp": 60,
"ext": {},
"mtype": 1
},
"type": "banner"
}
]
}
],
"expectedMakeBidsErrors": []
}
Loading

0 comments on commit bf7ab42

Please sign in to comment.