-
Notifications
You must be signed in to change notification settings - Fork 765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Adapter: Ogury #4082
Merged
Merged
New Adapter: Ogury #4082
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f26eff9
New Adapter: Ogury
krdzo a7b2104
New Adapter: Ogury - fix tests
krdzo ec83b52
New Adapter: Ogury - fix url
krdzo 0eaa327
New Adapter: Ogury - Fix user sync url
krdzo 3a6c990
add text params
krdzo c4d6496
Merge remote-tracking branch 'origin/master' into sync-upstream-master
krdzo 57fe53a
Merge pull request #8 from Ogury/sync-upstream-master
krdzo cfc66d0
* New Adapter: Ogury - filter imps
krdzo 1125795
change oguryAdapter to adapter
krdzo 2289c6c
remove adunitcode mapping
krdzo 119a517
ADV-20406 address pr comments (#12)
krdzo 12c4a88
New Adapter: Ogury - PR comments
krdzo bede260
delete "bidder" field after hoist (#14)
krdzo ceb8f89
fix typo (#15)
krdzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,178 @@ | ||
package ogury | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/prebid/openrtb/v20/openrtb2" | ||
|
||
"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" | ||
"github.com/prebid/prebid-server/v3/util/jsonutil" | ||
) | ||
|
||
type adapter struct { | ||
endpoint string | ||
} | ||
|
||
func Builder(_ openrtb_ext.BidderName, config config.Adapter, _ config.Server) (adapters.Bidder, error) { | ||
return &adapter{endpoint: config.Endpoint}, nil | ||
} | ||
|
||
func (a adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
headers := buildHeaders(request) | ||
|
||
var errors []error | ||
var impsWithOguryParams []openrtb2.Imp | ||
for i, imp := range request.Imp { | ||
var impExt, impExtBidderHoist map[string]json.RawMessage | ||
// extract ext | ||
if err := jsonutil.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 | ||
if bidder, ok := impExt[openrtb_ext.PrebidExtBidderKey]; ok { | ||
if err := jsonutil.Unmarshal(bidder, &impExtBidderHoist); err != nil { | ||
return nil, append(errors, &errortypes.BadInput{ | ||
Message: "Ogury bidder extension not provided or can't be unmarshalled", | ||
}) | ||
} | ||
} | ||
|
||
// extract every value from imp[].ext.bidder to imp[].ext | ||
for key, value := range impExtBidderHoist { | ||
impExt[key] = value | ||
} | ||
delete(impExt, openrtb_ext.PrebidExtBidderKey) | ||
|
||
ext, err := jsonutil.Marshal(impExt) | ||
if err != nil { | ||
return nil, append(errors, &errortypes.BadInput{ | ||
Message: "Error while marshaling Imp.Ext bidder extension", | ||
}) | ||
} | ||
request.Imp[i].Ext = ext | ||
|
||
// save adUnitCode | ||
request.Imp[i].TagID = imp.ID | ||
|
||
// currency conversion | ||
// Check if imp comes with bid floor amount defined in a foreign currency | ||
if imp.BidFloor > 0 && imp.BidFloorCur != "" && strings.ToUpper(imp.BidFloorCur) != "USD" { | ||
|
||
// Convert to US dollars | ||
convertedValue, err := requestInfo.ConvertCurrency(imp.BidFloor, imp.BidFloorCur, "USD") | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
// 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. | ||
request.Imp[i].BidFloorCur = "USD" | ||
request.Imp[i].BidFloor = convertedValue | ||
} | ||
|
||
// check if imp has ogury params and filter it | ||
_, hasAssetKey := impExtBidderHoist["assetKey"] | ||
_, hasAdUnitId := impExtBidderHoist["adUnitId"] | ||
if hasAssetKey && hasAdUnitId { | ||
impsWithOguryParams = append(impsWithOguryParams, request.Imp[i]) | ||
} | ||
} | ||
|
||
if len(impsWithOguryParams) == 0 && (request.Site == nil || request.Site.Publisher.ID == "") { | ||
return nil, []error{&errortypes.BadInput{ | ||
Message: "Invalid request. assetKey/adUnitId or request.site.publisher.id required", | ||
}} | ||
} else if len(impsWithOguryParams) > 0 { | ||
request.Imp = impsWithOguryParams | ||
} | ||
|
||
requestJSON, err := jsonutil.Marshal(request) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
requestData := &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: a.endpoint, | ||
Body: requestJSON, | ||
Headers: headers, | ||
ImpIDs: openrtb_ext.GetImpIDs(request.Imp), | ||
} | ||
|
||
return []*adapters.RequestData{requestData}, nil | ||
|
||
} | ||
|
||
func buildHeaders(request *openrtb2.BidRequest) http.Header { | ||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
if request.Device != nil { | ||
headers.Add("X-Forwarded-For", request.Device.IP) | ||
krdzo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
headers.Add("X-Forwarded-For", request.Device.IPv6) | ||
headers.Add("User-Agent", request.Device.UA) | ||
headers.Add("Accept-Language", request.Device.Language) | ||
} | ||
return headers | ||
|
||
} | ||
|
||
func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) { | ||
switch bid.MType { | ||
case openrtb2.MarkupBanner: | ||
return openrtb_ext.BidTypeBanner, nil | ||
case openrtb2.MarkupAudio: | ||
return openrtb_ext.BidTypeAudio, nil | ||
case openrtb2.MarkupNative: | ||
return openrtb_ext.BidTypeNative, nil | ||
case openrtb2.MarkupVideo: | ||
return openrtb_ext.BidTypeVideo, nil | ||
default: | ||
return "", &errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("Unsupported MType \"%d\", for impression \"%s\"", bid.MType, bid.ImpID), | ||
} | ||
} | ||
} | ||
krdzo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func (a adapter) MakeBids(request *openrtb2.BidRequest, _ *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 := jsonutil.Unmarshal(responseData.Body, &response); err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) | ||
bidResponse.Currency = response.Cur | ||
var errors []error | ||
for _, seatBid := range response.SeatBid { | ||
for i, bid := range seatBid.Bid { | ||
bidType, err := getMediaTypeForBid(bid) | ||
if err != nil { | ||
errors = append(errors, err) | ||
continue | ||
} | ||
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
Bid: &seatBid.Bid[i], | ||
BidType: bidType, | ||
}) | ||
} | ||
} | ||
if errors != nil { | ||
return nil, errors | ||
} | ||
|
||
return bidResponse, nil | ||
} |
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,21 @@ | ||
package ogury | ||
|
||
import ( | ||
"testing" | ||
|
||
"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) { | ||
bidder, buildErr := Builder(openrtb_ext.BidderOgury, config.Adapter{ | ||
Endpoint: "http://ogury.example.com"}, | ||
config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||
|
||
if buildErr != nil { | ||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
} | ||
|
||
adapterstest.RunJSONBidderTest(t, "ogurytest", bidder) | ||
} |
89 changes: 89 additions & 0 deletions
89
adapters/ogury/ogurytest/exemplary/banner_asset_ad_unit.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,89 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "ad-unit-code-as-imp-id", | ||
"banner": { | ||
"format": [{"w": 128, "h": 100}] | ||
}, | ||
"ext": { | ||
"gpid": "global position id", | ||
"bidder": { | ||
"assetKey": "OGY", | ||
"adUnitId": "123" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
|
||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://ogury.example.com", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id":"ad-unit-code-as-imp-id", | ||
"tagid": "ad-unit-code-as-imp-id", | ||
"banner": { | ||
"format": [{"w": 128, "h": 100}] | ||
}, | ||
"ext": { | ||
"gpid": "global position id", | ||
"assetKey": "OGY", | ||
"adUnitId": "123" | ||
} | ||
} | ||
] | ||
}, | ||
"impIDs":["ad-unit-code-as-imp-id"] | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"cur": "USD", | ||
"seatbid": [ | ||
{ | ||
"seat": "seat", | ||
"bid": [{ | ||
"id": "some-UUID", | ||
"impid": "ad-unit-code-as-imp-id", | ||
"price": 0.500000, | ||
"adm": "adm string", | ||
"crid": "crid_10", | ||
"h": 100, | ||
"w": 128, | ||
"mtype": 1 | ||
}] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
|
||
"expectedBidResponses": [ | ||
{ | ||
"currency": "USD", | ||
"bids": [ | ||
{ | ||
"bid": { | ||
"id": "some-UUID", | ||
"impid": "ad-unit-code-as-imp-id", | ||
"price": 0.5, | ||
"adm": "adm string", | ||
"crid": "crid_10", | ||
"h": 100, | ||
"w": 128, | ||
"mtype": 1 | ||
}, | ||
"type": "banner" | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consolidate this for loop with the for loop starting at the line 72.