Skip to content
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

AdOcean adapter - Support for sizes defined in prebid configuration. #1339

Merged
merged 20 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0c7d943
[ADOCEAN-20132] AdOcean adapter
mmuras Apr 17, 2020
447f061
[ADOCEAN-20132] AdOcean adapter - support for gdpr
mmuras Apr 17, 2020
1018dd8
[ADOCEAN-20132] AdOcean adapter - tests
mmuras Apr 20, 2020
545fc0c
[ADOCEAN-20132] AdOcean adapter - user sync
mmuras Apr 21, 2020
46a2350
[ADOCEAN-20132] AdOcean adapter - formatting
mmuras Apr 21, 2020
8c18a24
[ADOCEAN-20132] AdOcean adapter - send uuid to emitter
mmuras Apr 22, 2020
5c12273
[ADOCEAN-20132] adocean adapter - return nil if there is no creative
mmuras Apr 22, 2020
46df8ff
[ADOCEAN-20132] AdOcean adapter - add version parameter
mmuras Apr 23, 2020
c9f7379
[ADOCEAN-20132] AdOcean adapter - optimization
mmuras Apr 23, 2020
1f78191
Merge branch 'new-adapter-adocean'
mmuras Apr 23, 2020
a0f4171
[ADOCEAN-20132] AdOcean adapter - add to syncer_test.go
mmuras Apr 23, 2020
a2b9fc5
[ADOCEAN-20132] AdOcean adapter - changes after review:
mmuras Apr 27, 2020
da793fa
[ADOCEAN-20132] AdOcean adapter - changes after review:
mmuras May 1, 2020
54d8ddc
[ADOCEAN-20132] AdOcean adapter - changes after review:
mmuras May 4, 2020
b0b00d5
AdOcean adapter - support for multiple sizes
mmuras Apr 30, 2020
a055b36
AdOcean adapter - support for multiple sizes - changed method of
mmuras May 7, 2020
96bc694
AdOcean adapter - bump version to 1.1.0
mmuras May 7, 2020
68875c7
Merge branch 'adapter-adocean-multiple-sizes'
mmuras Jun 5, 2020
66d255b
[ADOCEAN-20132] AdOcean adapter - changes after review:
mmuras Jun 11, 2020
ede19a1
[ADOCEAN-20132] AdOcean adapter - changes after review:
mmuras Jun 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 118 additions & 37 deletions adapters/adocean/adocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
Expand All @@ -19,7 +20,7 @@ import (
"github.com/prebid/prebid-server/openrtb_ext"
)

const adapterVersion = "1.0.0"
const adapterVersion = "1.1.0"
const maxUriLength = 8000
const measurementCode = `
<script>
Expand Down Expand Up @@ -51,6 +52,12 @@ type ResponseAdUnit struct {
Error string `json:"error"`
}

type requestData struct {
Url *url.URL
Headers *http.Header
SlaveSizes map[string]string
}

func NewAdOceanBidder(client *http.Client, endpointTemplateString string) *AdOceanAdapter {
a := &adapters.HTTPAdapter{Client: client}
endpointTemplate, err := template.New("endpointTemplate").Parse(endpointTemplateString)
Expand Down Expand Up @@ -89,44 +96,59 @@ func (a *AdOceanAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adap
}
}

var httpRequests []*adapters.RequestData
var errors []error

var err error
requestsData := make([]*requestData, 0, len(request.Imp))
for _, auction := range request.Imp {
newHttpRequest, err := a.makeRequest(httpRequests, &auction, request, consentString)
requestsData, err = a.addNewBid(requestsData, &auction, request, consentString)
if err != nil {
errors = append(errors, err)
} else if newHttpRequest != nil {
httpRequests = append(httpRequests, newHttpRequest)
}
}

httpRequests := make([]*adapters.RequestData, 0, len(requestsData))
for _, requestData := range requestsData {
httpRequests = append(httpRequests, &adapters.RequestData{
Method: "GET",
Uri: requestData.Url.String(),
Headers: *requestData.Headers,
})
}

return httpRequests, errors
}

func (a *AdOceanAdapter) makeRequest(existingRequests []*adapters.RequestData, imp *openrtb.Imp, request *openrtb.BidRequest, consentString string) (*adapters.RequestData, error) {
func (a *AdOceanAdapter) addNewBid(
requestsData []*requestData,
imp *openrtb.Imp,
request *openrtb.BidRequest,
consentString string,
) ([]*requestData, error) {
var bidderExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &bidderExt); err != nil {
return nil, &errortypes.BadInput{
return requestsData, &errortypes.BadInput{
Message: "Error parsing bidderExt object",
}
}

var adOceanExt openrtb_ext.ExtImpAdOcean
if err := json.Unmarshal(bidderExt.Bidder, &adOceanExt); err != nil {
return nil, &errortypes.BadInput{
return requestsData, &errortypes.BadInput{
Message: "Error parsing adOceanExt parameters",
}
}

addedToExistingRequest := addToExistingRequest(existingRequests, &adOceanExt, imp.ID)
addedToExistingRequest := addToExistingRequest(requestsData, &adOceanExt, imp, (request.Test == 1))
if addedToExistingRequest {
return nil, nil
return requestsData, nil
}

url, err := a.makeURL(&adOceanExt, imp.ID, request, consentString)
slaveSizes := map[string]string{}
slaveSizes[adOceanExt.SlaveID] = getImpSizes(imp)

url, err := a.makeURL(&adOceanExt, imp, request, slaveSizes, consentString)
mmuras marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
return requestsData, err
}

headers := http.Header{}
Expand All @@ -147,54 +169,63 @@ func (a *AdOceanAdapter) makeRequest(existingRequests []*adapters.RequestData, i
headers.Add("Referer", request.Site.Page)
}

return &adapters.RequestData{
Method: "GET",
Uri: url,
Headers: headers,
}, nil
requestsData = append(requestsData, &requestData{
Url: url,
Headers: &headers,
SlaveSizes: slaveSizes,
})

return requestsData, nil
}

func addToExistingRequest(existingRequests []*adapters.RequestData, newParams *openrtb_ext.ExtImpAdOcean, auctionID string) bool {
requestsLoop:
for _, request := range existingRequests {
endpointURL, _ := url.Parse(request.Uri)
queryParams := endpointURL.Query()
func addToExistingRequest(requestsData []*requestData, newParams *openrtb_ext.ExtImpAdOcean, imp *openrtb.Imp, testImp bool) bool {
auctionID := imp.ID

for _, requestData := range requestsData {
queryParams := requestData.Url.Query()
masterID := queryParams["id"][0]

if masterID == newParams.MasterID {
aids := queryParams["aid"]
for _, aid := range aids {
slaveID := strings.SplitN(aid, ":", 2)[0]
if slaveID == newParams.SlaveID {
continue requestsLoop
}
if _, has := requestData.SlaveSizes[newParams.SlaveID]; has {
continue
}

queryParams.Add("aid", newParams.SlaveID+":"+auctionID)
endpointURL.RawQuery = queryParams.Encode()
newUri := endpointURL.String()
if len(newUri) < maxUriLength {
request.Uri = newUri
requestData.SlaveSizes[newParams.SlaveID] = getImpSizes(imp)
setSlaveSizesParam(&queryParams, requestData.SlaveSizes, testImp)

newUrl := *(requestData.Url)
newUrl.RawQuery = queryParams.Encode()
if len(newUrl.String()) < maxUriLength {
requestData.Url = &newUrl
return true
}

delete(requestData.SlaveSizes, newParams.SlaveID)
}
}

return false
}

func (a *AdOceanAdapter) makeURL(params *openrtb_ext.ExtImpAdOcean, auctionID string, request *openrtb.BidRequest, consentString string) (string, error) {
func (a *AdOceanAdapter) makeURL(
params *openrtb_ext.ExtImpAdOcean,
imp *openrtb.Imp,
request *openrtb.BidRequest,
slaveSizes map[string]string,
consentString string,
) (*url.URL, error) {
endpointParams := macros.EndpointTemplateParams{Host: params.EmitterDomain}
host, err := macros.ResolveMacros(a.endpointTemplate, endpointParams)
if err != nil {
return "", &errortypes.BadInput{
return nil, &errortypes.BadInput{
Message: "Unable to parse endpoint url template: " + err.Error(),
}
}

endpointURL, err := url.Parse(host)
if err != nil {
return "", &errortypes.BadInput{
return nil, &errortypes.BadInput{
Message: "Malformed URL: " + err.Error(),
}
}
Expand All @@ -205,6 +236,7 @@ func (a *AdOceanAdapter) makeURL(params *openrtb_ext.ExtImpAdOcean, auctionID st
}
endpointURL.Path = "/_" + strconv.Itoa(randomizedPart) + "/ad.json"

auctionID := imp.ID
queryParams := url.Values{}
queryParams.Add("pbsrv_v", adapterVersion)
queryParams.Add("id", params.MasterID)
Expand All @@ -218,9 +250,58 @@ func (a *AdOceanAdapter) makeURL(params *openrtb_ext.ExtImpAdOcean, auctionID st
if request.User != nil && request.User.BuyerUID != "" {
queryParams.Add("hcuserid", request.User.BuyerUID)
}

setSlaveSizesParam(&queryParams, slaveSizes, (request.Test == 1))
endpointURL.RawQuery = queryParams.Encode()

return endpointURL.String(), nil
return endpointURL, nil
}

func getImpSizes(imp *openrtb.Imp) string {
if imp.Banner == nil {
return ""
}

if len(imp.Banner.Format) > 0 {
sizes := make([]string, len(imp.Banner.Format))
for i, format := range imp.Banner.Format {
sizes[i] = strconv.FormatUint(format.W, 10) + "x" + strconv.FormatUint(format.H, 10)
}

return strings.Join(sizes, "_")
}

if imp.Banner.W != nil && imp.Banner.H != nil {
return strconv.FormatUint(*imp.Banner.W, 10) + "x" + strconv.FormatUint(*imp.Banner.H, 10)
}

return ""
}

func setSlaveSizesParam(queryParams *url.Values, slaveSizes map[string]string, orderByKey bool) {
sizeValues := make([]string, 0, len(slaveSizes))
slaveIDs := make([]string, 0, len(slaveSizes))
for k := range slaveSizes {
slaveIDs = append(slaveIDs, k)
}

if orderByKey {
sort.Strings(slaveIDs)
}

for _, slaveID := range slaveIDs {
sizes := slaveSizes[slaveID]
if sizes == "" {
continue
}

rawSlaveID := strings.Replace(slaveID, "adocean", "", 1)
sizeValues = append(sizeValues, rawSlaveID+"~"+sizes)
}

if len(sizeValues) > 0 {
queryParams.Set("aosspsizes", strings.Join(sizeValues, "-"))
}
}

func (a *AdOceanAdapter) MakeBids(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"format": [{
"w": 300,
"h": 250
}, {
"w": 320,
"h": 600
}]
}
}, {
Expand Down Expand Up @@ -68,7 +71,7 @@
},
"httpCalls": [{
"expectedRequest": {
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&aid=adoceanmyaowafpdwlrks%3Asecod-twelve&gdpr=1&gdpr_consent=COwK6gaOwK6gaFmAAAENAPCAAAAAAAAAAAAAAAAAAAAA.IFoEUQQgAIQwgIwQABAEAAAAOIAACAIAAAAQAIAgEAACEAAAAAgAQBAAAAAAAGBAAgAAAAAAAFAAECAAAgAAQARAEQAAAAAJAAIAAgAAAYQEAAAQmAgBC3ZAYzUw&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.0.0"
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&aid=adoceanmyaowafpdwlrks%3Asecod-twelve&aosspsizes=myaowafpdwlrks~300x250-myaozpniqismex~300x250_320x600&gdpr=1&gdpr_consent=COwK6gaOwK6gaFmAAAENAPCAAAAAAAAAAAAAAAAAAAAA.IFoEUQQgAIQwgIwQABAEAAAAOIAACAIAAAAQAIAgEAACEAAAAAgAQBAAAAAAAGBAAgAAAAAAAFAAECAAAgAAQARAEQAAAAAJAAIAAgAAAYQEAAAQmAgBC3ZAYzUw&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.1.0"
},
"mockResponse": {
"status": 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"httpCalls": [
{
"expectedRequest": {
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&gdpr=1&gdpr_consent=COwK6gaOwK6gaFmAAAENAPCAAAAAAAAAAAAAAAAAAAAA.IFoEUQQgAIQwgIwQABAEAAAAOIAACAIAAAAQAIAgEAACEAAAAAgAQBAAAAAAAGBAAgAAAAAAAFAAECAAAgAAQARAEQAAAAAJAAIAAgAAAYQEAAAQmAgBC3ZAYzUw&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.0.0"
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&aosspsizes=myaozpniqismex~300x250&gdpr=1&gdpr_consent=COwK6gaOwK6gaFmAAAENAPCAAAAAAAAAAAAAAAAAAAAA.IFoEUQQgAIQwgIwQABAEAAAAOIAACAIAAAAQAIAgEAACEAAAAAgAQBAAAAAAAGBAAgAAAAAAAFAAECAAAgAAQARAEQAAAAAJAAIAAgAAAYQEAAAQmAgBC3ZAYzUw&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.1.0"
},
"mockResponse": {
"status": 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"httpCalls": [
{
"expectedRequest": {
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.0.0"
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&aosspsizes=myaozpniqismex~300x250&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.1.0"
},
"mockResponse": {
"status": 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"httpCalls": [
{
"expectedRequest": {
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.0.0"
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&aosspsizes=myaozpniqismex~300x250&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.1.0"
},
"mockResponse": {
"status": 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"httpCalls": [
{
"expectedRequest": {
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.0.0"
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&aosspsizes=myaozpniqismex~300x250&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.1.0"
},
"mockResponse": {
"status": 500,
Expand Down
4 changes: 2 additions & 2 deletions adapters/adocean/adoceantest/supplemental/no-bid.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
},
"httpCalls": [{
"expectedRequest": {
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&aid=adoceanmyaowafpdwlrks%3Aao-test-two&gdpr=1&gdpr_consent=COwK6gaOwK6gaFmAAAENAPCAAAAAAAAAAAAAAAAAAAAA.IFoEUQQgAIQwgIwQABAEAAAAOIAACAIAAAAQAIAgEAACEAAAAAgAQBAAAAAAAGBAAgAAAAAAAFAAECAAAgAAQARAEQAAAAAJAAIAAgAAAYQEAAAQmAgBC3ZAYzUw&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.0.0"
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaozpniqismex%3Aao-test&aid=adoceanmyaowafpdwlrks%3Aao-test-two&aosspsizes=myaowafpdwlrks~300x250-myaozpniqismex~300x250&gdpr=1&gdpr_consent=COwK6gaOwK6gaFmAAAENAPCAAAAAAAAAAAAAAAAAAAAA.IFoEUQQgAIQwgIwQABAEAAAAOIAACAIAAAAQAIAgEAACEAAAAAgAQBAAAAAAAGBAAgAAAAAAAFAAECAAAgAAQARAEQAAAAAJAAIAAgAAAYQEAAAQmAgBC3ZAYzUw&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.1.0"
},
"mockResponse": {
"status": 200,
Expand All @@ -108,7 +108,7 @@
}
}, {
"expectedRequest": {
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaowafpdwlrks%3Aao-test-three&gdpr=1&gdpr_consent=COwK6gaOwK6gaFmAAAENAPCAAAAAAAAAAAAAAAAAAAAA.IFoEUQQgAIQwgIwQABAEAAAAOIAACAIAAAAQAIAgEAACEAAAAAgAQBAAAAAAAGBAAgAAAAAAAFAAECAAAgAAQARAEQAAAAAJAAIAAgAAAYQEAAAQmAgBC3ZAYzUw&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.0.0"
"uri": "https://myao.adocean.pl/_10000000/ad.json?aid=adoceanmyaowafpdwlrks%3Aao-test-three&aosspsizes=myaowafpdwlrks~300x250&gdpr=1&gdpr_consent=COwK6gaOwK6gaFmAAAENAPCAAAAAAAAAAAAAAAAAAAAA.IFoEUQQgAIQwgIwQABAEAAAAOIAACAIAAAAQAIAgEAACEAAAAAgAQBAAAAAAAGBAAgAAAAAAAFAAECAAAgAAQARAEQAAAAAJAAIAAgAAAYQEAAAQmAgBC3ZAYzUw&id=tmYF.DMl7ZBq.Nqt2Bq4FutQTJfTpxCOmtNPZoQUDcL.G7&nc=1&nosecure=1&pbsrv_v=1.1.0"
},
"mockResponse": {
"status": 200,
Expand Down
Loading