-
Notifications
You must be signed in to change notification settings - Fork 752
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: pragnesh <[email protected]>
- Loading branch information
Showing
23 changed files
with
2,002 additions
and
0 deletions.
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,144 @@ | ||
package smrtconnect | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"text/template" | ||
|
||
"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/macros" | ||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
) | ||
|
||
type adapter struct { | ||
endpoint *template.Template | ||
} | ||
|
||
// Builder builds a new instance of the Smrtconnect adapter for the given bidder with the given config. | ||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (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) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
var requests []*adapters.RequestData | ||
|
||
requestCopy := *request | ||
for _, imp := range request.Imp { | ||
smrtconnectExt, err := getImpressionExt(&imp) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
url, err := a.buildEndpointURL(smrtconnectExt) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
requestCopy.Imp = []openrtb2.Imp{imp} | ||
requestJSON, err := json.Marshal(requestCopy) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
requestData := &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: url, | ||
Body: requestJSON, | ||
} | ||
requests = append(requests, requestData) | ||
} | ||
return requests, nil | ||
} | ||
|
||
func getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtSmrtconnect, error) { | ||
var bidderExt adapters.ExtImpBidder | ||
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil { | ||
return nil, &errortypes.BadInput{ | ||
Message: "ext.bidder not provided", | ||
} | ||
} | ||
var smrtconnectExt openrtb_ext.ExtSmrtconnect | ||
if err := json.Unmarshal(bidderExt.Bidder, &smrtconnectExt); err != nil { | ||
return nil, &errortypes.BadInput{ | ||
Message: "ext.bidder not provided", | ||
} | ||
} | ||
imp.Ext = nil | ||
return &smrtconnectExt, nil | ||
} | ||
|
||
func (a *adapter) buildEndpointURL(params *openrtb_ext.ExtSmrtconnect) (string, error) { | ||
endpointParams := macros.EndpointTemplateParams{SupplyId: params.SupplyId} | ||
return macros.ResolveMacros(a.endpoint, endpointParams) | ||
} | ||
|
||
func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
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{&errortypes.BadServerResponse{ | ||
Message: "Bad Server Response", | ||
}} | ||
} | ||
|
||
if len(response.SeatBid) == 0 { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: "Empty SeatBid array", | ||
}} | ||
} | ||
|
||
var bidErrs []error | ||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) | ||
bidResponse.Currency = response.Cur | ||
for _, seatBid := range response.SeatBid { | ||
for i := range seatBid.Bid { | ||
bidType, err := getBidType(seatBid.Bid[i]) | ||
if err != nil { | ||
// could not determinate media type, append an error and continue with the next bid. | ||
bidErrs = append(bidErrs, err) | ||
continue | ||
} | ||
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
Bid: &seatBid.Bid[i], | ||
BidType: bidType, | ||
}) | ||
} | ||
} | ||
return bidResponse, bidErrs | ||
} | ||
|
||
func getBidType(bid openrtb2.Bid) (openrtb_ext.BidType, error) { | ||
// determinate media type by bid response field mtype | ||
switch bid.MType { | ||
case openrtb2.MarkupBanner: | ||
return openrtb_ext.BidTypeBanner, nil | ||
case openrtb2.MarkupVideo: | ||
return openrtb_ext.BidTypeVideo, nil | ||
case openrtb2.MarkupAudio: | ||
return openrtb_ext.BidTypeAudio, nil | ||
case openrtb2.MarkupNative: | ||
return openrtb_ext.BidTypeNative, nil | ||
} | ||
|
||
return "", &errortypes.BadInput{ | ||
Message: fmt.Sprintf("Could not define media type for impression: %s", bid.ImpID), | ||
} | ||
} |
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,28 @@ | ||
package smrtconnect | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/v2/adapters/adapterstest" | ||
"github.com/prebid/prebid-server/v2/config" | ||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
bidder, buildErr := Builder(openrtb_ext.BidderSmrtconnect, config.Adapter{ | ||
Endpoint: "http://test.smrtconnect.com/openrtb2/auction?supply_id={{.SupplyId}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 196}) | ||
|
||
if buildErr != nil { | ||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
} | ||
|
||
adapterstest.RunJSONBidderTest(t, "smrtconnecttest", bidder) | ||
} | ||
|
||
func TestEndpointTemplateMalformed(t *testing.T) { | ||
_, buildErr := Builder(openrtb_ext.BidderSmrtconnect, config.Adapter{ | ||
Endpoint: "{{Malformed}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 196}) | ||
|
||
assert.Error(t, buildErr) | ||
} |
90 changes: 90 additions & 0 deletions
90
adapters/smrtconnect/smrtconnecttest/exemplary/audio-app.json
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,90 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"app": { | ||
"bundle": "com.prebid" | ||
}, | ||
"device": { | ||
"ifa":"ec943cb9-61ec-460f-a925-6489c3fcc4e3" | ||
}, | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"audio": { | ||
"mimes": ["audio/mp4"], | ||
"protocols": [9,10] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"supply_id": "1" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
|
||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://test.smrtconnect.com/openrtb2/auction?supply_id=1", | ||
"body": { | ||
"id": "test-request-id", | ||
"app": { | ||
"bundle": "com.prebid" | ||
}, | ||
"device": { | ||
"ifa":"ec943cb9-61ec-460f-a925-6489c3fcc4e3" | ||
}, | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"audio": { | ||
"mimes": ["audio/mp4"], | ||
"protocols": [9,10] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"seatbid": [ | ||
{ | ||
"seat": "smrtconnect", | ||
"bid": [{ | ||
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800", | ||
"impid": "test-imp-id", | ||
"price": 0.500000, | ||
"adm": "some-test-ad", | ||
"crid": "test-crid", | ||
"mtype": 3 | ||
}] | ||
} | ||
], | ||
"cur": "USD" | ||
} | ||
} | ||
} | ||
], | ||
|
||
"expectedBidResponses": [ | ||
{ | ||
"currency": "USD", | ||
"bids": [ | ||
{ | ||
"bid": { | ||
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800", | ||
"impid": "test-imp-id", | ||
"price": 0.5, | ||
"adm": "some-test-ad", | ||
"crid": "test-crid", | ||
"mtype": 3 | ||
}, | ||
"type": "audio" | ||
} | ||
] | ||
} | ||
] | ||
} |
90 changes: 90 additions & 0 deletions
90
adapters/smrtconnect/smrtconnecttest/exemplary/audio-web.json
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,90 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"site": { | ||
"page": "prebid.org" | ||
}, | ||
"user": { | ||
"buyeruid": "be5e209ad46927520000000000000000" | ||
}, | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"audio": { | ||
"mimes": ["audio/mp4"], | ||
"protocols": [9,10] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"supply_id": "1" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
|
||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://test.smrtconnect.com/openrtb2/auction?supply_id=1", | ||
"body": { | ||
"id": "test-request-id", | ||
"site": { | ||
"page": "prebid.org" | ||
}, | ||
"user": { | ||
"buyeruid": "be5e209ad46927520000000000000000" | ||
}, | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"audio": { | ||
"mimes": ["audio/mp4"], | ||
"protocols": [9,10] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"seatbid": [ | ||
{ | ||
"seat": "smrtconnect", | ||
"bid": [{ | ||
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800", | ||
"impid": "test-imp-id", | ||
"price": 0.500000, | ||
"adm": "some-test-ad", | ||
"crid": "test-crid", | ||
"mtype": 3 | ||
}] | ||
} | ||
], | ||
"cur": "USD" | ||
} | ||
} | ||
} | ||
], | ||
|
||
"expectedBidResponses": [ | ||
{ | ||
"currency": "USD", | ||
"bids": [ | ||
{ | ||
"bid": { | ||
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800", | ||
"impid": "test-imp-id", | ||
"price": 0.5, | ||
"adm": "some-test-ad", | ||
"crid": "test-crid", | ||
"mtype": 3 | ||
}, | ||
"type": "audio" | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.