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: Onetag #1695

Merged
merged 5 commits into from
Feb 11, 2021
Merged
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
169 changes: 169 additions & 0 deletions adapters/onetag/onetag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package onetag

import (
"encoding/json"
"fmt"
"net/http"
"text/template"

"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/macros"
"github.com/prebid/prebid-server/openrtb_ext"
)

type onetagAdapter struct {
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
endpointTemplate template.Template
}

func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
template, err := template.New("endpointTemplate").Parse(config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
}

bidder := &onetagAdapter{
endpointTemplate: *template,
}

return bidder, nil
}

func (a *onetagAdapter) MakeRequests(request *openrtb.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {

if len(request.Imp) <= 0 {
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
return nil, []error{&errortypes.BadInput{
Message: "Missing Imp Object",
}}
}

pubID := ""
for idx, imp := range request.Imp {
onetagExt, err := getImpressionExt(imp)
if err != nil {
return nil, []error{err}
}
if onetagExt.PubId != "" {
if pubID == "" {
pubID = onetagExt.PubId
}
if pubID != onetagExt.PubId {
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
return nil, []error{&errortypes.BadInput{
Message: "There must be only one publisher ID",
}}
}
}
request.Imp[idx].Ext = onetagExt.Ext
}

if pubID == "" {
return nil, []error{&errortypes.BadInput{
Message: "Missing Publisher ID",
}}
}

url, err := a.buildEndpointURL(pubID)
if err != nil {
return nil, []error{err}
}

requestJSON, err := json.Marshal(request)
if err != nil {
return nil, []error{err}
}

requestData := &adapters.RequestData{
Method: "POST",
Uri: url,
Body: requestJSON,
}

return []*adapters.RequestData{requestData}, nil
}

func getImpressionExt(imp openrtb.Imp) (*openrtb_ext.ExtImpOnetag, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Bidder extension not provided or can't be unmarshalled",
}
}

var onetagExt openrtb_ext.ExtImpOnetag
if err := json.Unmarshal(bidderExt.Bidder, &onetagExt); err != nil {
return nil, &errortypes.BadInput{
Message: "Error while unmarshaling bidder extension",
}
}

return &onetagExt, nil
}

func (a *onetagAdapter) buildEndpointURL(pubID string) (string, error) {
endpointParams := macros.EndpointTemplateParams{PublisherID: pubID}
return macros.ResolveMacros(a.endpointTemplate, endpointParams)
}

func (a *onetagAdapter) MakeBids(request *openrtb.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if responseData.StatusCode == http.StatusNoContent {
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
}

if responseData.StatusCode == http.StatusBadRequest {
err := &errortypes.BadInput{
Message: "Unexpected status code: 400. Bad request from publisher. Run with request.debug = 1 for more info.",
}
return nil, []error{err}
}

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

var response openrtb.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 _, bid := range seatBid.Bid {
bidMediaType, err := getMediaTypeForBid(request.Imp, bid)
if err != nil {
return nil, []error{err}
}
b := &adapters.TypedBid{
Bid: &bid,
BidType: bidMediaType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
return bidResponse, nil
}

func getMediaTypeForBid(impressions []openrtb.Imp, bid openrtb.Bid) (openrtb_ext.BidType, error) {
for _, impression := range impressions {
if impression.ID == bid.ImpID {
if impression.Banner != nil {
return openrtb_ext.BidTypeBanner, nil
}
if impression.Video != nil {
return openrtb_ext.BidTypeVideo, nil
}
if impression.Native != nil {
return openrtb_ext.BidTypeNative, nil
}
}
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
}

return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("The impression with ID %s is not present into the request", bid.ImpID),
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
}
}
28 changes: 28 additions & 0 deletions adapters/onetag/onetag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package onetag

import (
"testing"

"github.com/prebid/prebid-server/adapters/adapterstest"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/stretchr/testify/assert"
)

func TestEndpointTemplateMalformed(t *testing.T) {
_, buildErr := Builder(openrtb_ext.BidderOneTag, config.Adapter{
Endpoint: "{{Malformed}}"})

assert.Error(t, buildErr)
}

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderOneTag, config.Adapter{
Endpoint: "https://example.com/prebid-server/{{.PublisherID}}"})

if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
}

adapterstest.RunJSONBidderTest(t, "onetagtest", bidder)
}
Loading