-
Notifications
You must be signed in to change notification settings - Fork 753
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
New Adapter: smartx #3109
Changes from 3 commits
8a87fea
bc6f73f
9bc44e2
d15a8f6
50d74b9
0616f85
7cd6376
1a66d09
5763e95
4fb973a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
} | ||
} |
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. | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit - please remove this comment. |
||
openRTBRequestJSON, err := json.MarshalIndent(openRTBRequest, "", " ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As of now,
|
||
} |
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) | ||
} |
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" | ||
}] | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
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.