-
Notifications
You must be signed in to change notification settings - Fork 802
Add Applogy adapter #1151
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
Merged
Merged
Add Applogy adapter #1151
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d4d0c76
Add Applogy adapter
IncSW 32462f1
fix comma
IncSW 69d921e
Applogy: request each impression
IncSW 5427c7b
update Applogy adapter
IncSW 8919520
return nil result when all impressions fails
IncSW ef73018
update Applogy tests
IncSW 6c2bd67
fix error message
IncSW 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 hidden or 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,162 @@ | ||
package applogy | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
"github.com/prebid/prebid-server/adapters" | ||
"github.com/prebid/prebid-server/errortypes" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
) | ||
|
||
type ApplogyAdapter struct { | ||
endpoint string | ||
} | ||
|
||
func (a *ApplogyAdapter) 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 | ||
totalImpressions := len(impressions) | ||
result := make([]*adapters.RequestData, 0, totalImpressions) | ||
errs := make([]error, 0, totalImpressions) | ||
impressionsByToken := make(map[string][]openrtb.Imp) | ||
|
||
for _, impression := range impressions { | ||
if impression.Banner == nil && impression.Video == nil && impression.Native == nil { | ||
errs = append(errs, &errortypes.BadInput{ | ||
Message: "Applogy only supports banner, video or native ads", | ||
}) | ||
continue | ||
} | ||
if impression.Banner != nil { | ||
banner := impression.Banner | ||
if banner.W == nil || banner.H == nil || *banner.W == 0 || *banner.H == 0 { | ||
if len(banner.Format) == 0 { | ||
errs = append(errs, &errortypes.BadInput{ | ||
Message: "banner size information missing", | ||
}) | ||
continue | ||
} | ||
format := banner.Format[0] | ||
banner.W = &format.W | ||
banner.H = &format.H | ||
} | ||
} | ||
IncSW marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(impression.Ext) == 0 { | ||
errs = append(errs, errors.New("impression extensions required")) | ||
continue | ||
} | ||
var bidderExt adapters.ExtImpBidder | ||
err := json.Unmarshal(impression.Ext, &bidderExt) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
if len(bidderExt.Bidder) == 0 { | ||
errs = append(errs, errors.New("Applogy token required")) | ||
IncSW marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue | ||
} | ||
var impressionExt openrtb_ext.ExtImpApplogy | ||
err = json.Unmarshal(bidderExt.Bidder, &impressionExt) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
if impressionExt.Token == "" { | ||
errs = append(errs, errors.New("Applogy token required")) | ||
continue | ||
} | ||
impressionsByToken[impressionExt.Token] = append(impressionsByToken[impressionExt.Token], impression) | ||
} | ||
|
||
for token, impressions := range impressionsByToken { | ||
request.Imp = impressions | ||
body, err := json.Marshal(request) | ||
if err != nil { | ||
errs = append(errs, err) | ||
return nil, errs | ||
IncSW marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
result = append(result, &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: a.endpoint + "/" + token, | ||
Body: body, | ||
Headers: headers, | ||
}) | ||
IncSW marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
IncSW marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
request.Imp = impressions | ||
return result, errs | ||
} | ||
|
||
func (a *ApplogyAdapter) 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) + ". Run with request.debug = 1 for more info", | ||
}} | ||
case http.StatusOK: | ||
break | ||
default: | ||
return nil, []error{&errortypes.BadServerResponse{ | ||
Message: "Unexpected status code: " + strconv.Itoa(responseData.StatusCode) + ". Run with request.debug = 1 for more info", | ||
}} | ||
} | ||
|
||
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 _, bid := range seatBid.Bid { | ||
bid := bid | ||
IncSW marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var bidType openrtb_ext.BidType | ||
for _, impression := range request.Imp { | ||
if impression.ID != bid.ImpID { | ||
continue | ||
} | ||
switch { | ||
case impression.Banner != nil: | ||
bidType = openrtb_ext.BidTypeBanner | ||
case impression.Video != nil: | ||
bidType = openrtb_ext.BidTypeVideo | ||
case impression.Native != nil: | ||
bidType = openrtb_ext.BidTypeNative | ||
} | ||
break | ||
} | ||
if bidType == "" { | ||
errs = append(errs, &errortypes.BadServerResponse{ | ||
Message: "ignoring bid id=" + bid.ID + ", request doesn't contain any valid impression with id=" + bid.ImpID, | ||
}) | ||
continue | ||
} | ||
response.Bids = append(response.Bids, &adapters.TypedBid{ | ||
Bid: &bid, | ||
BidType: bidType, | ||
}) | ||
} | ||
} | ||
|
||
return response, errs | ||
} | ||
|
||
func NewApplogyBidder(endpoint string) *ApplogyAdapter { | ||
return &ApplogyAdapter{ | ||
endpoint: endpoint, | ||
} | ||
} |
This file contains hidden or 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,11 @@ | ||
package applogy | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/adapters/adapterstest" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
adapterstest.RunJSONBidderTest(t, "applogytest", NewApplogyBidder("http://example.com/prebid")) | ||
} |
145 changes: 145 additions & 0 deletions
145
adapters/applogy/applogytest/exemplary/simple-banner.json
This file contains hidden or 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,145 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"imp": [{ | ||
"id": "test-impression-id-1", | ||
"banner": { | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-1" | ||
} | ||
} | ||
}, { | ||
"id": "test-impression-id-2", | ||
"banner": { | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-1" | ||
} | ||
} | ||
}, { | ||
"id": "test-impression-id-3", | ||
"banner": { | ||
"w": 300, | ||
"h": 250 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-2" | ||
} | ||
} | ||
}] | ||
}, | ||
"httpCalls": [{ | ||
"expectedRequest": { | ||
"uri": "http://example.com/prebid/test-token-1", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [{ | ||
"id": "test-impression-id-1", | ||
"banner": { | ||
"h": 250, | ||
"w": 300 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-1" | ||
} | ||
} | ||
}, { | ||
"id": "test-impression-id-2", | ||
"banner": { | ||
"h": 250, | ||
"w": 300 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-1" | ||
} | ||
} | ||
}] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-response-id", | ||
"seatbid": [{ | ||
"bid": [{ | ||
"id": "test-bid-id-1", | ||
"impid": "test-impression-id-1", | ||
"price": 1 | ||
}, { | ||
"id": "test-bid-id-2", | ||
"impid": "test-impression-id-2", | ||
"price": 2 | ||
}] | ||
}] | ||
} | ||
} | ||
}, { | ||
"expectedRequest": { | ||
"uri": "http://example.com/prebid/test-token-2", | ||
"body": { | ||
"id": "test-request-id", | ||
"imp": [{ | ||
"id": "test-impression-id-3", | ||
"banner": { | ||
"h": 250, | ||
"w": 300 | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"token": "test-token-2" | ||
} | ||
} | ||
}] | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-response-id-3", | ||
"seatbid": [{ | ||
"bid": [{ | ||
"id": "test-bid-id-3", | ||
"impid": "test-impression-id-3", | ||
"price": 3 | ||
}] | ||
}] | ||
} | ||
} | ||
}], | ||
"expectedBidResponses": [{ | ||
"bids": [{ | ||
"bid": { | ||
"id": "test-bid-id-1", | ||
"impid": "test-impression-id-1", | ||
"price": 1 | ||
}, | ||
"type": "banner" | ||
}, { | ||
"bid": { | ||
"id": "test-bid-id-2", | ||
"impid": "test-impression-id-2", | ||
"price": 2 | ||
}, | ||
"type": "banner" | ||
}] | ||
},{ | ||
"bids": [ { | ||
"bid": { | ||
"id": "test-bid-id-3", | ||
"impid": "test-impression-id-3", | ||
"price": 3 | ||
}, | ||
"type": "banner" | ||
}] | ||
}] | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.