Skip to content

Commit

Permalink
Kubient adapter (prebid#1094)
Browse files Browse the repository at this point in the history
  • Loading branch information
mapcuk authored and mansinahar committed Apr 15, 2020
1 parent 41856f3 commit b0a8c2c
Show file tree
Hide file tree
Showing 13 changed files with 502 additions and 1 deletion.
110 changes: 110 additions & 0 deletions adapters/kubient/kubient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package kubient

import (
"encoding/json"
"fmt"
"github.com/prebid/prebid-server/openrtb_ext"
"net/http"

"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
)

func NewKubientBidder(endpoint string) *KubientAdapter {
return &KubientAdapter{endpoint: endpoint}
}

// Implements Bidder interface.
type KubientAdapter struct {
endpoint string
}

// MakeRequests prepares the HTTP requests which should be made to fetch bids.
func (adapter *KubientAdapter) MakeRequests(
openRTBRequest *openrtb.BidRequest,
reqInfo *adapters.ExtraRequestInfo,
) (
requestsToBidder []*adapters.RequestData,
errs []error,
) {
openRTBRequestJSON, err := json.Marshal(openRTBRequest)
if err != nil {
errs = append(errs, err)
return nil, errs
}

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
requestToBidder := &adapters.RequestData{
Method: "POST",
Uri: adapter.endpoint,
Body: openRTBRequestJSON,
Headers: headers,
}
requestsToBidder = append(requestsToBidder, requestToBidder)

return requestsToBidder, errs
}

// MakeBids makes the bids
func (adapter *KubientAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var errs []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. Run with request.debug = 1 for more info", response.StatusCode),
}}
}

if response.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", 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 {
bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
if err != nil {
errs = append(errs, err)
} else {
b := &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
}
return bidResponse, errs
}

func getMediaTypeForImp(impID string, imps []openrtb.Imp) (openrtb_ext.BidType, error) {
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner == nil && imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
}
return mediaType, nil
}
}

// This shouldnt happen. Lets handle it just incase by returning an error.
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find impression \"%s\" ", impID),
}
}
10 changes: 10 additions & 0 deletions adapters/kubient/kubient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kubient

import (
"github.com/prebid/prebid-server/adapters/adapterstest"
"testing"
)

func TestJsonSamples(t *testing.T) {
adapterstest.RunJSONBidderTest(t, "kubienttest", NewKubientBidder("http://127.0.0.1:5000/bid"))
}
97 changes: 97 additions & 0 deletions adapters/kubient/kubienttest/exemplary/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"mockBidRequest": {
"id": "test-banner-request-id",
"imp": [
{
"id": "test-imp-banner-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://127.0.0.1:5000/bid",
"body": {
"id": "test-banner-request-id",
"imp": [
{
"id": "test-imp-banner-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-banner-request-id",
"seatbid": [
{
"seat": "772",
"bid": [{
"id": "7706636740145184841",
"impid": "test-imp-banner-id",
"price": 0.500000,
"adid": "29681110",
"adm": "some-test-ad",
"adomain": ["advertsite.com"],
"cid": "772",
"crid": "29681110",
"h": 576,
"w": 1024
}]
}
],
"bidid": "5778926625248726496",
"cur": "USD"
}
}
}
],
"expectedBids": [
{
"bid": {
"id": "7706636740145184841",
"impid": "test-imp-banner-id",
"price": 0.5,
"adm": "some-test-ad",
"adid": "29681110",
"adomain": ["advertsite.com"],
"cid": "772",
"crid": "29681110",
"w": 1024,
"h": 576
},
"type": "banner"
}
]
}
87 changes: 87 additions & 0 deletions adapters/kubient/kubienttest/exemplary/video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"mockBidRequest": {
"id": "test-video-request-id",
"imp": [
{
"id": "test-imp-video-id",
"video": {
"mimes": ["video/mp4"],
"protocols": [2, 5],
"w": 1024,
"h": 576
},
"ext": {
"bidder": {}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://127.0.0.1:5000/bid",
"body": {
"id": "test-video-request-id",
"imp": [
{
"id": "test-imp-video-id",
"video": {
"mimes": ["video/mp4"],
"protocols": [2, 5],
"w": 1024,
"h": 576
},
"ext": {
"bidder": {}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "ssp-response-id",
"seatbid": [
{
"seat": "83",
"bid": [{
"id": "7706636740145184841",
"impid": "test-imp-video-id",
"price": 0.500000,
"adid": "29681110",
"adm": "some-video-ad",
"adomain": ["advertsite.com"],
"cid": "958",
"crid": "29681110",
"h": 576,
"w": 1024
}]
}
],
"bidid": "5778926625248726496",
"cur": "USD"
}
}
}
],

"expectedBids": [
{
"bid": {
"id": "7706636740145184841",
"impid": "test-imp-video-id",
"price": 0.5,
"adm": "some-test-ad",
"adid": "29681110",
"adomain": ["advertsite.com"],
"cid": "83",
"crid": "29681110",
"w": 1024,
"h": 576
},
"type": "video"
}
]

}
57 changes: 57 additions & 0 deletions adapters/kubient/kubienttest/supplemental/bad_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"bidder": {}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://127.0.0.1:5000/bid",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"bidder": {}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": "{\"id\"data.lost"
}
}
],
"expectedMakeBidsErrors": [
{
"value": "json: cannot unmarshal string into Go value of type openrtb.BidResponse",
"comparison": "literal"
}
]
}
Loading

0 comments on commit b0a8c2c

Please sign in to comment.