-
Notifications
You must be signed in to change notification settings - Fork 749
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
Improved error handling on Beachfront adapter #873
Changes from all commits
1eb50e8
ac3d6da
73fc7f0
b2a73f1
8422a0b
43a85af
bf20d8b
963ddd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,13 @@ package beachfront | |
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/prebid/prebid-server/errortypes" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/prebid/prebid-server/adapters" | ||
"github.com/prebid/prebid-server/errortypes" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
|
||
"github.com/mxmCherry/openrtb" | ||
|
@@ -22,7 +24,7 @@ const VideoEndpoint = "https://reachms.bfmio.com/bid.json?exchange_id=" | |
const VideoEndpointSuffix = "&prebidserver" | ||
|
||
const beachfrontAdapterName = "BF_PREBID_S2S" | ||
const beachfrontAdapterVersion = "0.2.2" | ||
const beachfrontAdapterVersion = "0.3.0" | ||
|
||
type BeachfrontAdapter struct { | ||
} | ||
|
@@ -140,15 +142,14 @@ func (a *BeachfrontAdapter) MakeRequests(request *openrtb.BidRequest) ([]*adapte | |
var beachfrontRequests BeachfrontRequests | ||
var reqJSON []byte | ||
var uri string | ||
var errs = make([]error, 0) | ||
var errs = make([]error, 0, len(request.Imp)) | ||
var err error | ||
var imps int | ||
|
||
uri = getEndpoint(request) | ||
|
||
beachfrontRequests, errs, imps = preprocess(request, uri) | ||
|
||
// These are fatal errors ------------- | ||
if uri == VideoEndpoint { | ||
reqJSON, err = json.Marshal(beachfrontRequests.Video) | ||
uri = uri + beachfrontRequests.Video.AppId + VideoEndpointSuffix | ||
|
@@ -175,6 +176,15 @@ func (a *BeachfrontAdapter) MakeRequests(request *openrtb.BidRequest) ([]*adapte | |
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
headers.Add("Accept", "application/json") | ||
|
||
if request.Device != nil { | ||
addHeaderIfNonEmpty(headers, "User-Agent", request.Device.UA) | ||
addHeaderIfNonEmpty(headers, "X-Forwarded-For", request.Device.IP) | ||
addHeaderIfNonEmpty(headers, "Accept-Language", request.Device.Language) | ||
if request.Device.DNT != nil { | ||
addHeaderIfNonEmpty(headers, "DNT", strconv.Itoa(int(*request.Device.DNT))) | ||
} | ||
} | ||
|
||
return []*adapters.RequestData{{ | ||
Method: "POST", | ||
Uri: uri, | ||
|
@@ -288,9 +298,6 @@ func getBannerRequest(req *openrtb.BidRequest) (BeachfrontBannerRequest, []error | |
return beachfrontReq, errs, imps | ||
} | ||
|
||
/* | ||
Prepare the request that has been received from Prebid.js, translating it to the beachfront format | ||
*/ | ||
func getVideoRequest(req *openrtb.BidRequest) (BeachfrontVideoRequest, []error, int) { | ||
var videoImpsIndex = 0 | ||
var beachfrontReq = NewBeachfrontVideoRequest() | ||
|
@@ -324,7 +331,7 @@ func getVideoRequest(req *openrtb.BidRequest) (BeachfrontVideoRequest, []error, | |
The req could contain banner,audio,native and video imps when It arrives here. I am only | ||
interested in video | ||
|
||
The beach front video endpoint is only capable of returning a single nurl and price, wrapped in | ||
The beachfront video endpoint is only capable of returning a single nurl and price, wrapped in | ||
an openrtb format, so even though I'm building a request here that will include multiple video | ||
impressions, only a single URL will be returned. Hopefully the beachfront endpoint can be modified | ||
in the future to return multiple video ads | ||
|
@@ -394,12 +401,33 @@ func getVideoRequest(req *openrtb.BidRequest) (BeachfrontVideoRequest, []error, | |
func (a *BeachfrontAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
var bids []openrtb.Bid | ||
var bidtype = getBidType(internalRequest) | ||
|
||
/* | ||
Beachfront is now sending an empty array and 200 as their "no results" response. This should catch that. | ||
*/ | ||
|
||
if response.StatusCode == http.StatusOK && len(response.Body) <= 2 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why we can't just check |
||
return nil, nil | ||
} | ||
|
||
if response.StatusCode == http.StatusNoContent { | ||
return nil, nil | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we reduce:
To a single statement?
|
||
if response.StatusCode == http.StatusBadRequest { | ||
return nil, []error{&errortypes.BadInput{ | ||
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode), | ||
}} | ||
} | ||
|
||
if response.StatusCode != http.StatusOK { | ||
return nil, []error{fmt.Errorf("Unexpected status code: %d. Run with request.debug = 1 for more info", response.StatusCode)} | ||
} | ||
|
||
bids, errs := postprocess(response, externalRequest, internalRequest.ID, bidtype) | ||
|
||
if len(errs) != 0 { | ||
return nil, append(errs, &errortypes.BadServerResponse{ | ||
Message: "Failed to process the beachfront response", | ||
}) | ||
return nil, errs | ||
} | ||
|
||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(BidCapacity) | ||
|
@@ -499,6 +527,13 @@ func extractVideoCrid(nurl string) string { | |
return strings.TrimSuffix(chunky[2], ":") | ||
} | ||
|
||
// Thank you, brightroll. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I ask what's the purpose of this comment? If it's just a leftover of some sort, can we please remove it? Thanks! |
||
func addHeaderIfNonEmpty(headers http.Header, headerName string, headerValue string) { | ||
if len(headerValue) > 0 { | ||
headers.Add(headerName, headerValue) | ||
} | ||
} | ||
|
||
func NewBeachfrontBidder() *BeachfrontAdapter { | ||
return &BeachfrontAdapter{} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "some_test_ad", | ||
"site": { | ||
"page": "https://test.opposingviews.com/i/society/republican-sen-collins-may-change-vote-tax-bill?cb=1234534" | ||
}, | ||
"imp": [ | ||
{ | ||
"bidfloor": 0.02, | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"bidfloor": 0.02, | ||
"appId": "00000000-1111-2222-3333-11111111111" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
|
||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "https://display.bfmio.com/prebid_display", | ||
"body": { | ||
"slots": [ | ||
{ | ||
"slot": "", | ||
"id": "00000000-1111-2222-3333-11111111111", | ||
"bidfloor": 0.02, | ||
"sizes": [ | ||
{ | ||
"w": 300, | ||
"h": 250 | ||
} | ||
] | ||
} | ||
], | ||
"domain": "test.opposingviews.com", | ||
"page": "https://test.opposingviews.com/i/society/republican-sen-collins-may-change-vote-tax-bill?cb=1234534", | ||
"referrer": "", | ||
"search": "", | ||
"secure": 0, | ||
"requestId": "some_test_ad", | ||
"isMobile": 0, | ||
"ip": "", | ||
"deviceModel": "", | ||
"deviceOs": "", | ||
"dnt": 0, | ||
"ua": "", | ||
"adapterName": "BF_PREBID_S2S", | ||
"adapterVersion": "0.3.0", | ||
"user": { | ||
} | ||
} | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": [] | ||
} | ||
} | ||
], | ||
|
||
"expectedBids": [] | ||
} |
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.
I'm not sure if it's uncommon to have three or more Imps in a given request but, according to the code flow, I believe We'll have at most two Error object elements in this slice, so maybe we are wastiong memory by assigning more than we need whenever we have three or more Imps in a request