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 7 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)
}
}
}
90 changes: 90 additions & 0 deletions adapters/smartx/smartx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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"
)

type adapter struct {
endpointURL string
}

func Builder(_ openrtb_ext.BidderName, config config.Adapter, _ config.Server) (adapters.Bidder, error) {
return &adapter{
endpointURL: config.Endpoint,
}, nil
}

func (a *adapter) MakeRequests(openRTBRequest *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) (requestsToBidder []*adapters.RequestData, errs []error) {
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")
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)
}
}

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

func (a *adapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
return nil, []error{err}
}

if adapters.IsResponseStatusCodeNoContent(responseData) {
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.

you might wanna reverse this. You should check for no content first before you check CheckResponseStatusCodeForErrors because CheckResponseStatusCodeForErrors function will return an error even if response from bidder is 204 which is StatusNoContent. So -

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


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))
if response.Cur != "" {
bidResponse.Currency = response.Cur
}

var errs []error

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

return bidResponse, errs
}
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"
}]
}
]
}
124 changes: 124 additions & 0 deletions adapters/smartx/smartxtest/exemplary/02-consent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"w": 300,
"h": 250
},
"ext": {
"prebid": {
"bidder": {
"smartx": {
"publisherId": "11986",
"tagId": "Nu68JuOWAvrbzoyrOR9a7A",
"siteId": "22860"
}
}
}
}
}
],
"user": {
"ext": {
"consent": "COvFyGBOvFyGBAbAAAENAPCAAOAAAAAAAAAAAEEUACCKAAA.IFoEUQQgAIQwgIwQABAEAAAAOIAACAIAAAAQAIAgEAACEAAAAAgAQBAAAAAAAGBAAgAAAAAAAFAAECAAAgAAQARAEQAAAAAJAAIAAgAAAYQEAAAQmAgBC3ZAYzUw"
}
}
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://localhost/prebid_server",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"w": 300,
"h": 250
},
"ext": {
"prebid": {
"bidder": {
"smartx": {
"publisherId": "11986",
"tagId": "Nu68JuOWAvrbzoyrOR9a7A",
"siteId": "22860"
}
}
}
}
}
],
"user": {
"ext": {
"consent": "COvFyGBOvFyGBAbAAAENAPCAAOAAAAAAAAAAAEEUACCKAAA.IFoEUQQgAIQwgIwQABAEAAAAOIAACAIAAAAQAIAgEAACEAAAAAgAQBAAAAAAAGBAAgAAAAAAAFAAECAAAgAAQARAEQAAAAAJAAIAAgAAAYQEAAAQmAgBC3ZAYzUw"
}
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "958",
"bid": [
{
"id": "7706636740145184841",
"impid": "test-imp-id",
"price": 0.5,
"adid": "29681110",
"adm": "some-test-ad",
"adomain": [
"https://advertiser.example.com"
],
"cid": "958",
"crid": "29681110",
"h": 250,
"w": 300,
"ext": {
"ix": {}
}
}
]
}
],
"bidid": "5778926625248726496",
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "7706636740145184841",
"impid": "test-imp-id",
"price": 0.5,
"adm": "some-test-ad",
"adid": "29681110",
"adomain": [
"https://advertiser.example.com"
],
"cid": "958",
"crid": "29681110",
"w": 300,
"h": 250,
"ext": {
"ix": {}
}
},
"type": "video"
}
]
}
]
}
Loading