This repository has been archived by the owner on Dec 22, 2022. It is now read-only.
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.
Co-authored-by: Aiholkin <artem.iholkin@smartyads.com>
- Loading branch information
1 parent
300ebe2
commit 7901267
Showing
26 changed files
with
1,054 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,140 @@ | ||
package adman | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
"github.com/prebid/prebid-server/adapters" | ||
"github.com/prebid/prebid-server/errortypes" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
) | ||
|
||
// AdmanAdapter struct | ||
type AdmanAdapter struct { | ||
URI string | ||
} | ||
|
||
// NewAdmanBidder Initializes the Bidder | ||
func NewAdmanBidder(endpoint string) *AdmanAdapter { | ||
return &AdmanAdapter{ | ||
URI: endpoint, | ||
} | ||
} | ||
|
||
type admanParams struct { | ||
TagID string `json:"TagID"` | ||
} | ||
|
||
// MakeRequests create bid request for adman demand | ||
func (a *AdmanAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
var errs []error | ||
var admanExt openrtb_ext.ExtImpAdman | ||
var err error | ||
|
||
var adapterRequests []*adapters.RequestData | ||
|
||
reqCopy := *request | ||
for _, imp := range request.Imp { | ||
reqCopy.Imp = []openrtb.Imp{imp} | ||
|
||
var bidderExt adapters.ExtImpBidder | ||
if err = json.Unmarshal(reqCopy.Imp[0].Ext, &bidderExt); err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
if err = json.Unmarshal(bidderExt.Bidder, &admanExt); err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
reqCopy.Imp[0].TagID = admanExt.TagID | ||
|
||
adapterReq, errors := a.makeRequest(&reqCopy) | ||
if adapterReq != nil { | ||
adapterRequests = append(adapterRequests, adapterReq) | ||
} | ||
errs = append(errs, errors...) | ||
} | ||
return adapterRequests, errs | ||
} | ||
|
||
func (a *AdmanAdapter) makeRequest(request *openrtb.BidRequest) (*adapters.RequestData, []error) { | ||
|
||
var errs []error | ||
|
||
reqJSON, err := json.Marshal(request) | ||
|
||
if err != nil { | ||
errs = append(errs, err) | ||
return nil, errs | ||
} | ||
|
||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
headers.Add("Accept", "application/json") | ||
return &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: a.URI, | ||
Body: reqJSON, | ||
Headers: headers, | ||
}, errs | ||
} | ||
|
||
// MakeBids makes the bids | ||
func (a *AdmanAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
var errs []error | ||
|
||
if response.StatusCode == http.StatusNoContent { | ||
return nil, nil | ||
} | ||
|
||
if response.StatusCode == http.StatusNotFound { | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode), | ||
}} | ||
} | ||
|
||
var bidResp openrtb.BidResponse | ||
|
||
if err := json.Unmarshal(response.Body, &bidResp); err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(1) | ||
|
||
for _, sb := range bidResp.SeatBid { | ||
for i := range sb.Bid { | ||
bidType, err := getMediaTypeForImp(sb.Bid[i].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 bidResponse, errs | ||
} | ||
|
||
func getMediaTypeForImp(impID string, imps []openrtb.Imp) (openrtb_ext.BidType, error) { | ||
mediaType := openrtb_ext.BidTypeBanner | ||
for _, imp := range imps { | ||
if imp.ID == impID { | ||
if imp.Banner == nil && imp.Video != nil { | ||
mediaType = openrtb_ext.BidTypeVideo | ||
} | ||
return mediaType, nil | ||
} | ||
} | ||
|
||
// This shouldnt happen. Lets handle it just incase by returning an error. | ||
return "", &errortypes.BadInput{ | ||
Message: fmt.Sprintf("Failed to find impression \"%s\" ", impID), | ||
} | ||
} |
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,12 @@ | ||
package adman | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/adapters/adapterstest" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
admanAdapter := NewAdmanBidder("http://eu-ams-1.admanmedia.com/?c=o&m=ortb") | ||
adapterstest.RunJSONBidderTest(t, "admantest", admanAdapter) | ||
} |
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,134 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
{ | ||
"w": 300, | ||
"h": 600 | ||
} | ||
] | ||
}, | ||
"tagid": "16", | ||
"ext": { | ||
"bidder": { | ||
"TagID": "16" | ||
} | ||
} | ||
} | ||
], | ||
"app": { | ||
"id": "1", | ||
"bundle": "com.wls.testwlsapplication" | ||
}, | ||
"device": { | ||
"ip": "123.123.123.123", | ||
"ifa": "zxcjbzxmc-zxcbmz-zxbcz-zxczx" | ||
} | ||
}, | ||
|
||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://eu-ams-1.admanmedia.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 | ||
} | ||
] | ||
}, | ||
"tagid": "16", | ||
"ext": { | ||
"bidder": { | ||
"TagID": "16" | ||
} | ||
} | ||
} | ||
], | ||
"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=\"https://eu-ams-1.admanmedia.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" | ||
} | ||
} | ||
} | ||
], | ||
"seat": "adman" | ||
} | ||
], | ||
"cur": "USD" | ||
} | ||
} | ||
} | ||
], | ||
|
||
"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=\"https://eu-ams-1.admanmedia.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" | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.