Skip to content

Commit

Permalink
* New Adapter: Ogury - filter imps
Browse files Browse the repository at this point in the history
  • Loading branch information
krdzo authored Nov 28, 2024
1 parent 57fe53a commit cfc66d0
Show file tree
Hide file tree
Showing 13 changed files with 483 additions and 60 deletions.
95 changes: 67 additions & 28 deletions adapters/ogury/ogury.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

"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"
"github.com/prebid/prebid-server/v3/adapters"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/errortypes"
"github.com/prebid/prebid-server/v3/openrtb_ext"
)

type oguryAdapter struct {
Expand All @@ -28,31 +28,36 @@ func Builder(_ openrtb_ext.BidderName, config config.Adapter, _ config.Server) (
func (a oguryAdapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
headers := setHeaders(request)

request.Imp = filterValidImps(request)
if len(request.Imp) == 0 {
return nil, []error{&errortypes.BadInput{
Message: "Invalid request. assetKey/adUnitId or request.site.publisher.id required",
}}
}

var errors []error
var impExt, impExtBidderHoist map[string]json.RawMessage
for i, imp := range request.Imp {
var impExt, impExtBidderHoist map[string]json.RawMessage
// extract ext
if err := json.Unmarshal(imp.Ext, &impExt); err != nil {
return nil, append(errors, &errortypes.BadInput{
Message: "Bidder extension not provided or can't be unmarshalled",
})
}
// find Ogury bidder params
bidder, ok := impExt["bidder"]
if ok {
if bidder, ok := impExt[openrtb_ext.PrebidExtBidderKey]; ok {
if err := json.Unmarshal(bidder, &impExtBidderHoist); err != nil {
return nil, append(errors, &errortypes.BadInput{
Message: "Bidder extension not provided or can't be unmarshalled",
Message: "Ogury bidder extension not provided or can't be unmarshalled",
})
}

}

impExtOut := make(map[string]any, len(impExt)-1+len(impExtBidderHoist))

// extract Ogury "bidder" params from imp.ext.bidder to imp.ext
for key, value := range impExt {
if key != "bidder" {
if key != openrtb_ext.PrebidExtBidderKey {
impExtOut[key] = value
}
}
Expand All @@ -66,14 +71,14 @@ func (a oguryAdapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *ad
Message: "Error while marshaling Imp.Ext bidder exension",
})
}
request.Imp[i].Ext = ext

// save adUnitCode
request.Imp[i].TagID = imp.ID
if impExtOut["gpid"] == "" {
impExtOut["gpid"] = imp.ID
if adUnitCode := getAdUnitCode(impExt); adUnitCode != "" {
request.Imp[i].TagID = adUnitCode
} else {
request.Imp[i].TagID = imp.ID
}

request.Imp[i].Ext = ext
}

// currency conversion
Expand Down Expand Up @@ -111,6 +116,51 @@ func (a oguryAdapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *ad

}

func filterValidImps(request *openrtb2.BidRequest) (validImps []openrtb2.Imp) {
for _, imp := range request.Imp {
var impExt adapters.ExtImpBidder
var impExtOgury openrtb_ext.ImpExtOgury

if err := json.Unmarshal(imp.Ext, &impExt); err != nil {
continue
}
if err := json.Unmarshal(impExt.Bidder, &impExtOgury); err != nil {
continue
}
if impExtOgury.AssetKey != "" && impExtOgury.AdUnitID != "" {
validImps = append(validImps, imp)
}
}

// if we have imp with assetKey/adUnitId then we want to serve them
if len(validImps) > 0 {
return validImps
}

// no assetKey/adUnitId imps then we serve everything if publisher.ID exists
if request.Site != nil && request.Site.Publisher.ID != "" {
return request.Imp
}

// else no valid imp
return nil
}

func getAdUnitCode(ext map[string]json.RawMessage) string {
var prebidExt openrtb_ext.ExtImpPrebid
v, ok := ext["prebid"]
if !ok {
return ""
}

err := json.Unmarshal(v, &prebidExt)
if err != nil {
return ""
}

return prebidExt.AdUnitCode
}

func setHeaders(request *openrtb2.BidRequest) http.Header {
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
Expand Down Expand Up @@ -144,21 +194,10 @@ func getMediaTypeForBid(impressions []openrtb2.Imp, bid openrtb2.Bid) (openrtb_e
}

func (a oguryAdapter) MakeBids(request *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if responseData.StatusCode == http.StatusNoContent {
if adapters.IsResponseStatusCodeNoContent(responseData) {
return nil, nil
}

if responseData.StatusCode == http.StatusBadRequest {
err := &errortypes.BadInput{
Message: "Unexpected status code: 400. Bad request from publisher. Run with request.debug = 1 for more info.",
}
return nil, []error{err}
}

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

Expand Down
6 changes: 3 additions & 3 deletions adapters/ogury/ogury_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package ogury
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/prebid/prebid-server/v3/adapters/adapterstest"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/openrtb_ext"
)

func TestJsonSamples(t *testing.T) {
Expand Down
11 changes: 8 additions & 3 deletions adapters/ogury/ogurytest/exemplary/banner_asset_ad_unit.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
},
"ext": {
"gpid": "global position id",
"prebid": {
"adunitcode": "ad-unit-code"
},
"bidder": {
"assetKey": "OGY",
"adUnitId": "123"
Expand All @@ -27,14 +30,17 @@
"imp": [
{
"id":"test-imp-id",
"tagid": "test-imp-id",
"tagid": "ad-unit-code",
"banner": {
"format": [{"w": 128, "h": 100}]
},
"ext": {
"gpid": "global position id",
"assetKey": "OGY",
"adUnitId": "123"
"adUnitId": "123",
"prebid": {
"adunitcode": "ad-unit-code"
}
}
}
]
Expand Down Expand Up @@ -85,4 +91,3 @@
}
]
}

141 changes: 141 additions & 0 deletions adapters/ogury/ogurytest/exemplary/banner_multi_imp_with_params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{
"mockBidRequest": {
"id": "test-request-id",
"site": {
"publisher": {
"id": "pub123"
}
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [{"w": 128, "h": 100}]
},
"ext": {
"gpid": "global position id",
"prebid": {
"adunitcode": "ad-unit-code"
},
"bidder": {
"assetKey": "OGY",
"adUnitId": "123"
}
}
},
{
"id": "test-imp-id-2",
"banner": {
"format": [{"w": 128, "h": 100}]
},
"ext": {"bidder": {}}
},
{
"id": "test-imp-id-3",
"banner": {
"format": [{"w": 1, "h": 1}]
},
"ext": {
"gpid": "global position id",
"prebid": {
"adunitcode": "ad-unit-code-2"
},
"bidder": {
"assetKey": "OGY3",
"adUnitId": "1234"
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "http://ogury.example.com",
"body": {
"id": "test-request-id",
"site": {
"publisher": {
"id": "pub123"
}
},
"imp": [
{
"id":"test-imp-id",
"tagid": "ad-unit-code",
"banner": {
"format": [{"w": 128, "h": 100}]
},
"ext": {
"gpid": "global position id",
"assetKey": "OGY",
"adUnitId": "123",
"prebid": {
"adunitcode": "ad-unit-code"
}
}
},
{
"id":"test-imp-id-3",
"tagid": "ad-unit-code-2",
"banner": {
"format": [{"w": 1, "h": 1}]
},
"ext": {
"gpid": "global position id",
"assetKey": "OGY3",
"adUnitId": "1234",
"prebid": {
"adunitcode": "ad-unit-code-2"
}
}
}
]
},
"impIDs":["test-imp-id", "test-imp-id-3"]
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"cur": "USD",
"seatbid": [
{
"seat": "seat",
"bid": [{
"id": "some-UUID",
"impid": "test-imp-id",
"price": 0.500000,
"adm": "adm string",
"crid": "crid_10",
"h": 100,
"w": 128
}]
}
]
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "some-UUID",
"impid": "test-imp-id",
"price": 0.5,
"adm": "adm string",
"crid": "crid_10",
"h": 100,
"w": 128
},
"type": "banner"
}
]
}
]
}
Loading

0 comments on commit cfc66d0

Please sign in to comment.