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: Loyal #3586

Merged
merged 12 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
159 changes: 159 additions & 0 deletions adapters/loyal/loyal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package loyal

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

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)

type adapter struct {
endpoint string
}

type reqBodyExt struct {
LoyalBidderExt 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
}
onkarvhanumante marked this conversation as resolved.
Show resolved Hide resolved

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var err error
Copy link
Contributor

Choose a reason for hiding this comment

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

since scope of err is within the loop, no need to err outside for loop on line 42

line 48 and 52 could be modified as below:


		if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
			errs = append(errs, err)
			continue
		}
		if err := json.Unmarshal(bidderExt.Bidder, &loyalExt); err != nil {
			errs = append(errs, err)
			continue
		}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

var errs []error
var adapterRequests []*adapters.RequestData

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

var bidderExt adapters.ExtImpBidder
var loyalExt openrtb_ext.ImpExtLoyal

if err = json.Unmarshal(imp.Ext, &bidderExt); err != nil {
errs = append(errs, err)
continue
}
if err = json.Unmarshal(bidderExt.Bidder, &loyalExt); err != nil {
errs = append(errs, err)
continue
}

impExt := reqBodyExt{LoyalBidderExt: reqBodyExtBidder{}}

if loyalExt.PlacementID != "" {
impExt.LoyalBidderExt.PlacementID = loyalExt.PlacementID
impExt.LoyalBidderExt.Type = "publisher"
} else if loyalExt.EndpointID != "" {
impExt.LoyalBidderExt.EndpointID = loyalExt.EndpointID
impExt.LoyalBidderExt.Type = "network"
}

finalyImpExt, err := json.Marshal(impExt)
if err != nil {
errs = append(errs, err)
continue
}

reqCopy.Imp[0].Ext = finalyImpExt

adapterReq, err := a.makeRequest(&reqCopy)
if err != nil {
errs = append(errs, err)
continue
}

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

if len(adapterRequests) == 0 {
errs = append(errs, errors.New("found no valid impressions"))
return nil, errs
}

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,
}, nil
}

func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if adapters.IsResponseStatusCodeNoContent(responseData) {
return nil, nil
}

if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
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))
if len(response.Cur) != 0 {
bidResponse.Currency = response.Cur
}

for _, seatBid := range response.SeatBid {
for i := range seatBid.Bid {
bidType, err := getBidMediaType(&seatBid.Bid[i])
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 getBidMediaType(bid *openrtb2.Bid) (openrtb_ext.BidType, error) {
var extBid openrtb_ext.ExtBid
err := json.Unmarshal(bid.Ext, &extBid)
if err != nil {
return "", fmt.Errorf("unable to deserialize imp %v bid.ext", bid.ImpID)
}
teqblaze marked this conversation as resolved.
Show resolved Hide resolved

if extBid.Prebid == nil {
return "", fmt.Errorf("imp %v with unknown media type", bid.ImpID)
}

return extBid.Prebid.Type, nil
teqblaze marked this conversation as resolved.
Show resolved Hide resolved
}
20 changes: 20 additions & 0 deletions adapters/loyal/loyal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package loyal

import (
"testing"

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderEmtv, config.Adapter{
Endpoint: "http://example.com/pserver"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice use of an obviously fake testing endpoint. Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

replaced

Copy link
Contributor

Choose a reason for hiding this comment

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

This was a compliment, not a critique. Sorry if you interpreted this as sarcasm. We prefer the use of a fake testing endpoint in unit tests. Feel free to revert commit cf18f0e

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


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

adapterstest.RunJSONBidderTest(t, "loyaltest", bidder)
}
133 changes: 133 additions & 0 deletions adapters/loyal/loyaltest/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": "loyal"
}
],
"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
Loading