forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Adapter: DecenterAds (prebid#1669)
Co-authored-by: vlad <[email protected]>
- Loading branch information
Showing
22 changed files
with
1,046 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,124 @@ | ||
package decenterads | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
"github.com/prebid/prebid-server/adapters" | ||
"github.com/prebid/prebid-server/config" | ||
"github.com/prebid/prebid-server/errortypes" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
) | ||
|
||
type adapter struct { | ||
endpoint string | ||
} | ||
|
||
func (a *adapter) MakeRequests(request *openrtb.BidRequest, _ *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
headers.Add("Accept", "application/json") | ||
impressions := request.Imp | ||
result := make([]*adapters.RequestData, 0, len(impressions)) | ||
errs := make([]error, 0, len(impressions)) | ||
|
||
for _, impression := range impressions { | ||
var impExt map[string]json.RawMessage | ||
if err := json.Unmarshal(impression.Ext, &impExt); err != nil { | ||
errs = append(errs, fmt.Errorf("unable to parse bidder parameers: %s", err)) | ||
continue | ||
} | ||
|
||
bidderExt, bidderExtExists := impExt["bidder"] | ||
if !bidderExtExists || len(bidderExt) == 0 { | ||
errs = append(errs, errors.New("bidder parameters required")) | ||
continue | ||
} | ||
|
||
impression.Ext = bidderExt | ||
request.Imp = []openrtb.Imp{impression} | ||
body, err := json.Marshal(request) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
result = append(result, &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: a.endpoint, | ||
Body: body, | ||
Headers: headers, | ||
}) | ||
} | ||
|
||
request.Imp = impressions | ||
return result, errs | ||
} | ||
|
||
func (a *adapter) MakeBids(request *openrtb.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
var errs []error | ||
|
||
switch responseData.StatusCode { | ||
case http.StatusNoContent: | ||
return nil, nil | ||
case http.StatusBadRequest: | ||
return nil, []error{&errortypes.BadInput{ | ||
Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode), | ||
}} | ||
case http.StatusOK: | ||
break | ||
default: | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: "unexpected status code: " + strconv.Itoa(responseData.StatusCode), | ||
}} | ||
} | ||
|
||
var bidResponse openrtb.BidResponse | ||
err := json.Unmarshal(responseData.Body, &bidResponse) | ||
if err != nil { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: err.Error(), | ||
}} | ||
} | ||
|
||
response := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) | ||
|
||
for _, seatBid := range bidResponse.SeatBid { | ||
for i := range seatBid.Bid { | ||
response.Bids = append(response.Bids, &adapters.TypedBid{ | ||
Bid: &seatBid.Bid[i], | ||
BidType: getMediaTypeForImp(seatBid.Bid[i].ImpID, request.Imp), | ||
}) | ||
} | ||
} | ||
|
||
return response, errs | ||
} | ||
|
||
func getMediaTypeForImp(impID string, imps []openrtb.Imp) openrtb_ext.BidType { | ||
for _, imp := range imps { | ||
if imp.ID == impID { | ||
if imp.Banner != nil { | ||
return openrtb_ext.BidTypeBanner | ||
} else if imp.Video != nil { | ||
return openrtb_ext.BidTypeVideo | ||
} else if imp.Native != nil { | ||
return openrtb_ext.BidTypeNative | ||
} else if imp.Audio != nil { | ||
return openrtb_ext.BidTypeAudio | ||
} | ||
} | ||
} | ||
return openrtb_ext.BidTypeBanner | ||
} | ||
|
||
// Builder builds a new instance of the DecenterAds adapter for the given bidder with the given config. | ||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) { | ||
bidder := &adapter{ | ||
endpoint: config.Endpoint, | ||
} | ||
return bidder, 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,18 @@ | ||
package decenterads | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/adapters/adapterstest" | ||
"github.com/prebid/prebid-server/config" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
bidder, buildErr := Builder(openrtb_ext.BidderDecenterAds, config.Adapter{ | ||
Endpoint: "http://example.com/?c=o&m=ortb"}) | ||
if buildErr != nil { | ||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
} | ||
adapterstest.RunJSONBidderTest(t, "decenteradstest", bidder) | ||
} |
128 changes: 128 additions & 0 deletions
128
adapters/decenterads/decenteradstest/exemplary/simple-banner.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,128 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
{ | ||
"w": 300, | ||
"h": 600 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"placementId": "3" | ||
} | ||
} | ||
} | ||
], | ||
"app": { | ||
"id": "1", | ||
"bundle": "com.wls.testwlsapplication" | ||
}, | ||
"device": { | ||
"ip": "123.123.123.123", | ||
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx" | ||
} | ||
}, | ||
|
||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://example.com/?c=o&m=ortb", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
{ | ||
"w": 300, | ||
"h": 600 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"placementId": "3" | ||
} | ||
} | ||
], | ||
"app": { | ||
"id": "1", | ||
"bundle": "com.wls.testwlsapplication" | ||
}, | ||
"device": { | ||
"ip": "123.123.123.123", | ||
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx" | ||
} | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"seatbid": [ | ||
{ | ||
"bid": [ | ||
{ | ||
"id": "test_bid_id", | ||
"impid": "test-imp-id", | ||
"price": 0.27543, | ||
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"http://supply.decenterads.com/?c=o&m=adm&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>", | ||
"cid": "test_cid", | ||
"crid": "test_crid", | ||
"dealid": "test_dealid", | ||
"w": 300, | ||
"h": 250, | ||
"ext": { | ||
"prebid": { | ||
"type": "banner" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
|
||
"expectedBidResponses": [ | ||
{ | ||
"bids":[ | ||
{ | ||
"bid": { | ||
"id": "test_bid_id", | ||
"impid": "test-imp-id", | ||
"price": 0.27543, | ||
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"http://supply.decenterads.com/?c=o&m=adm&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>", | ||
"cid": "test_cid", | ||
"crid": "test_crid", | ||
"dealid": "test_dealid", | ||
"w": 300, | ||
"h": 250, | ||
"ext": { | ||
"prebid": { | ||
"type": "banner" | ||
} | ||
} | ||
}, | ||
"type": "banner" | ||
} | ||
] | ||
} | ||
] | ||
} |
116 changes: 116 additions & 0 deletions
116
adapters/decenterads/decenteradstest/exemplary/simple-video.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,116 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"device": { | ||
"ip": "123.123.123.123", | ||
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx" | ||
}, | ||
"app": { | ||
"id": "1", | ||
"bundle": "com.wls.testwlsapplication" | ||
}, | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"video": { | ||
"mimes": ["video/mp4"], | ||
"protocols": [2, 5], | ||
"w": 1024, | ||
"h": 576 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"placementId": "3" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
|
||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://example.com/?c=o&m=ortb", | ||
"body": { | ||
"id": "test-request-id", | ||
"device": { | ||
"ip": "123.123.123.123", | ||
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx" | ||
}, | ||
"app": { | ||
"id": "1", | ||
"bundle": "com.wls.testwlsapplication" | ||
}, | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"video": { | ||
"mimes": ["video/mp4"], | ||
"protocols": [2, 5], | ||
"w": 1024, | ||
"h": 576 | ||
}, | ||
"ext": { | ||
"placementId": "3" | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"seatbid": [ | ||
{ | ||
"bid": [ | ||
{ | ||
"id": "test_bid_id", | ||
"impid": "test-imp-id", | ||
"price": 0.27543, | ||
"adm": "<VAST version=\"3.0\"><Ad id=\"20001\" sequence=\"1\"><InLine><AdSystem version=\"4.0\"><![CDATA[iabtechlab]]></AdSystem><AdTitle><![CDATA[Inline Simple Ad]]></AdTitle><Impression><![CDATA[]]></Impression><Creatives><Creative id=\"5480\" sequence=\"1\"><Linear><Duration>00:01:00</Duration><MediaFiles><MediaFile id=\"5246\" delivery=\"progressive\" type=\"video/mp4\" bitrate=\"600\" width=\"640\" height=\"360\" minBitrate=\"500\" maxBitrate=\"700\" scalable=\"1\" maintainAspectRatio=\"1\" codec=\"0\"><![CDATA[https://s0.2mdn.net/4253510/google_ddm_animation_480P.mp4]]></MediaFile></MediaFiles><VideoClicks><ClickThrough id=\"blog\"><![CDATA[https://example.com]]></ClickThrough></VideoClicks></Linear></Creative></Creatives></InLine></Ad></VAST>", | ||
"cid": "test_cid", | ||
"crid": "test_crid", | ||
"dealid": "test_dealid", | ||
"ext": { | ||
"prebid": { | ||
"type": "video" | ||
} | ||
} | ||
} | ||
], | ||
"seat": "decenterads" | ||
} | ||
], | ||
"cur": "USD" | ||
} | ||
} | ||
} | ||
], | ||
|
||
|
||
"expectedBidResponses": [ | ||
{ | ||
"currency": "USD", | ||
"bids": [ | ||
{ | ||
"bid": { | ||
"id": "test_bid_id", | ||
"impid": "test-imp-id", | ||
"price": 0.27543, | ||
"adm": "<VAST version=\"3.0\"><Ad id=\"20001\" sequence=\"1\"><InLine><AdSystem version=\"4.0\"><![CDATA[iabtechlab]]></AdSystem><AdTitle><![CDATA[Inline Simple Ad]]></AdTitle><Impression><![CDATA[]]></Impression><Creatives><Creative id=\"5480\" sequence=\"1\"><Linear><Duration>00:01:00</Duration><MediaFiles><MediaFile id=\"5246\" delivery=\"progressive\" type=\"video/mp4\" bitrate=\"600\" width=\"640\" height=\"360\" minBitrate=\"500\" maxBitrate=\"700\" scalable=\"1\" maintainAspectRatio=\"1\" codec=\"0\"><![CDATA[https://s0.2mdn.net/4253510/google_ddm_animation_480P.mp4]]></MediaFile></MediaFiles><VideoClicks><ClickThrough id=\"blog\"><![CDATA[https://example.com]]></ClickThrough></VideoClicks></Linear></Creative></Creatives></InLine></Ad></VAST>", | ||
"cid": "test_cid", | ||
"crid": "test_crid", | ||
"dealid": "test_dealid", | ||
"ext": { | ||
"prebid": { | ||
"type": "video" | ||
} | ||
} | ||
}, | ||
"type": "video" | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.