-
Notifications
You must be signed in to change notification settings - Fork 741
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: SmartHub #1932
Merged
Merged
New Adapter: SmartHub #1932
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2573576
adding SmartHub adapter
SmartHubSolutions e5d4d33
fix typo
SmartHubSolutions e0b7f6f
naming according to docs
SmartHubSolutions e713bba
removing import aliases
SmartHubSolutions 7a6c090
removing named return params
SmartHubSolutions 0f8c117
removing extra check on empty []Imp
SmartHubSolutions 44ca904
fixing validation schema to avoid the situation when param is an empt…
SmartHubSolutions 9ec73d6
using SmartHub subdomain as part of the host for building endpoint URL
SmartHubSolutions File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package smarthub | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/openrtb_ext" | ||
) | ||
|
||
var validParams = []string{ | ||
`{"partnerName":"partnertest", "seat":"9Q20EdGxzgWdfPYShScl", "token":"eKmw6alpP3zWQhRCe3flOpz0wpuwRFjW"}`, | ||
} | ||
|
||
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.BidderSmartHub, json.RawMessage(validParam)); err != nil { | ||
t.Errorf("Schema rejected smarthub params: %s", validParam) | ||
} | ||
} | ||
} | ||
|
||
var invalidParams = []string{ | ||
``, | ||
`null`, | ||
`true`, | ||
`5`, | ||
`[]`, | ||
`{}`, | ||
`{"anyparam": "anyvalue"}`, | ||
`{"partnerName":"partnertest"}`, | ||
`{"seat":"9Q20EdGxzgWdfPYShScl"}`, | ||
`{"token":"Y9Evrh40ejsrCR4EtidUt1cSxhJsz8X1"}`, | ||
`{"seat":"9Q20EdGxzgWdfPYShScl", "token":"alNYtemWggraDVbhJrsOs9pXc3Eld32E"}`, | ||
`{"partnerName":"partnertest", "token":"LNywdP2ebX5iETF8gvBeEoB6Cam64eeq"}`, | ||
`{"partnerName":"partnertest", "seat":"9Q20EdGxzgWdfPYShScl"}`, | ||
`{"partnerName":"", "seat":"", "token":""}`, | ||
`{"partnerName":"", "seat":"9Q20EdGxzgWdfPYShScl", "token":"alNYtemWggraDVbhJrsOs9pXc3Eld32E"}`, | ||
`{"partnerName":"partnertest", "seat":"9Q20EdGxzgWdfPYShScl", "token":""}`, | ||
`{"partnerName":"partnertest", "seat":"", "token":"alNYtemWggraDVbhJrsOs9pXc3Eld32E"}`, | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package smarthub | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"text/template" | ||
|
||
"github.com/mxmCherry/openrtb/v15/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" | ||
) | ||
|
||
const ( | ||
ADAPTER_VER = "1.0.0" | ||
) | ||
|
||
type adapter struct { | ||
endpoint template.Template | ||
} | ||
|
||
type bidExt struct { | ||
MediaType string `json:"mediaType"` | ||
} | ||
|
||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) { | ||
template, 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: *template, | ||
} | ||
|
||
return bidder, nil | ||
} | ||
|
||
func (a *adapter) getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtSmartHub, error) { | ||
var bidderExt adapters.ExtImpBidder | ||
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { | ||
return nil, &errortypes.BadInput{ | ||
Message: "Bidder extension not provided or can't be unmarshalled", | ||
} | ||
} | ||
|
||
var smarthubExt openrtb_ext.ExtSmartHub | ||
if err := json.Unmarshal(bidderExt.Bidder, &smarthubExt); err != nil { | ||
return nil, &errortypes.BadInput{ | ||
Message: "Error while unmarshaling bidder extension", | ||
} | ||
} | ||
|
||
return &smarthubExt, nil | ||
} | ||
|
||
func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtSmartHub) (string, error) { | ||
endpointParams := macros.EndpointTemplateParams{ | ||
Host: params.PartnerName, | ||
AccountID: params.Seat, | ||
SourceId: params.Token, | ||
} | ||
return macros.ResolveMacros(a.endpoint, endpointParams) | ||
} | ||
|
||
func (a *adapter) MakeRequests( | ||
openRTBRequest *openrtb2.BidRequest, | ||
reqInfo *adapters.ExtraRequestInfo, | ||
) ( | ||
[]*adapters.RequestData, | ||
[]error, | ||
) { | ||
var smarthubExt *openrtb_ext.ExtSmartHub | ||
smarthubExt, err := a.getImpressionExt(&(openRTBRequest.Imp[0])) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
url, err := a.buildEndpointURL(smarthubExt) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
reqJSON, err := json.Marshal(openRTBRequest) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
headers.Add("Accept", "application/json") | ||
headers.Add("Prebid-Adapter-Ver", ADAPTER_VER) | ||
|
||
return []*adapters.RequestData{{ | ||
Method: http.MethodPost, | ||
Body: reqJSON, | ||
Uri: url, | ||
Headers: headers, | ||
}}, nil | ||
} | ||
|
||
func (a *adapter) MakeBids( | ||
openRTBRequest *openrtb2.BidRequest, | ||
requestToBidder *adapters.RequestData, | ||
bidderRawResponse *adapters.ResponseData, | ||
) ( | ||
*adapters.BidderResponse, | ||
[]error, | ||
) { | ||
if bidderRawResponse.StatusCode == http.StatusNoContent { | ||
return nil, nil | ||
} | ||
|
||
if bidderRawResponse.StatusCode == http.StatusBadRequest { | ||
return nil, []error{&errortypes.BadInput{ | ||
Message: fmt.Sprintf("Bad Request. %s", string(bidderRawResponse.Body)), | ||
}} | ||
} | ||
|
||
if bidderRawResponse.StatusCode == http.StatusServiceUnavailable { | ||
return nil, []error{&errortypes.BadInput{ | ||
Message: "Bidder unavailable. Please contact the bidder support.", | ||
}} | ||
} | ||
|
||
if bidderRawResponse.StatusCode != http.StatusOK { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("Status Code: [ %d ] %s", bidderRawResponse.StatusCode, string(bidderRawResponse.Body)), | ||
}} | ||
} | ||
|
||
responseBody := bidderRawResponse.Body | ||
var bidResp openrtb2.BidResponse | ||
if err := json.Unmarshal(responseBody, &bidResp); err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
if len(bidResp.SeatBid) == 0 { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: "Array SeatBid cannot be empty", | ||
}} | ||
} | ||
|
||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(1) | ||
|
||
bids := bidResp.SeatBid[0].Bid | ||
|
||
if len(bids) == 0 { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: "Array SeatBid[0].Bid cannot be empty", | ||
}} | ||
} | ||
|
||
bid := bids[0] | ||
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 it always only one bid in response? 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. Yes, we don't support more than one bid in response |
||
|
||
var bidExt bidExt | ||
var bidType openrtb_ext.BidType | ||
|
||
if err := json.Unmarshal(bid.Ext, &bidExt); err != nil { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: "Field BidExt is required", | ||
}} | ||
} | ||
|
||
bidType, err := getBidType(bidExt) | ||
|
||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
Bid: &bid, | ||
BidType: bidType, | ||
}) | ||
return bidResponse, nil | ||
} | ||
|
||
func getBidType(ext bidExt) (openrtb_ext.BidType, error) { | ||
return openrtb_ext.ParseBidType(ext.MediaType) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package smarthub | ||
|
||
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.BidderSmartHub, config.Adapter{ | ||
Endpoint: "http://{{.Host}}-prebid.smart-hub.io/?seat={{.AccountID}}&token={{.SourceId}}"}) | ||
|
||
assert.NoError(t, buildErr) | ||
adapterstest.RunJSONBidderTest(t, "smarthubtest", bidder) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I see you pass all three parameters to your URL. Do you need all three to NOT be empty? As of right now, you marked them as
required
insidestatic/bidder-params/smarthub.json
but they can still be zero-lenght strings. In case you need that any or all three of your parameters to not be empty you could add theminLength
property like:Without the additions shown above, an all empty parameter
req.Imp[i].Ext
would be allowed. In other words, for the moment, a test case like this is currently passing: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.
If that's what you intended, it's fine
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.
Yes, thanks a lot for pointing the mistake! All three parameters mustn't be an empty string. I've fixed this in
static/bidder-params/smarthub.json
and also added possible cases to test intoinvalidParams