Skip to content

Commit

Permalink
New Adapter: Trustedstack
Browse files Browse the repository at this point in the history
  • Loading branch information
BudhirajaChinmay committed Apr 9, 2024
1 parent a35a668 commit b4a6ab3
Show file tree
Hide file tree
Showing 17 changed files with 1,091 additions and 0 deletions.
54 changes: 54 additions & 0 deletions adapters/trustedstack/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package trustedstack

import (
"encoding/json"
"testing"

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

// This file actually intends to test static/bidder-params/trustedstack.json
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.BidderTrustedstack, json.RawMessage(validParam)); err != nil {
t.Errorf("Schema rejected trustedstack params: %s", validParam)
}
}
}

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.BidderTrustedstack, json.RawMessage(invalidParam)); err == nil {
t.Errorf("Schema allowed unexpected params: %s", invalidParam)
}
}
}

var validParams = []string{
`{"cid":"123", "crid":"1234"}`,
}

var invalidParams = []string{
``,
`null`,
`true`,
`5`,
`4.2`,
`[]`,
`{}`,
`{"cid":"", "crid":""}`,
`{"cid":"only cid is present"}`,
`{"crid":"only crid is present"}`,
`{"ccid":"123","ccrid":"123"}`,
`{"aid":123, "placementId":"123", "siteId":"321"}`,
}
121 changes: 121 additions & 0 deletions adapters/trustedstack/trustedstack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package trustedstack

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

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/errortypes"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)

type adapter struct {
endpoint string
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var errs []error

reqJson, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}

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

return []*adapters.RequestData{{
Method: "POST",
Uri: a.endpoint,
Body: reqJson,
Headers: headers,
}}, errs
}

func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var errs []error

if response.StatusCode == http.StatusNoContent {
return nil, nil
}

if response.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}

if response.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode),
}}
}

var bidResp openrtb2.BidResponse

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

bidResponse := adapters.NewBidderResponse()

for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
if err != nil {
errs = append(errs, err)
} else {
b := &adapters.TypedBid{
Bid: &sb.Bid[i],
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
}
return bidResponse, errs
}

// Builder builds a new instance of the Trustedstack adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
url := buildEndpoint(config.Endpoint, config.ExtraAdapterInfo)
return &adapter{
endpoint: url,
}, nil
}

func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner == nil && imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
}
return mediaType, nil
}
}

return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to find impression \"%s\" ", impID),
}
}

func buildEndpoint(mnetUrl, hostUrl string) string {

if len(hostUrl) == 0 {
return mnetUrl
}
urlObject, err := url.Parse(mnetUrl)
if err != nil {
return mnetUrl
}
values := urlObject.Query()
values.Add("src", hostUrl)
urlObject.RawQuery = values.Encode()
return urlObject.String()
}
31 changes: 31 additions & 0 deletions adapters/trustedstack/trustedstack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package trustedstack

import (
"testing"

"github.com/stretchr/testify/assert"

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderTrustedstack, config.Adapter{
Endpoint: "https://example.trustedstack.com/rtb/prebid",
ExtraAdapterInfo: "http://localhost:8080/extrnal_url",
}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}

adapterstest.RunJSONBidderTest(t, "trustedstacktest", bidder)
}

func TestEndpointTemplateMalformed(t *testing.T) {
_, buildErr := Builder(openrtb_ext.BidderTrustedstack, config.Adapter{
Endpoint: "{{Malformed}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

assert.Nil(t, buildErr)
}
83 changes: 83 additions & 0 deletions adapters/trustedstack/trustedstacktest/exemplary/multi-format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "1",
"banner": {
"format": [
{
"w": 320,
"h": 50
}
]
},
"video": {
"mimes": [
"video/mp4"
],
"protocols": [
2,
5
],
"w": 320,
"h": 480
},
"ext": {
"bidder": {
"cid": "8CUTSTCID",
"crid": "999999999"
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://example.trustedstack.com/rtb/prebid?src=http%3A%2F%2Flocalhost%3A8080%2Fextrnal_url",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "1",
"banner": {
"format": [
{
"w": 320,
"h": 50
}
]
},
"video": {
"mimes": [
"video/mp4"
],
"protocols": [
2,
5
],
"w": 320,
"h": 480
},
"ext": {
"bidder": {
"cid": "8CUTSTCID",
"crid": "999999999"
}
}
}
]
}
},

"mockResponse": {
"status": 204,
"body": ""
}
}
],

"expectedBidResponses": []
}
Loading

0 comments on commit b4a6ab3

Please sign in to comment.