Skip to content

Commit

Permalink
TypeA Rise adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha S committed Jun 1, 2023
1 parent 38870ad commit 63b2045
Show file tree
Hide file tree
Showing 14 changed files with 1,218 additions and 0 deletions.
134 changes: 134 additions & 0 deletions adapters/rise/rise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
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/errortypes"
"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) {
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 := a.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
}

func (a *adapter) extractPublisherID(openRTBRequest *openrtb2.BidRequest) (string, error) {
var (
bidderExt adapters.ExtImpBidder
impExt openrtb_ext.ImpExtRise
publisherID string
)

for _, imp := range openRTBRequest.Imp {
if err := json.Unmarshal(imp.Ext, &bidderExt); err == nil {
if err = json.Unmarshal(bidderExt.Bidder, &impExt); err == nil && impExt.PublisherID != "" {
publisherID = strings.TrimSpace(impExt.PublisherID)
break
}
}
}
if publisherID == "" {
return "", errors.New("no publisherID supplied")
}

return publisherID, 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 responseData.StatusCode == http.StatusNoContent {
return nil, nil
}

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

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, bid := range seatBid.Bid {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: getMediaTypeForBid(bid.ImpID, request.Imp),
})
}
}

return bidResponse, nil
}

func getMediaTypeForBid(impID string, imps []openrtb2.Imp) openrtb_ext.BidType {
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner != nil {
return openrtb_ext.BidTypeBanner
} else if imp.Video != nil {
return openrtb_ext.BidTypeVideo
}
}
}
return openrtb_ext.BidTypeBanner
}
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)
}
209 changes: 209 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,209 @@
{
"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
}
]
}
],
"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
},
"type": "video"
}
]
}
]
}
Loading

0 comments on commit 63b2045

Please sign in to comment.