Skip to content

Commit

Permalink
use mtype for parsing media type and expand test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
product-trustedstack committed Apr 18, 2024
1 parent bb8f29b commit 5c3111b
Show file tree
Hide file tree
Showing 11 changed files with 931 additions and 17 deletions.
48 changes: 32 additions & 16 deletions adapters/trustedstack/trustedstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,8 @@ func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest
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),
}}
if err := adapters.CheckResponseStatusCodeForErrors(response); err != nil {
return nil, []error{err}
}

var bidResp openrtb2.BidResponse
Expand All @@ -66,7 +58,7 @@ func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest

for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
bidType, err := getMediaTypeForImp(&sb.Bid[i], internalRequest.Imp)
if err != nil {
errs = append(errs, err)
} else {
Expand All @@ -89,19 +81,43 @@ func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server co
}, nil
}

func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
mediaType := openrtb_ext.BidTypeBanner
func getMediaTypeForImp(bid *openrtb2.Bid, imps []openrtb2.Imp) (openrtb_ext.BidType, error) {
mediaType, err := getBidMediaTypeFromMtype(bid)
if err == nil {
return mediaType, nil
}
mediaType = openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner == nil && imp.Video != nil {
if imp.ID == bid.ImpID {
switch {
case imp.Banner == nil && imp.Video != nil && imp.Audio == nil && imp.Native == nil:
mediaType = openrtb_ext.BidTypeVideo
case imp.Banner == nil && imp.Video == nil && imp.Audio != nil && imp.Native == nil:
mediaType = openrtb_ext.BidTypeAudio
case imp.Banner == nil && imp.Video == nil && imp.Audio == nil && imp.Native != nil:
mediaType = openrtb_ext.BidTypeNative
}
return mediaType, nil
}
}

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

func getBidMediaTypeFromMtype(bid *openrtb2.Bid) (openrtb_ext.BidType, error) {
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
default:
return "", fmt.Errorf("unable to fetch mediaType for imp: %s", bid.ImpID)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "1",
"audio": {
"mimes": ["audio/mp3"],
"protocols": [2, 5]
},
"ext": {
"bidder": {
"cid": "8CUTSTCID",
"crid": "999999999"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://example.trustedstack.com/rtb/prebid?src=http%3A%2F%2Fhosturl.com",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "1",
"audio": {
"mimes": [
"audio/mp3"
],
"protocols": [
2,
5
]
},
"ext": {
"bidder": {
"cid": "8CUTSTCID",
"crid": "999999999"
}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"cur": "USD",
"seatbid": [
{
"seat": "trustedstack",
"bid": [
{
"id": "test-bid-id",
"impid": "1",
"price": 2.50,
"adm": "some-test-ad",
"crid": "test-crid",
"mtype": 3
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test-bid-id",
"impid": "1",
"price": 2.50,
"adm": "some-test-ad",
"crid": "test-crid",
"mtype": 3
},
"type": "audio"
}
]
}
]
}
88 changes: 88 additions & 0 deletions adapters/trustedstack/trustedstacktest/exemplary/simple-audio.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "1",
"audio": {
"mimes": ["audio/mp3"],
"protocols": [2, 5]
},
"ext": {
"bidder": {
"cid": "8CUTSTCID",
"crid": "999999999"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "https://example.trustedstack.com/rtb/prebid?src=http%3A%2F%2Fhosturl.com",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "1",
"audio": {
"mimes": [
"audio/mp3"
],
"protocols": [
2,
5
]
},
"ext": {
"bidder": {
"cid": "8CUTSTCID",
"crid": "999999999"
}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"cur": "USD",
"seatbid": [
{
"seat": "trustedstack",
"bid": [
{
"id": "test-bid-id",
"impid": "1",
"price": 2.50,
"adm": "some-test-ad",
"crid": "test-crid"
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test-bid-id",
"impid": "1",
"price": 2.50,
"adm": "some-test-ad",
"crid": "test-crid"
},
"type": "audio"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "1",
"banner": {
"format": [
{
"w": 320,
"h": 50
}
]
},
"ext": {
"bidder": {
"cid": "8CUTSTCID",
"crid": "999999999"
}
}
}
]
},

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

"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "trustedstack",
"bid": [
{
"id": "test-bid-id",
"impid": "1",
"price": 1.50,
"adm": "some-test-ad",
"crid": "test-crid",
"h": 50,
"w": 320,
"mtype": 1
}
]
}
],
"cur": "USD"
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "test-bid-id",
"impid": "1",
"price": 1.50,
"adm": "some-test-ad",
"crid": "test-crid",
"w": 320,
"h": 50,
"mtype": 1
},
"type": "banner"
}
]
}
]
}
Loading

0 comments on commit 5c3111b

Please sign in to comment.