Skip to content

Commit

Permalink
Merge branch 'prebid:master' into freewheel-go-adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mwang-sticky authored Nov 10, 2022
2 parents 679e695 + 98b7bb4 commit b667a7b
Show file tree
Hide file tree
Showing 77 changed files with 5,898 additions and 583 deletions.
18 changes: 16 additions & 2 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ func GetAccount(ctx context.Context, cfg *config.Configuration, fetcher stored_r
return account, nil
}

// TCF2Enforcements maps enforcement algo string values to their integer representation and is
// used to limit string compares
var TCF2Enforcements = map[string]config.TCF2EnforcementAlgo{
config.TCF2EnforceAlgoBasic: config.TCF2BasicEnforcement,
config.TCF2EnforceAlgoFull: config.TCF2FullEnforcement,
}

// setDerivedConfig modifies an account object by setting fields derived from other fields set in the account configuration
func setDerivedConfig(account *config.Account) {
account.GDPR.PurposeConfigs = map[consentconstants.Purpose]*config.AccountGDPRPurpose{
Expand All @@ -107,9 +114,16 @@ func setDerivedConfig(account *config.Account) {
10: &account.GDPR.Purpose10,
}

// To look for a purpose's vendor exceptions in O(1) time, for each purpose we fill this hash table with bidders
// located in the VendorExceptions field of the GDPR.PurposeX struct
for _, pc := range account.GDPR.PurposeConfigs {
// To minimize the number of string compares per request, we set the integer representation
// of the enforcement algorithm on each purpose config
pc.EnforceAlgoID = config.TCF2UndefinedEnforcement
if algo, exists := TCF2Enforcements[pc.EnforceAlgo]; exists {
pc.EnforceAlgoID = algo
}

// To look for a purpose's vendor exceptions in O(1) time, for each purpose we fill this hash table with bidders
// located in the VendorExceptions field of the GDPR.PurposeX struct
if pc.VendorExceptions == nil {
continue
}
Expand Down
15 changes: 15 additions & 0 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func TestSetDerivedConfig(t *testing.T) {
purpose1VendorExceptions []openrtb_ext.BidderName
feature1VendorExceptions []openrtb_ext.BidderName
basicEnforcementVendors []string
enforceAlgo string
wantEnforceAlgoID config.TCF2EnforcementAlgo
}{
{
description: "Nil purpose 1 vendor exceptions",
Expand Down Expand Up @@ -158,13 +160,24 @@ func TestSetDerivedConfig(t *testing.T) {
description: "Multiple basic enforcement vendors",
basicEnforcementVendors: []string{"appnexus", "rubicon"},
},
{
description: "Basic Enforcement algorithm",
enforceAlgo: config.TCF2EnforceAlgoBasic,
wantEnforceAlgoID: config.TCF2BasicEnforcement,
},
{
description: "Full Enforcement algorithm",
enforceAlgo: config.TCF2EnforceAlgoFull,
wantEnforceAlgoID: config.TCF2FullEnforcement,
},
}

for _, tt := range tests {
account := config.Account{
GDPR: config.AccountGDPR{
Purpose1: config.AccountGDPRPurpose{
VendorExceptions: tt.purpose1VendorExceptions,
EnforceAlgo: tt.enforceAlgo,
},
SpecialFeature1: config.AccountGDPRSpecialFeature{
VendorExceptions: tt.feature1VendorExceptions,
Expand Down Expand Up @@ -193,6 +206,8 @@ func TestSetDerivedConfig(t *testing.T) {
assert.ElementsMatch(t, purpose1ExceptionMapKeys, tt.purpose1VendorExceptions, tt.description)
assert.ElementsMatch(t, feature1ExceptionMapKeys, tt.feature1VendorExceptions, tt.description)
assert.ElementsMatch(t, basicEnforcementMapKeys, tt.basicEnforcementVendors, tt.description)

assert.Equal(t, account.GDPR.Purpose1.EnforceAlgoID, tt.wantEnforceAlgoID, tt.description)
}
}

Expand Down
154 changes: 154 additions & 0 deletions adapters/appush/appush.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package appush

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

"github.com/prebid/openrtb/v17/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
}

type reqBodyExt struct {
AppushBidderExt reqBodyExtBidder `json:"bidder"`
}

type reqBodyExtBidder struct {
Type string `json:"type"`
PlacementID string `json:"placementId,omitempty"`
EndpointID string `json:"endpointId,omitempty"`
}

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, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var err error
var adapterRequests []*adapters.RequestData

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

var bidderExt adapters.ExtImpBidder
var appushExt openrtb_ext.ImpExtAppush

if err = json.Unmarshal(reqCopy.Imp[0].Ext, &bidderExt); err != nil {
return nil, []error{err}
}
if err = json.Unmarshal(bidderExt.Bidder, &appushExt); err != nil {
return nil, []error{err}
}

temp := reqBodyExt{AppushBidderExt: reqBodyExtBidder{}}

if appushExt.PlacementID != "" {
temp.AppushBidderExt.PlacementID = appushExt.PlacementID
temp.AppushBidderExt.Type = "publisher"
} else if appushExt.EndpointID != "" {
temp.AppushBidderExt.EndpointID = appushExt.EndpointID
temp.AppushBidderExt.Type = "network"
}

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

reqCopy.Imp[0].Ext = finalyImpExt

adapterReq, err := a.makeRequest(&reqCopy)
if err != nil {
return nil, []error{err}
}

if adapterReq != nil {
adapterRequests = append(adapterRequests, adapterReq)
}
}
return adapterRequests, nil
}

func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) {
reqJSON, err := json.Marshal(request)
if err != nil {
return nil, err
}

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
return &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: reqJSON,
Headers: headers,
}, err
}

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.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
for _, seatBid := range response.SeatBid {
for i := range seatBid.Bid {
bidType, err := getMediaTypeForImp(seatBid.Bid[i].ImpID, request.Imp)
if err != nil {
return nil, []error{err}
}

b := &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
return bidResponse, nil
}

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

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

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

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

adapterstest.RunJSONBidderTest(t, "appushtest", bidder)
}
133 changes: 133 additions & 0 deletions adapters/appush/appushtest/exemplary/endpointId.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"mockBidRequest": {
"id": "test-request-id",
"device": {
"ip": "123.123.123.123",
"ua": "iPad"
},
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"imp": [
{
"id": "test-imp-id",
"tagid": "test",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"endpointId": "test"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://example.com/pserver",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"tagid": "test",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"endpointId": "test",
"type": "network"
}
}
}
],
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"device": {
"ip": "123.123.123.123",
"ua": "iPad"
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"http://example.com/pserver&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
}
}
],
"seat": "appush"
}
],
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"bids": [
{
"bid": {
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"http://example.com/pserver&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"ext": {
"prebid": {
"type": "banner"
}
}
},
"type": "banner"
}
]
}
]
}
Loading

0 comments on commit b667a7b

Please sign in to comment.