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: Adagio #1907

Merged
merged 8 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
145 changes: 145 additions & 0 deletions adapters/adagio/adagio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package adagio

import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"github.com/mxmCherry/openrtb/v15/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"
"net/http"
)

// Builder builds a new instance of the Adagio 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
}

type adapter struct {
endpoint string
}

// MakeRequests prepares the HTTP requests which should be made to fetch bids.
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
json, err := json.Marshal(request)
if err != nil {
return nil, []error{err}
}

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")

if request.Device != nil {
if len(request.Device.IPv6) > 0 {
headers.Add("X-Forwarded-For", request.Device.IPv6)
}
if len(request.Device.IP) > 0 {
headers.Add("X-Forwarded-For", request.Device.IP)
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
}
}

if request.Test == 0 {
// Gzip the body
// Note: Gzipping could be handled natively later: https://github.com/prebid/prebid-server/issues/1812
var bodyBuf bytes.Buffer
gz := gzip.NewWriter(&bodyBuf)
_, err = gz.Write(json)
if err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: You can make the error checking inline:

if _, err := gz.Write(json); err == nil {
  if err := gz.Close(); err == nil {
    json = bodyBuf.Bytes()
    headers.Add("Content-Encoding", "gzip")
    // /!\ Go already sets the `Accept-Encoding: gzip` header. Never add it manually, or Go won't decompress the response.
    //headers.Add("Accept-Encoding", "gzip")
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I updated

err = gz.Close()
if err == nil {
json = bodyBuf.Bytes()
headers.Add("Content-Encoding", "gzip")
// /!\ Go already sets the `Accept-Encoding: gzip` header. Never add it manually, or Go won't decompress the response.
//headers.Add("Accept-Encoding", "gzip")
}
}
}

requestToBidder := &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: json,
Headers: headers,
}

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

const unexpectedStatusCodeFormat = "Unexpected status code: %d. Run with request.debug = 1 for more info"

// MakeBids unpacks the server's response into Bids.
func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, _ *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
switch response.StatusCode {
case http.StatusOK:
break
case 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.

Please consider adding JSON tests for status codes other than StatusOK as well in the supplemental directory

case http.StatusServiceUnavailable:
fallthrough
case http.StatusBadRequest:
fallthrough
case http.StatusUnauthorized:
fallthrough
case http.StatusForbidden:
err := &errortypes.BadInput{
Message: fmt.Sprintf(unexpectedStatusCodeFormat, response.StatusCode),
}
return nil, []error{err}
default:
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf(unexpectedStatusCodeFormat, response.StatusCode),
}
return nil, []error{err}
}

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

bidsCapacity := len(internalRequest.Imp)
errs := make([]error, 0, bidsCapacity)
bidderResponse := adapters.NewBidderResponseWithBidsCapacity(bidsCapacity)
var typedBid *adapters.TypedBid
for _, seatBid := range openRTBBidderResponse.SeatBid {
for _, bid := range seatBid.Bid {
activeBid := bid
bidType, err := getMediaTypeForImp(activeBid.ImpID, internalRequest.Imp)
if err != nil {
errs = append(errs, err)
continue
}

typedBid = &adapters.TypedBid{Bid: &activeBid, BidType: bidType}
bidderResponse.Bids = append(bidderResponse.Bids, typedBid)
}
}

return bidderResponse, nil
}

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
} else if imp.Video != nil {
return openrtb_ext.BidTypeVideo, nil
}

}
}

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

import (
"encoding/json"
"fmt"
"github.com/mxmCherry/openrtb/v15/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/stretchr/testify/assert"
"net/http"
"testing"

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

func buildFakeBidRequest() openrtb2.BidRequest {
imp1 := openrtb2.Imp{
ID: "1",
Banner: &openrtb2.Banner{},
Ext: json.RawMessage(`{"bidder": {"organizationId": "1000", "site": "test-site"}}`),
}
imp2 := openrtb2.Imp{
ID: "2",
Banner: &openrtb2.Banner{},
Video: &openrtb2.Video{},
Ext: json.RawMessage(`{"bidder": {"organizationId": "1000", "site": "test-site"}}`),
}
imp3 := openrtb2.Imp{
ID: "3",
Native: &openrtb2.Native{},
Ext: json.RawMessage(`{"bidder": {"organizationId": "1000", "site": "test-site"}}`),
}

fakeBidRequest := openrtb2.BidRequest{
ID: "abc-123",
Imp: []openrtb2.Imp{imp1, imp2, imp3},
}

return fakeBidRequest
}

func buildFakeRequestData(t *testing.T, fakeBidRequest openrtb2.BidRequest) adapters.RequestData {
imps, err := json.Marshal(fakeBidRequest.Imp)
if err != nil {
t.Fatalf("Json marshal failed: %v", err)
}

fakeRequestData := adapters.RequestData{
Method: "POST",
Body: []byte(fmt.Sprintf(`{"imp": %v}`, imps)),
}

return fakeRequestData
}

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderAdagio, config.Adapter{
Endpoint: "http://localhost/prebid_server"})

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

adapterstest.RunJSONBidderTest(t, "adagiotest", bidder)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It may not be a bad idea to have a multiformat test. And a test or two to exercise that bad response paths in MakeBids().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi @hhhjort, thanks for your review. I added some tests. Let me know if it is ok you.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason you hardcoded some tests in by hand rather than adding more JSON tests?

}

func TestMakeRequests(t *testing.T) {
fakeBidRequest := buildFakeBidRequest()
fakeBidRequest.Test = 1

bidder, buildErr := Builder(openrtb_ext.BidderAdagio, config.Adapter{
Endpoint: "http://localhost/prebid_server"})

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

requestData, err := bidder.MakeRequests(&fakeBidRequest, nil)

assert.Nil(t, err)
assert.Equal(t, 1, len(requestData))

body := &openrtb2.BidRequest{}
_ = json.Unmarshal(requestData[0].Body, body)
Copy link
Contributor

Choose a reason for hiding this comment

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

I know the likelihood of an Unmarshal error here is quite low but still might be a good idea to have an assert.NoError check after the unmarshal rather than skipping the error check completely.


assert.Equal(t, 3, len(body.Imp))
}

func TestAdapter_MakeRequestsGzip(t *testing.T) {
fakeBidRequest := buildFakeBidRequest()

bidder, buildErr := Builder(openrtb_ext.BidderAdagio, config.Adapter{
Endpoint: "http://localhost/prebid_server"})

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

requestData, _ := bidder.MakeRequests(&fakeBidRequest, nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please consider adding a NoError assertion here rather than skipping the error check

assert.Equal(t, []string([]string{"gzip"}), requestData[0].Headers["Content-Encoding"])
}

func TestMakeBids(t *testing.T) {
fakeBidRequest := buildFakeBidRequest()
fakeRequestData := buildFakeRequestData(t, fakeBidRequest)

bidder, buildErr := Builder(openrtb_ext.BidderAdagio, config.Adapter{
Endpoint: "http://localhost/prebid_server"})

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

fakeBidResponse := &adapters.ResponseData{}

fakeBidResponse.StatusCode = http.StatusForbidden
_, err := bidder.MakeBids(&fakeBidRequest, &fakeRequestData, fakeBidResponse)
assert.NotNil(t, err)

fakeBidResponse.StatusCode = http.StatusInternalServerError
_, err = bidder.MakeBids(&fakeBidRequest, &fakeRequestData, fakeBidResponse)
assert.NotNil(t, err)

fakeBidResponse.StatusCode = http.StatusOK
fakeBidResponse.Body = []byte(`"imp": 1`)
_, err = bidder.MakeBids(&fakeBidRequest, &fakeRequestData, fakeBidResponse)
assert.NotNil(t, err)

fakeBidResponse.Body = []byte(`{"id": "abc", "seatbid": [{"bid": [{"id": "internal-id", "impid": "1", "banner": {}}]}]}`)
re, err := bidder.MakeBids(&fakeBidRequest, &fakeRequestData, fakeBidResponse)
assert.Equal(t, 1, len(re.Bids))

}
Loading