Skip to content

Commit

Permalink
Add Yieldlab Adapter (#1287)
Browse files Browse the repository at this point in the history
Co-authored-by: Mirko Feddern <mirkorean@users.noreply.github.com>
Signed-off-by: Alex Klinkert <alex@klinkert.io>

Co-authored-by: Alexander Pinnecke <alexander.pinnecke@googlemail.com>
Co-authored-by: Alex Klinkert <alex@klinkert.io>
Co-authored-by: Mirko Feddern <mirkorean@users.noreply.github.com>
  • Loading branch information
4 people authored Jun 2, 2020
1 parent 748e2d0 commit d00ff3f
Show file tree
Hide file tree
Showing 22 changed files with 1,147 additions and 68 deletions.
2 changes: 1 addition & 1 deletion adapters/adapterstest/test_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func diffErrorLists(t *testing.T, description string, actual []error, expected [
t.Helper()

if len(expected) != len(actual) {
t.Fatalf("%s had wrong error count. Expected %d, got %d", description, len(expected), len(actual))
t.Fatalf("%s had wrong error count. Expected %d, got %d (%v)", description, len(expected), len(actual), actual)
}
for i := 0; i < len(actual); i++ {
if expected[i].Comparison == "literal" {
Expand Down
7 changes: 7 additions & 0 deletions adapters/yieldlab/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package yieldlab

const adSlotIdSeparator = ","
const adsizeSeparator = "x"
const adSourceBanner = "<script src=\"%v\"></script>"
const adSourceURL = "https://ad.yieldlab.net/d/%v/%v/%v?%v"
const creativeID = "%v%v%v"
63 changes: 63 additions & 0 deletions adapters/yieldlab/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package yieldlab

import (
"encoding/json"
"testing"

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

// This file actually intends to test static/bidder-params/yieldlab.json
//
// These also validate the format of the external API: request.imp[i].ext.yieldlab

// TestValidParams makes sure that the yieldlab schema accepts all imp.ext fields which we intend to support.
func TestValidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json-schemas. %v", err)
}

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

// TestInvalidParams makes sure that the yieldlab schema rejects all the imp.ext fields we don't support.
func TestInvalidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json-schemas. %v", err)
}

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

var validParams = []string{
`{"adslotId": "123","supplyId":"23456","adSize":"100x100"}`,
`{"adslotId": "123","supplyId":"23456","adSize":"100x100","extId":"asdf"}`,
`{"adslotId": "123","supplyId":"23456","adSize":"100x100","extId":"asdf","targeting":{"a":"b"}}`,
`{"adslotId": "123","supplyId":"23456","adSize":"100x100","targeting":{"a":"b"}}`,
`{"adslotId": "123","supplyId":"23456","adSize":"100x100","targeting":{"a":"b"}}`,
}

var invalidParams = []string{
`{"supplyId":"23456","adSize":"100x100"}`,
`{"adslotId": "123","adSize":"100x100","extId":"asdf"}`,
`{"adslotId": "123","supplyId":"23456","extId":"asdf","targeting":{"a":"b"}}`,
`{"adslotId": "123","supplyId":"23456"}`,
`{"adSize":"100x100","supplyId":"23456"}`,
`{"adslotId": "123","adSize":"100x100"}`,
`{"supplyId":"23456"}`,
`{"adslotId": "123"}`,
`{}`,
`[]`,
`{"a":"b"}`,
`null`,
}
29 changes: 29 additions & 0 deletions adapters/yieldlab/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package yieldlab

import (
"strconv"
"time"
)

type bidResponse struct {
ID uint64 `json:"id"`
Price uint `json:"price"`
Advertiser string `json:"advertiser"`
Adsize string `json:"adsize"`
Pid uint64 `json:"pid"`
Did uint64 `json:"did"`
Pvid string `json:"pvid"`
}

type cacheBuster func() string

type weekGenerator func() string

var defaultCacheBuster cacheBuster = func() string {
return strconv.FormatInt(time.Now().Unix(), 10)
}

var defaultWeekGenerator weekGenerator = func() string {
_, week := time.Now().ISOWeek()
return strconv.Itoa(week)
}
12 changes: 12 additions & 0 deletions adapters/yieldlab/usersync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package yieldlab

import (
"text/template"

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

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

import (
"testing"
"text/template"

"github.com/stretchr/testify/assert"

"github.com/prebid/prebid-server/privacy"
"github.com/prebid/prebid-server/privacy/gdpr"
)

func TestYieldlabSyncer(t *testing.T) {
temp := template.Must(template.New("sync-template").Parse("https://ad.yieldlab.net/mr?t=2&pid=9140838&gdpr={{.GDPR}}&gdpr_consent={{.GDPRConsent}}&redirectUri=http%3A%2F%2Flocalhost%2F%2Fsetuid%3Fbidder%3Dyieldlab%26gdpr%3D{{.GDPR}}%26gdpr_consent%3D{{.GDPRConsent}}%26uid%3D%25%25YL_UID%25%25"))
syncer := NewYieldlabSyncer(temp)
syncInfo, err := syncer.GetUsersyncInfo(privacy.Policies{
GDPR: gdpr.Policy{
Signal: "0",
},
})
assert.NoError(t, err)
assert.Equal(t, "https://ad.yieldlab.net/mr?t=2&pid=9140838&gdpr=0&gdpr_consent=&redirectUri=http%3A%2F%2Flocalhost%2F%2Fsetuid%3Fbidder%3Dyieldlab%26gdpr%3D0%26gdpr_consent%3D%26uid%3D%25%25YL_UID%25%25", syncInfo.URL)
assert.Equal(t, "redirect", syncInfo.Type)
assert.EqualValues(t, 70, syncer.GDPRVendorID())
assert.False(t, syncInfo.SupportCORS)
}
Loading

0 comments on commit d00ff3f

Please sign in to comment.