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: smartx #3109

Merged
merged 10 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
58 changes: 58 additions & 0 deletions adapters/smartx/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package smartx

import (
"encoding/json"
"testing"

"github.com/prebid/prebid-server/openrtb_ext"
)

var validParams = []string{
`{"tagId":"Nu68JuOWAvrbzoyrOR9a7A", "publisherId":"11986", "siteId":"22860"}`,
`{"tagId":"Nu68JuOWAvrbzoyrOR9a7A", "publisherId":"11986", "appId":"22860"}`,
}

func TestValidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json-schemas. %v", err)
}

for _, validParam := range validParams {
if err := validator.Validate(openrtb_ext.BidderSmartx, json.RawMessage(validParam)); err != nil {
t.Errorf("Schema rejected smartx params: %s", validParam)
}
}
}

var invalidParams = []string{
``,
`null`,
`true`,
`5`,
`[]`,
`{}`,
`{"anyparam": "anyvalue"}`,
`{"tagId":"Nu68JuOWAvrbzoyrOR9a7A"}`,
`{"publisherId":"11986"}`,
`{"siteId":"22860"}`,
`{"appId":"22860"}`,
`{"tagId":"Nu68JuOWAvrbzoyrOR9a7A", "publisherId":"11986"}`,
`{"tagId":"Nu68JuOWAvrbzoyrOR9a7A", "siteId":"22860"}`,
`{"tagId":"Nu68JuOWAvrbzoyrOR9a7A", "appId":"22860"}`,
`{"publisherId":"11986", "appId":"22860"}`,
`{"publisherId":"11986", "appId":"22860"}`,
}

func TestInvalidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json-schemas. %v", err)
}

for _, invalidParam := range invalidParams {
if err := validator.Validate(openrtb_ext.BidderSmartHub, json.RawMessage(invalidParam)); err == nil {
t.Errorf("Schema allowed unexpected params: %s", invalidParam)
}
}
}
116 changes: 116 additions & 0 deletions adapters/smartx/smartx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package smartx

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

"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 implementation of the adapters.Bidder interface.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

type adapter struct {
endpointURL string
}

func Builder(_ openrtb_ext.BidderName, config config.Adapter, _ config.Server) (adapters.Bidder, error) {
//println("Builder smartx", config.Endpoint)
// initialize the adapter and return it
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick - I don't think you need to have this comment here. Please remove them

return &adapter{
endpointURL: config.Endpoint,
}, nil
}

// MakeRequests prepares the HTTP requests which should be made to fetch bids.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

func (a *adapter) MakeRequests(openRTBRequest *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) (requestsToBidder []*adapters.RequestData, errs []error) {
// parse the requests answer
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

openRTBRequestJSON, err := json.MarshalIndent(openRTBRequest, "", " ")
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to choose MarshalIndent instead of MarshalJson? I think - adding an indentation would slightly take performance hit. Requesting you to please use MarshalJson if possible.

Copy link
Contributor

Choose a reason for hiding this comment

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

thank you, debug leftover. adjusted

if err != nil {
// can't parse the request, this is an critical error
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

errs = append(errs, fmt.Errorf("marshal bidRequest: %w", err))
return nil, errs
}

// create the HEADER of the request
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

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

if openRTBRequest.Device != nil {
if openRTBRequest.Device.UA != "" {
headers.Set("User-Agent", openRTBRequest.Device.UA)
}

if openRTBRequest.Device.IP != "" {
headers.Set("Forwarded", "for="+openRTBRequest.Device.IP)
headers.Set("X-Forwarded-For", openRTBRequest.Device.IP)
}
}

// add the new request to the list
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

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

// MakeBids unpacks the server's response into Bids.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
// check HTTP status code 400 - Bad Request
// check HTTP status code NOT 200
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

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

// check HTTP status code 204 - No Content
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

if adapters.IsResponseStatusCodeNoContent(responseData) {
return nil, nil
}

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

if len(response.SeatBid) == 0 {
return nil, []error{errors.New("no bidders found in JSON response")}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
bidResponse.Currency = response.Cur
onkarvhanumante marked this conversation as resolved.
Show resolved Hide resolved

var errs []error

// loop the SeatBids
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

for _, seatBid := range response.SeatBid {
// loop the Bids
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

for i, bid := range seatBid.Bid {
// get the MType of the bid
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

bidType, err := getMediaTypeForBid(bid)

if err != nil {
// if an error occures add it to the errors list and continue
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

errs = append(errs, err)
continue
}

// add the response to the list
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - please remove this comment.

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

return bidResponse, errs
}

func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
return openrtb_ext.BidTypeVideo, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

the smartx adapter supports both video and native. This value should be dervied from openrtb2.Bid.MType field.

Copy link
Contributor

Choose a reason for hiding this comment

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

As of now, getMediaTypeForBid() only returns openrtb_ext.BidTypeVideo. Instead skimpily assign openrtb_ext.BidTypeVideo as BidType

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

}
24 changes: 24 additions & 0 deletions adapters/smartx/smartx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package smartx

import (
"testing"

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

const testsDir = "smartxtest"
const testsBidderEndpoint = "http://localhost/prebid_server"
onkarvhanumante marked this conversation as resolved.
Show resolved Hide resolved

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

adapterstest.RunJSONBidderTest(t, testsDir, bidder)
}
77 changes: 77 additions & 0 deletions adapters/smartx/smartxtest/exemplary/01-video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"mockBidRequest":{
"id":"test-request-id-video",
"imp":[
{
"id":"test-imp-id",
"video":{
"mimes":[
"video/mp4"
]
}
}
]
},
"httpCalls":[
{
"expectedRequest":{
"uri":"http://localhost/prebid_server",
"body":{
"id":"test-request-id-video",
"imp":[
{
"id":"test-imp-id",
"video":{
"mimes":[
"video/mp4"
]
}
}
]
}
},
"mockResponse":{
"status":200,
"body":{
"id":"test-request-id-video",
"seatbid":[
{
"seat":"smartadserver",
"bid":[
{
"id":"8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid":"test-imp-id-video",
"price":0.500000,
"adm":"some-test-ad",
"crid":"crid_10",
"h":576,
"w":1024,
"mtype":2
}
]
}
],
"cur":"EUR"
}
}
}
],
"expectedBidResponses":[
{
"bids":[{
"bid":{
"id":"8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid":"test-imp-id-video",
"price":0.500000,
"adm":"some-test-ad",
"crid":"crid_10",
"h":576,
"w":1024,
"mtype":2
},
"currency":"EUR",
"type": "video"
}]
}
]
}
Loading