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: Rise #2815

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
120 changes: 120 additions & 0 deletions adapters/rise/rise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package rise

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

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

// adapter is a Rise implementation of the adapters.Bidder interface.
type adapter struct {
endpointURL string
}

func Builder(_ openrtb_ext.BidderName, config config.Adapter, _ config.Server) (adapters.Bidder, error) {
onkarvhanumante marked this conversation as resolved.
Show resolved Hide resolved
return &adapter{
endpointURL: config.Endpoint,
}, nil
}

// MakeRequests prepares the HTTP requests which should be made to fetch bids.
func (a *adapter) MakeRequests(openRTBRequest *openrtb2.BidRequest, _ *adapters.ExtraRequestInfo) (requestsToBidder []*adapters.RequestData, errs []error) {
publisherID, err := extractPublisherID(openRTBRequest)
if err != nil {
errs = append(errs, fmt.Errorf("extractPublisherID: %w", err))
return nil, errs
}

openRTBRequestJSON, err := json.Marshal(openRTBRequest)
if err != nil {
errs = append(errs, fmt.Errorf("marshal bidRequest: %w", err))
return nil, errs
}

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

return append(requestsToBidder, &adapters.RequestData{
Method: http.MethodPost,
Uri: a.endpointURL + "?publisher_id=" + publisherID,
Body: openRTBRequestJSON,
Headers: headers,
}), nil
}

// MakeBids unpacks the server's response into Bids.
func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *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}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Checking response status code this way is correct but you could also make use of the following code -

if httputil.IsResponseStatusCodeNoContent(response) {
		return nil, nil
	}

	if err := httputil.CheckResponseStatusCodeForErrors(response); err != nil {
		return nil, []error{err}
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, updated.


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

var errs []error

for _, seatBid := range response.SeatBid {
for i, bid := range seatBid.Bid {
bidType, err := getMediaTypeForBid(bid)
if err != nil {
errs = append(errs, err)
continue
}

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

return bidResponse, errs
}

func extractPublisherID(openRTBRequest *openrtb2.BidRequest) (string, error) {
var err error
for _, imp := range openRTBRequest.Imp {
var bidderExt adapters.ExtImpBidder
if err = json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return "", fmt.Errorf("unmarshal bidderExt: %w", err)
}

var impExt openrtb_ext.ImpExtRise
if err = json.Unmarshal(bidderExt.Bidder, &impExt); err != nil {
return "", fmt.Errorf("unmarshal ImpExtRise: %w", err)
}

if impExt.PublisherID != "" {
return strings.TrimSpace(impExt.PublisherID), nil
}
}

return "", errors.New("no publisherID supplied")
}

func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
switch bid.MType {
case openrtb2.MarkupBanner:
return openrtb_ext.BidTypeBanner, nil
case openrtb2.MarkupVideo:
return openrtb_ext.BidTypeVideo, nil
default:
Copy link
Contributor

Choose a reason for hiding this comment

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

In rise.yaml, you have defined that your bidder will be supporting just Banner and Video. I believe you might wanna error out if bidder response returns Audio or Native as bid type. So here is the code -

switch bid.MType {
	case openrtb2.MarkupBanner:
		return openrtb_ext.BidTypeBanner, nil
	case openrtb2.MarkupVideo:
		return openrtb_ext.BidTypeVideo, nil
	default:
                 return "", fmt.Errorf("unsupported MType %d", bid.MType)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, updated

return "", fmt.Errorf("unsupported MType %d", bid.MType)
}
}
24 changes: 24 additions & 0 deletions adapters/rise/rise_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package rise

import (
"testing"

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

const testsDir = "risetest"
const testsBidderEndpoint = "http://localhost/prebid_server"

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(
openrtb_ext.BidderRise,
config.Adapter{Endpoint: testsBidderEndpoint},
config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}

adapterstest.RunJSONBidderTest(t, testsDir, bidder)
}
211 changes: 211 additions & 0 deletions adapters/rise/risetest/exemplary/banner-and-video-app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 300
}
]
},
"ext": {
"bidder": {
"publisher_id": "72721",
"path": "mvo",
"zone": "1r"
}
}
},
{
"id": "test-imp-video-id",
"video": {
"mimes": ["video/mp4"],
"minduration": 1,
"maxduration": 2,
"protocols": [1, 2, 5],
"w": 1020,
"h": 780,
"startdelay": 1,
"placement": 1,
"playbackmethod": [2],
"delivery": [1],
"api": [1, 2, 3, 4]
},
"ext": {
"bidder": {
"publisher_id": "72721",
"path": "mvo",
"zone": "1r"
}
}
}
],
"app": {
"id": "agltb3B1Yi1pbmNyDAsSA0FwcBiJkfIUDA",
"name": "Yahoo Weather",
"bundle": "12345",
"storeurl": "https://itunes.apple.com/id628677149",
"cat": ["IAB15", "IAB15-10"],
"ver": "1.0.2",
"publisher": {
"id": "1"
}
},
"device": {
"dnt": 0,
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1 like Mac OS X) AppleWebKit / 534.46(KHTML, like Gecko) Version / 5.1 Mobile / 9 A334 Safari / 7534.48 .3",
"ip": "123.145.167.189",
"ifa": "AA000DFE74168477C70D291f574D344790E0BB11",
"carrier": "VERIZON",
"language": "en",
"make": "Apple",
"model": "iPhone",
"os": "iOS",
"osv": "6.1",
"js": 1,
"connectiontype": 3,
"devicetype": 1
}
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://localhost/prebid_server?publisher_id=72721",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 300
}
]
},
"ext": {
"bidder": {
"publisher_id": "72721",
"zone": "1r",
"path": "mvo"
}
}
},
{
"id": "test-imp-video-id",
"video": {
"mimes": ["video/mp4"],
"minduration": 1,
"maxduration": 2,
"protocols": [1, 2, 5],
"w": 1020,
"h": 780,
"startdelay": 1,
"placement": 1,
"playbackmethod": [2],
"delivery": [1],
"api": [1, 2, 3, 4]
},
"ext": {
"bidder": {
"publisher_id": "72721",
"zone": "1r",
"path": "mvo"
}
}
}
],
"app": {
"id": "agltb3B1Yi1pbmNyDAsSA0FwcBiJkfIUDA",
"name": "Yahoo Weather",
"bundle": "12345",
"storeurl": "https://itunes.apple.com/id628677149",
"cat": ["IAB15", "IAB15-10"],
"ver": "1.0.2",
"publisher": {
"id": "1"
}
},
"device": {
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1 like Mac OS X) AppleWebKit / 534.46(KHTML, like Gecko) Version / 5.1 Mobile / 9 A334 Safari / 7534.48 .3",
"ip": "123.145.167.189",
"devicetype": 1,
"make": "Apple",
"model": "iPhone",
"os": "iOS",
"osv": "6.1",
"js": 1,
"dnt": 0,
"language": "en",
"carrier": "VERIZON",
"connectiontype": 3,
"ifa": "AA000DFE74168477C70D291f574D344790E0BB11"
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "958",
"bid": [
{
"id": "7706636740145184841",
"impid": "test-imp-video-id",
"price": 0.5,
"adid": "29681110",
"adm": "some-test-ad",
"adomain": ["yahoo.com"],
"cid": "958",
"crid": "29681110",
"h": 576,
"w": 1024,
"mtype": 2
}
]
}
],
"bidid": "5778926625248726496",
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"bids": [
{
"bid": {
"id": "7706636740145184841",
"impid": "test-imp-video-id",
"price": 0.5,
"adm": "some-test-ad",
"adid": "29681110",
"adomain": ["yahoo.com"],
"cid": "958",
"crid": "29681110",
"w": 1024,
"h": 576,
"mtype": 2
},
"type": "video"
}
]
}
]
}
Loading