Skip to content

Commit

Permalink
New Adapter: Zemanta (prebid#1774)
Browse files Browse the repository at this point in the history
* add zemanta adapter

* update openrtb package for zemanta

* fix loop iterator reference bug

* fix getMediaTypeForImp to match server behavior
  • Loading branch information
rokostik authored and Dan Barnett committed May 11, 2021
1 parent f5c3e18 commit 8fa698a
Show file tree
Hide file tree
Showing 22 changed files with 1,104 additions and 0 deletions.
50 changes: 50 additions & 0 deletions adapters/zemanta/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package zemanta

import (
"encoding/json"
"testing"

"github.com/prebid/prebid-server/openrtb_ext"
)

func TestValidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json schema. %v", err)
}

for _, p := range validParams {
if err := validator.Validate(openrtb_ext.BidderZemanta, json.RawMessage(p)); err != nil {
t.Errorf("Schema rejected valid params: %s", p)
}
}
}

func TestInvalidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json schema. %v", err)
}

for _, p := range invalidParams {
if err := validator.Validate(openrtb_ext.BidderZemanta, json.RawMessage(p)); err == nil {
t.Errorf("Schema allowed invalid params: %s", p)
}
}
}

var validParams = []string{
`{"publisher": {"id": "publisher-id"}}`,
`{"publisher": {"id": "publisher-id", "name": "publisher-name", "domain": "publisher-domain.com"}, "tagid": "tag-id", "bcat": ["bad-category"], "badv": ["bad-advertiser"]}`,
}

var invalidParams = []string{
`{"publisher": {"id": 1234}}`,
`{"publisher": {"id": "pub-id", "name": 1234}}`,
`{"publisher": {"id": "pub-id", "domain": 1234}}`,
`{"publisher": {"id": "pub-id"}, "tagid": 1234}`,
`{"publisher": {"id": "pub-id"}, "bcat": "not-array"}`,
`{"publisher": {"id": "pub-id"}, "bcat": [1234]}`,
`{"publisher": {"id": "pub-id"}, "badv": "not-array"}`,
`{"publisher": {"id": "pub-id"}, "badv": [1234]}`,
}
12 changes: 12 additions & 0 deletions adapters/zemanta/usersync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package zemanta

import (
"text/template"

"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/usersync"
)

func NewZemantaSyncer(temp *template.Template) usersync.Usersyncer {
return adapters.NewSyncer("zemanta", temp, adapters.SyncTypeRedirect)
}
33 changes: 33 additions & 0 deletions adapters/zemanta/usersync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package zemanta

import (
"testing"
"text/template"

"github.com/prebid/prebid-server/privacy"
"github.com/prebid/prebid-server/privacy/ccpa"
"github.com/prebid/prebid-server/privacy/gdpr"
"github.com/stretchr/testify/assert"
)

func TestSyncer(t *testing.T) {
syncURL := "http://prebidtest.zemanta.com/usersync/prebidtest?gdpr={{.GDPR}}&gdpr_consent={{.GDPRConsent}}&us_privacy={{.USPrivacy}}&cb=host%2Fsetuid%3Fbidder%3Dzemanta%26uid%3D__ZUID__"
syncURLTemplate := template.Must(
template.New("sync-template").Parse(syncURL),
)

syncer := NewZemantaSyncer(syncURLTemplate)
syncInfo, err := syncer.GetUsersyncInfo(privacy.Policies{
GDPR: gdpr.Policy{
Signal: "A",
Consent: "B",
},
CCPA: ccpa.Policy{
Consent: "C",
},
})

assert.NoError(t, err)
assert.Equal(t, "http://prebidtest.zemanta.com/usersync/prebidtest?gdpr=A&gdpr_consent=B&us_privacy=C&cb=host%2Fsetuid%3Fbidder%3Dzemanta%26uid%3D__ZUID__", syncInfo.URL)
assert.Equal(t, "redirect", syncInfo.Type)
}
147 changes: 147 additions & 0 deletions adapters/zemanta/zemanta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package zemanta

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

"github.com/mxmCherry/openrtb/v14/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
)

type adapter struct {
endpoint string
}

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

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
reqCopy := *request

var errs []error
var zemantaExt openrtb_ext.ExtImpZemanta
for i := 0; i < len(reqCopy.Imp); i++ {
imp := reqCopy.Imp[i]

var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
errs = append(errs, err)
continue
}
if err := json.Unmarshal(bidderExt.Bidder, &zemantaExt); err != nil {
errs = append(errs, err)
continue
}
imp.TagID = zemantaExt.TagId
reqCopy.Imp[i] = imp
}

publisher := &openrtb2.Publisher{
ID: zemantaExt.Publisher.Id,
Name: zemantaExt.Publisher.Name,
Domain: zemantaExt.Publisher.Domain,
}
if reqCopy.Site != nil {
siteCopy := *reqCopy.Site
siteCopy.Publisher = publisher
reqCopy.Site = &siteCopy
} else if reqCopy.App != nil {
appCopy := *reqCopy.App
appCopy.Publisher = publisher
reqCopy.App = &appCopy
}

if zemantaExt.BCat != nil {
reqCopy.BCat = zemantaExt.BCat
}
if zemantaExt.BAdv != nil {
reqCopy.BAdv = zemantaExt.BAdv
}

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

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

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

func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if responseData.StatusCode == http.StatusNoContent {
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 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

var errs []error
for _, seatBid := range response.SeatBid {
for i := range seatBid.Bid {
bid := seatBid.Bid[i]
bidType, err := getMediaTypeForImp(bid.ImpID, request.Imp)
if err != nil {
errs = append(errs, err)
continue
}

b := &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}

return bidResponse, errs
}

func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
for _, imp := range imps {
if imp.ID == impID {
if imp.Native != nil {
return openrtb_ext.BidTypeNative, nil
} else if imp.Banner != nil {
return openrtb_ext.BidTypeBanner, nil
}
}
}

return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find native/banner impression \"%s\" ", impID),
}
}
20 changes: 20 additions & 0 deletions adapters/zemanta/zemanta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package zemanta

import (
"testing"

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderZemanta, config.Adapter{
Endpoint: "http://example.com/bid"})

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

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

0 comments on commit 8fa698a

Please sign in to comment.