Skip to content

Commit

Permalink
Sonobi: native and currency conversion support (prebid#3889)
Browse files Browse the repository at this point in the history
Co-authored-by: bansawbanchee <[email protected]>
  • Loading branch information
bansawbanchee and bansawbanchee authored Oct 7, 2024
1 parent f27bcef commit 8689e0d
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 8 deletions.
39 changes: 31 additions & 8 deletions adapters/sonobi/sonobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
Expand Down Expand Up @@ -53,6 +54,25 @@ func (a *SonobiAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adap

reqCopy.Imp[0].TagID = sonobiExt.TagID

// If the bid floor currency is not USD, do the conversion to USD
if reqCopy.Imp[0].BidFloor > 0 && reqCopy.Imp[0].BidFloorCur != "" && strings.ToUpper(reqCopy.Imp[0].BidFloorCur) != "USD" {

// Convert to US dollars
convertedValue, err := reqInfo.ConvertCurrency(reqCopy.Imp[0].BidFloor, reqCopy.Imp[0].BidFloorCur, "USD")
if err != nil {
errs = append(errs, err)
continue
}

// Update after conversion. All imp elements inside request.Imp are shallow copies
// therefore, their non-pointer values are not shared memory and are safe to modify.
reqCopy.Imp[0].BidFloorCur = "USD"
reqCopy.Imp[0].BidFloor = convertedValue
}

// Sonobi only bids in USD
reqCopy.Cur = append(make([]string, 0, 1), "USD")

adapterReq, errors := a.makeRequest(&reqCopy)
if adapterReq != nil {
adapterRequests = append(adapterRequests, adapterReq)
Expand Down Expand Up @@ -115,19 +135,19 @@ func (a *SonobiAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externalR
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
bidResponse.Currency = "USD" // Sonobi only bids in USD

for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bidType, err := getMediaTypeForImp(sb.Bid[i].ImpID, internalRequest.Imp)
bid := sb.Bid[i]
bidType, err := getMediaTypeForImp(bid.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 nil, []error{err}
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
})
}
}
return bidResponse, errs
Expand All @@ -140,6 +160,9 @@ func getMediaTypeForImp(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType,
if imp.Banner == nil && imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
}
if imp.Banner == nil && imp.Video == nil && imp.Native != nil {
mediaType = openrtb_ext.BidTypeNative
}
return mediaType, nil
}
}
Expand Down
2 changes: 2 additions & 0 deletions adapters/sonobi/sonobitest/exemplary/banner.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"mockBidRequest": {
"cur": ["GBP"],
"id": "some-request-id",
"site": {
"page": "http://tester.go.sonobi.com",
Expand Down Expand Up @@ -48,6 +49,7 @@
},
"uri": "https://apex.go.sonobi.com/prebid?partnerid=71d9d3d8af",
"body": {
"cur": ["USD"],
"id": "some-request-id",
"imp": [
{
Expand Down
143 changes: 143 additions & 0 deletions adapters/sonobi/sonobitest/exemplary/native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"mockBidRequest": {
"cur": ["USD"],
"id": "some-request-id",
"site": {
"page": "http://tester.go.sonobi.com",
"domain": "sonobi.com"
},
"device": {
"ip": "123.123.123.123"
},
"imp": [
{
"id": "some-impression-id",
"native": {
"request": "{\"ver\":\"1.2\",\"context\":1,\"contextsubtype\":11,\"plcmttype\":2,\"plcmtcnt\":3,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":1000}},{\"id\":2,\"required\":1,\"img\":{\"type\":3,\"wmin\":600,\"hmin\":600}},{\"id\":3,\"required\":0,\"data\":{\"type\":1,\"len\":200}},{\"id\":4,\"required\":0,\"data\":{\"type\":2,\"len\":3000}},{\"id\":5,\"required\":0,\"data\":{\"type\":6,\"len\":60}},{\"id\":6,\"required\":0,\"data\":{\"type\":500}},{\"id\":10,\"required\":0,\"data\":{\"type\":12,\"len\":15}}],\"eventtrackers\":[{\"event\":1,\"methods\":[1,2]},{\"event\":2,\"methods\":[1]}],\"privacy\":1}",
"ver": "1.2",
"battr": [
1,
2,
6,
7,
8,
9,
10,
14
]
},
"ext": {
"bidder": {
"TagID": "/7780971/apex_3pdm_integration"
}
}
}
],
"test": 1,
"tmax": 500
},
"httpCalls": [
{
"expectedRequest": {
"headers": {
"Accept": [
"application/json"
],
"Content-Type": [
"application/json;charset=utf-8"
]
},
"uri": "https://apex.go.sonobi.com/prebid?partnerid=71d9d3d8af",
"body": {
"cur": ["USD"],
"id": "some-request-id",
"imp": [
{
"id": "some-impression-id",
"native": {
"request": "{\"ver\":\"1.2\",\"context\":1,\"contextsubtype\":11,\"plcmttype\":2,\"plcmtcnt\":3,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":1000}},{\"id\":2,\"required\":1,\"img\":{\"type\":3,\"wmin\":600,\"hmin\":600}},{\"id\":3,\"required\":0,\"data\":{\"type\":1,\"len\":200}},{\"id\":4,\"required\":0,\"data\":{\"type\":2,\"len\":3000}},{\"id\":5,\"required\":0,\"data\":{\"type\":6,\"len\":60}},{\"id\":6,\"required\":0,\"data\":{\"type\":500}},{\"id\":10,\"required\":0,\"data\":{\"type\":12,\"len\":15}}],\"eventtrackers\":[{\"event\":1,\"methods\":[1,2]},{\"event\":2,\"methods\":[1]}],\"privacy\":1}",
"ver": "1.2",
"battr": [
1,
2,
6,
7,
8,
9,
10,
14
]
},
"tagid": "/7780971/apex_3pdm_integration",
"ext": {
"bidder": {
"TagID": "/7780971/apex_3pdm_integration"
}
}
}
],
"site": {
"domain": "sonobi.com",
"page": "http://tester.go.sonobi.com"
},
"device": {
"ip": "123.123.123.123"
},
"test": 1,
"tmax": 500
},
"impIDs": [
"some-impression-id"
]
},
"mockResponse": {
"status": 200,
"body": {
"id": "some-request-id",
"seatbid": [
{
"bid": [
{
"id": "1",
"impid": "some-impression-id",
"price": 2.8649999999999998,
"adm": "test-markup",
"adomain": [
"sonobi.com"
],
"cid": "house",
"crid": "sandbox"

}
],
"seat": "sonobi"
}
],
"bidid": "sandbox_642305097",
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "1",
"impid": "some-impression-id",
"price": 2.8649999999999998,
"adm": "test-markup",
"adomain": [
"sonobi.com"
],
"cid": "house",
"crid": "sandbox"
},
"type": "native"
}
]
}
]
}
2 changes: 2 additions & 0 deletions adapters/sonobi/sonobitest/exemplary/no-bid.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"mockBidRequest": {
"cur": ["USD"],
"id": "some-request-id",
"site": {
"page": "http://tester.go.sonobi.com",
Expand Down Expand Up @@ -48,6 +49,7 @@
},
"uri": "https://apex.go.sonobi.com/prebid?partnerid=71d9d3d8af",
"body": {
"cur": ["USD"],
"id": "some-request-id",
"imp": [
{
Expand Down
Loading

0 comments on commit 8689e0d

Please sign in to comment.