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: Brave #2396

Merged
merged 7 commits into from
Jan 31, 2023
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
164 changes: 164 additions & 0 deletions adapters/brave/brave.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package brave

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

"github.com/mxmCherry/openrtb/v16/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/macros"
"github.com/prebid/prebid-server/openrtb_ext"
)

type adapter struct {
endpoint *template.Template
}

// Builder builds a new instance of the Brave adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
uri, err := template.New("endpointTemplate").Parse(config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
}

bidder := &adapter{
endpoint: uri,
}
return bidder, nil
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errors []error
var braveExt *openrtb_ext.ExtImpBrave
var err error

for i, imp := range request.Imp {
braveExt, err = a.getImpressionExt(&imp)
thebraveio marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
errors = append(errors, err)
break
}
request.Imp[i].Ext = nil
}
thebraveio marked this conversation as resolved.
Show resolved Hide resolved

if len(errors) > 0 {
return nil, errors
}

reqJSON, 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")

url, err := a.buildEndpointURL(braveExt)
if err != nil {
return nil, []error{err}
}

return []*adapters.RequestData{{
Method: http.MethodPost,
Body: reqJSON,
Uri: url,
Headers: headers,
}}, nil
}

func (a *adapter) getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtImpBrave, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: "ext.bidder not provided",
}
}
var braveExt openrtb_ext.ExtImpBrave
if err := json.Unmarshal(bidderExt.Bidder, &braveExt); err != nil {
return nil, &errortypes.BadInput{
Message: "ext.bidder not provided",
}
}
return &braveExt, nil
}

func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtImpBrave) (string, error) {
endpointParams := macros.EndpointTemplateParams{PublisherID: params.PlacementID}
return macros.ResolveMacros(a.endpoint, endpointParams)
}

func (a *adapter) MakeBids(
receivedRequest *openrtb2.BidRequest,
bidderRequest *adapters.RequestData,
bidderResponse *adapters.ResponseData,
) (
*adapters.BidderResponse,
[]error,
) {

if bidderResponse.StatusCode == http.StatusNoContent {
return nil, []error{&errortypes.BadInput{Message: "No bid"}}
}

if bidderResponse.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", bidderResponse.StatusCode),
}}
}

if bidderResponse.StatusCode == http.StatusServiceUnavailable {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Service Unavailable. Status Code: [ %d ] ", bidderResponse.StatusCode),
}}
}

if bidderResponse.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Something went wrong, please contact your Account Manager. Status Code: [ %d ] ", bidderResponse.StatusCode),
}}
}

var bidResponse openrtb2.BidResponse
if err := json.Unmarshal(bidderResponse.Body, &bidResponse); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: "Bad Server Response",
}}
}

if len(bidResponse.SeatBid) == 0 {
return nil, []error{&errortypes.BadServerResponse{
Message: "Empty SeatBid array",
}}
}

bidResponseFinal := adapters.NewBidderResponseWithBidsCapacity(len(bidResponse.SeatBid[0].Bid))
sb := bidResponse.SeatBid[0]

for _, bid := range sb.Bid {
bidResponseFinal.Bids = append(bidResponseFinal.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: getMediaTypeForImp(bid.ImpID, receivedRequest.Imp),
})
}
return bidResponseFinal, nil
}

func getMediaTypeForImp(impId string, imps []openrtb2.Imp) openrtb_ext.BidType {
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impId {
if imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
} else if imp.Native != nil {
mediaType = openrtb_ext.BidTypeNative
}
return mediaType
Copy link
Contributor

Choose a reason for hiding this comment

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

Although many adapters do this, we consider it an anti-pattern. Does your bidding server return the media type in the response? Such as the new OpenRTB 2.6 mtype field?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, we using 2.5 and response with adm without mtype, that's why we have to recognise type by using req parameter

}
}
return mediaType
}
28 changes: 28 additions & 0 deletions adapters/brave/brave_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package brave

import (
"testing"

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderBrave, config.Adapter{
Endpoint: "http://point.braveglobal.tv/?t=3&partner={{.PublisherID}}"})
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 using an obviously fake url for testing. There is no need for this to match the real endpoint and might be harder to maintain if you need to change it in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh ok, got it , thanks


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

adapterstest.RunJSONBidderTest(t, "bravetest", bidder)
}

func TestEndpointTemplateMalformed(t *testing.T) {
_, buildErr := Builder(openrtb_ext.BidderBrave, config.Adapter{
Endpoint: "{{Malformed}}"})

assert.Error(t, buildErr)
}
140 changes: 140 additions & 0 deletions adapters/brave/bravetest/exemplary/banner-app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"mockBidRequest": {
"id": "request-id",
"app": {
"publisher": {
"id": "123456789"
},
"cat": [
"IAB9-1"
],
"bundle": "com.app.test",
"name": "Test App",
"id": "12345678"
},
"device": {
"ua": "useragent",
"ip": "100.100.100.100",
"language": "en"
},
"tmax": 1000,
"user": {
"id": "some-user"
},
"imp": [
{
"id": "impression-id",
"tagid": "tid",
"banner": {
"w":320,
"h":50
},
"ext": {
"bidder": {
"placementId": "f897beb0daba0253d8e59a098eef9311"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"headers": {
"Content-Type": [
"application/json;charset=utf-8"
],
"Accept": [
"application/json"
]
},
"uri": "http://point.braveglobal.tv/?t=3&partner=f897beb0daba0253d8e59a098eef9311",
"body": {
"id": "request-id",
"device": {
"ua": "useragent",
"ip": "100.100.100.100",
"language": "en"
},
"imp": [
{
"id": "impression-id",
"banner": {
"w":320,
"h":50
},
"tagid": "tid"
}
],
"app": {
"publisher": {
"id": "123456789"
},
"cat": [
"IAB9-1"
],
"bundle": "com.app.test",
"name": "Test App",
"id": "12345678"
},
"user": {
"id": "some-user"
},
"tmax": 1000
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "resp-id",
"seatbid": [
{
"bid": [
{
"id": "123456789",
"impid": "impression-id",
"price": 2,
"adm": "adm code",
"adomain": [
"testdomain.com"
],
"crid": "100",
"w":320,
"h":50
}
],
"type": "banner",
"seat": "brave"
}
],
"cur": "USD",
"ext": {
"responsetimemillis": {
"brave": 120
},
"tmaxrequest": 1000
}
}
}
}
],
"expectedBidResponses": [
{
"bids": [{
"bid": {
"id": "123456789",
"impid": "impression-id",
"price": 2,
"adm": "adm code",
"adomain": [
"testdomain.com"
],
"crid": "100",
"w":320,
"h":50
},
"type": "banner"
}]
}
]
}
Loading