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

SilverMob adapter #1561

Merged
merged 8 commits into from
Nov 11, 2020
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
55 changes: 55 additions & 0 deletions adapters/silvermob/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package silvermob

import (
"encoding/json"
"testing"

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

// TestValidParams makes sure that the silvermob schema accepts all imp.ext fields which we intend to support.
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.BidderSilverMob, json.RawMessage(validParam)); err != nil {
t.Errorf("Schema rejected silvermob params: %s", validParam)
}
}
}

// TestInvalidParams makes sure that the silvermob schema rejects all the imp.ext fields we don't support.
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.BidderSilverMob, json.RawMessage(invalidParam)); err == nil {
t.Errorf("Schema allowed unexpected params: %s", invalidParam)
}
}
}

var validParams = []string{
`{"zoneid": "16", "host": "us"}`,
`{"zoneid": "16", "host": "eu"}`,
}

var invalidParams = []string{
``,
`null`,
`true`,
`5`,
`4.2`,
`[]`,
`{}`,
`{"ZoneID": "asd", "Host": "123"}`,
`{}`,
`{"ZoneID": "asd"}`,
`{"Host": "111"}`,
hhhjort marked this conversation as resolved.
Show resolved Hide resolved
}
193 changes: 193 additions & 0 deletions adapters/silvermob/silvermob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package silvermob

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

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

type SilverMobAdapter struct {
endpoint template.Template
}

func NewSilverMobBidder(endpointTemplate string) *SilverMobAdapter {
template, err := template.New("endpointTemplate").Parse(endpointTemplate)
if err != nil {
return nil
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
}
return &SilverMobAdapter{endpoint: *template}
}

func GetHeaders(request *openrtb.BidRequest) *http.Header {
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
headers.Add("X-Openrtb-Version", "2.5")

if request.Device != nil {
if len(request.Device.UA) > 0 {
headers.Add("User-Agent", request.Device.UA)
}

if len(request.Device.IPv6) > 0 {
headers.Add("X-Forwarded-For", request.Device.IPv6)
}

if len(request.Device.IP) > 0 {
headers.Add("X-Forwarded-For", request.Device.IP)
}
}

return &headers
}

func (a *SilverMobAdapter) MakeRequests(
openRTBRequest *openrtb.BidRequest,
reqInfo *adapters.ExtraRequestInfo,
) (
requestsToBidder []*adapters.RequestData,
errs []error,
) {

var errors []error
var silvermobExt *openrtb_ext.ExtSilverMob
var err error

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

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

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

reqJSON, err := json.Marshal(openRTBRequest)
if err != nil {
return nil, []error{err}
}

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

func (a *SilverMobAdapter) getImpressionExt(imp *openrtb.Imp) (*openrtb_ext.ExtSilverMob, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
Message: "ext.bidder not provided",
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
}
}
var silvermobExt openrtb_ext.ExtSilverMob
if err := json.Unmarshal(bidderExt.Bidder, &silvermobExt); err != nil {
return nil, &errortypes.BadInput{
Message: "ext.bidder not provided",
hhhjort marked this conversation as resolved.
Show resolved Hide resolved
}
}
return &silvermobExt, nil
}

func (a *SilverMobAdapter) buildEndpointURL(params *openrtb_ext.ExtSilverMob) (string, error) {
endpointParams := macros.EndpointTemplateParams{ZoneID: params.ZoneID, Host: params.Host}
return macros.ResolveMacros(a.endpoint, endpointParams)
}

func (a *SilverMobAdapter) CheckResponseStatusCodes(response *adapters.ResponseData) error {
if response.StatusCode == http.StatusNoContent {
return &errortypes.BadInput{Message: "No bid response"}
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
}

if response.StatusCode == http.StatusBadRequest {
return &errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: [ %d ]", response.StatusCode),
}
}

if response.StatusCode == http.StatusServiceUnavailable {
return &errortypes.BadInput{
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
Message: fmt.Sprintf("Something went wrong, please contact your Account Manager. Status Code: [ %d ] ", response.StatusCode),
}
}

if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
return &errortypes.BadInput{
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
Message: fmt.Sprintf("Something went wrong, please contact your Account Manager. Status Code: [ %d ] ", response.StatusCode),
}
}

return nil
}

func (a *SilverMobAdapter) MakeBids(
openRTBRequest *openrtb.BidRequest,
requestToBidder *adapters.RequestData,
bidderRawResponse *adapters.ResponseData,
) (
bidderResponse *adapters.BidderResponse,
errs []error,
) {
httpStatusError := a.CheckResponseStatusCodes(bidderRawResponse)
if httpStatusError != nil {
return nil, []error{httpStatusError}
}

responseBody := bidderRawResponse.Body
var bidResp openrtb.BidResponse
if err := json.Unmarshal(responseBody, &bidResp); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: "Bad Server Response",
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
}}
}

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

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

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

func getMediaTypeForImp(impId string, imps []openrtb.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
hhhjort marked this conversation as resolved.
Show resolved Hide resolved
}
}
return mediaType
}
11 changes: 11 additions & 0 deletions adapters/silvermob/silvermob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package silvermob

import (
"testing"

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

func TestJsonSamples(t *testing.T) {
adapterstest.RunJSONBidderTest(t, "silvermobtest", NewSilverMobBidder("http://{{.Host}}.example.com/api/dsp/bid/{{.ZoneID}}"))
}
Loading