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: Pangle #1697

Merged
merged 3 commits into from
Feb 18, 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
201 changes: 201 additions & 0 deletions adapters/pangle/pangle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package pangle

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

"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/openrtb_ext"
)

type pangleAdapter struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason to repeat the package name in this type. The name adapter will be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Endpoint string
}

type wrappedExtImpBidder struct {
*adapters.ExtImpBidder
AdType int `json:"adtype,omitempty"`
}

type pangleBidExt struct {
Pangle *bidExt `json:"pangle,omitempty"`
}

type bidExt struct {
AdType *int `json:"adtype,omitempty"`
}

/* Builder */

func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &pangleAdapter{
Endpoint: config.Endpoint,
}

return bidder, nil
}

/* MakeRequests */

func getAdType(imp openrtb.Imp, parsedImpExt *wrappedExtImpBidder) (adType int) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style Nitpick: The flow might be easier to read is you return the adType directly instead of using a named return.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected

adType = -1
// video
if imp.Video != nil {
if parsedImpExt != nil && parsedImpExt.Prebid != nil && parsedImpExt.Prebid.IsRewardedInventory == 1 {
adType = 7
return
}
if imp.Instl == 1 {
adType = 8
return
}
}
// banner
if imp.Banner != nil {
if imp.Instl == 1 {
adType = 2
} else {
adType = 1
}
return
}
// native
if imp.Native != nil && len(imp.Native.Request) > 0 {
adType = 5
return
}

return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to verify the orders of the checks (first video, then banner, then native) is intentional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it's intentional

}

func (pa *pangleAdapter) MakeRequests(request *openrtb.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var requests []*adapters.RequestData
var errs []error

requestCopy := *request
for _, imp := range request.Imp {
var impExt wrappedExtImpBidder
if err := json.Unmarshal(imp.Ext, &impExt); err != nil {
errs = append(errs, fmt.Errorf("failed unmarshalling imp ext (err)%s", err.Error()))
continue
}
// detect and fill adtype
if adType := getAdType(imp, &impExt); adType == -1 {
errs = append(errs, fmt.Errorf("not a supported adtype"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may wish to use a the BadInput error type for this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

continue
} else {
impExt.AdType = adType
if newImpExt, err := json.Marshal(impExt); err == nil {
imp.Ext = newImpExt
} else {
errs = append(errs, fmt.Errorf("failed re-marshalling imp ext with adtype"))
continue
}
}
// for setting token
var bidderImpExt openrtb_ext.ImpExtPangle
if err := json.Unmarshal(impExt.Bidder, &bidderImpExt); err != nil {
errs = append(errs, fmt.Errorf("failed unmarshalling bidder imp ext (err)%s", err.Error()))
continue
}

requestCopy.Imp = []openrtb.Imp{imp}
requestJSON, err := json.Marshal(requestCopy)
if err != nil {
errs = append(errs, err)
continue
}

requestData := &adapters.RequestData{
Method: "POST",
Uri: pa.Endpoint,
Body: requestJSON,
Headers: http.Header{
"TOKEN": []string{bidderImpExt.Token},
"Content-Type": []string{"application/json"},
},
}
requests = append(requests, requestData)
}

return requests, errs
}

/* MakeBids */

func getMediaTypeForBid(bid *openrtb.Bid) (openrtb_ext.BidType, error) {
if bid == nil {
return "", fmt.Errorf("the bid request object is nil")
}

var bidExt pangleBidExt
if err := json.Unmarshal(bid.Ext, &bidExt); err != nil {
return "", fmt.Errorf("invalid bid ext")
} else if bidExt.Pangle == nil || bidExt.Pangle.AdType == nil {
return "", fmt.Errorf("missing pangleExt/adtype in bid ext")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test for this condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

}

switch *bidExt.Pangle.AdType {
case 1:
return openrtb_ext.BidTypeBanner, nil
case 2:
return openrtb_ext.BidTypeBanner, nil
case 5:
return openrtb_ext.BidTypeNative, nil
case 7:
return openrtb_ext.BidTypeVideo, nil
case 8:
return openrtb_ext.BidTypeVideo, nil
}

return "", fmt.Errorf("unrecognized adtype in response")
}

func (p *pangleAdapter) MakeBids(request *openrtb.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if responseData.StatusCode == http.StatusNoContent {
return nil, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test for this condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

}

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}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add supplemental json tests for these status codes conditions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests for these status codes have been added


var response openrtb.BidResponse
if err := json.Unmarshal(responseData.Body, &response); err != nil {
return nil, []error{err}
}

var errs []error
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't you making one request per Imp? Why are you expecting a number of bids equal to the number of imps in the original openRTB request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right! thanks for pointing that out. it has been corrected

bidResponse.Currency = response.Cur
for _, seatBid := range response.SeatBid {
for _, bid := range seatBid.Bid {
mediaType, err := getMediaTypeForBid(&bid)
if err != nil {
errs = append(errs, err)
continue
}
b := &adapters.TypedBid{
Bid: &bid,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've been made aware of a bug with taking the address of a for loop variable due to some unexpected (IMHO) Go behavior. Please make a local copy of the bid first:

	for _, seatBid := range response.SeatBid {
		for _, bid := range seatBid.Bid {
			bid := bid
			
			mediaType, err := getMediaTypeForBid(&bid)
			if err != nil {
				errs = append(errs, err)
				continue
			}
			b := &adapters.TypedBid{
				Bid:     &bid,
				BidType: mediaType,
			}
			bidResponse.Bids = append(bidResponse.Bids, b)
		}
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

BidType: mediaType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}

return bidResponse, errs
}
22 changes: 22 additions & 0 deletions adapters/pangle/pangle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pangle

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) {
conf := config.Adapter{
Endpoint: "https://pangle.io/api/get_ads",
ExtraAdapterInfo: "{\"token\": \"123\"}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The adapter doesn't use the ExtraAdapterInfo field. Is this left over from an earlier design?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is. thanks for pointing that out

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

adapterstest.RunJSONBidderTest(t, "pangletest", bidder)
}
127 changes: 127 additions & 0 deletions adapters/pangle/pangletest/exemplary/app_banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"mockBidRequest": {
"id": "test-request-id",
"app": {
"bundle": "com.prebid"
},
"device": {
"ifa": "87857b31-8942-4646-ae80-ab9c95bf3fab"
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"bidder": {
"token": "123"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://pangle.io/api/get_ads",
"headers": {
"Content-Type": [
"application/json"
],
"TOKEN": [
"123"
]
},
"body": {
"id": "test-request-id",
"app": {
"bundle": "com.prebid"
},
"device": {
"ifa": "87857b31-8942-4646-ae80-ab9c95bf3fab"
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"adtype": 1,
"bidder": {
"token": "123"
},
"prebid": null
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "seat-id",
"bid": [
{
"id": "1",
"impid": "test-imp-id",
"adid": "11110126",
"price": 0.500000,
"adm": "some-test-ad",
"crid": "test-crid",
"h": 250,
"w": 300,
"ext": {
"pangle": {
"adtype": 1
}
}
}
]
}
],
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "1",
"impid": "test-imp-id",
"adid": "11110126",
"price": 0.5,
"adm": "some-test-ad",
"crid": "test-crid",
"w": 300,
"h": 250,
"ext": {
"pangle": {
"adtype": 1
}
}
},
"type": "banner"
}
]
}
]
}
Loading