Skip to content

Commit

Permalink
support multiple impressions and change host in test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ownAdx-prebid committed Jul 12, 2023
1 parent 3ef3e38 commit 3bf0199
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 83 deletions.
163 changes: 113 additions & 50 deletions adapters/ownadx/ownadx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,47 @@ type bidExt struct {
MediaType string `json:"mediaType"`
}

func getRequestData(reqJSON []byte, url string) []*adapters.RequestData {
func (adapter *adapter) getRequestData(bidRequest *openrtb2.BidRequest, impExt *openrtb_ext.ExtImpOwnAdx, imps []openrtb2.Imp) (*adapters.RequestData, error) {
pbidRequest := createBidRequest(bidRequest, imps)
reqJSON, err := json.Marshal(pbidRequest)
if err != nil {
return nil, err
}
adapter.buildEndpointURL(impExt)
url, err := adapter.buildEndpointURL(impExt)
if err != nil {
return nil, err
}

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
headers.Add("x-openrtb-version", "2.5")

return []*adapters.RequestData{{
Method: http.MethodPost,
Body: reqJSON,
return &adapters.RequestData{
Method: "POST",
Uri: url,
Headers: headers,
}}
}
Body: reqJSON,
Headers: headers}, nil

}
func createBidRequest(rtbBidRequest *openrtb2.BidRequest, imps []openrtb2.Imp) *openrtb2.BidRequest {
bidRequest := *rtbBidRequest
bidRequest.Imp = imps
return &bidRequest
}
func getExtImps(imps []openrtb2.Imp, impsExt []openrtb_ext.ExtImpOwnAdx) map[openrtb_ext.ExtImpOwnAdx][]openrtb2.Imp {
respExt := make(map[openrtb_ext.ExtImpOwnAdx][]openrtb2.Imp)
for idx := range imps {
imp := imps[idx]
impExt := impsExt[idx]
if respExt[impExt] == nil {
respExt[impExt] = make([]openrtb2.Imp, 0)
}
respExt[impExt] = append(respExt[impExt], imp)
}
return respExt
}
func (adapter *adapter) buildEndpointURL(params *openrtb_ext.ExtImpOwnAdx) (string, error) {
endpointParams := macros.EndpointTemplateParams{
ZoneID: params.SspId,
Expand Down Expand Up @@ -62,30 +89,55 @@ func getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtImpOwnAdx, error) {
}

func (adapter *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var ownAdxExt *openrtb_ext.ExtImpOwnAdx
ownAdxExt, err := getImpressionExt(&(request.Imp[0]))
if err != nil {
return nil, []error{
httpBadResponseError(fmt.Sprintf("Bidder extension not valid or can't be unmarshalled")),
}
}

endPoint, err := adapter.buildEndpointURL(ownAdxExt)
if err != nil {
return nil, []error{err}
errs := make([]error, 0, len(request.Imp))
if len(request.Imp) == 0 {
errs = append(errs, &errortypes.BadInput{
Message: "No impression in the bid request"},
)
return nil, errs
}

reqJSON, err := json.Marshal(request)
if err != nil {
return nil, []error{err}
imps, impExts, err := getImpressionsAndImpExt(request.Imp)
if len(imps) == 0 {
return nil, err
}
errs = append(errs, err...)
if len(imps) == 0 {
return nil, err
}

return getRequestData(reqJSON, endPoint), nil
extImps := getExtImps(imps, impExts)

reqDetail := make([]*adapters.RequestData, 0, len(extImps))
for k, imps := range extImps {
bidRequest, err := adapter.getRequestData(request, &k, imps)
if err != nil {
errs = append(errs, err)
} else {
reqDetail = append(reqDetail, bidRequest)
}
}
return reqDetail, errs
}
func httpBadResponseError(message string) error {
return &errortypes.BadServerResponse{
Message: message,

func getImpressionsAndImpExt(imps []openrtb2.Imp) ([]openrtb2.Imp, []openrtb_ext.ExtImpOwnAdx, []error) {
impLen := len(imps)
errors := make([]error, 0, impLen)
rsImpExts := make([]openrtb_ext.ExtImpOwnAdx, 0, impLen)
rsImps := make([]openrtb2.Imp, 0, impLen)

for _, imp := range imps {
ownAdxExt, err := getImpressionExt(&(imp))
if err != nil {
errors = append(errors, err)
continue
}

rsImps = append(rsImps, imp)
rsImpExts = append(rsImpExts, *ownAdxExt)
}
return rsImps, rsImpExts, errors
}

func getBidType(ext bidExt) (openrtb_ext.BidType, error) {
Expand All @@ -98,55 +150,66 @@ func (adapter *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalR
}
if response.StatusCode == http.StatusBadRequest {
return nil, []error{
httpBadResponseError(fmt.Sprintf("Bad request: %d", response.StatusCode)),
&errortypes.BadServerResponse{
Message: fmt.Sprintf("Bad request: %d", response.StatusCode),
},
}
}
if response.StatusCode != http.StatusOK {
return nil, []error{
httpBadResponseError(fmt.Sprintf("Unexpected http status code: %d", response.StatusCode)),
&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.test = 1 for more info.", response.StatusCode),
},
}
}
var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{
httpBadResponseError(fmt.Sprintf("Bad server response ")),
&errortypes.BadServerResponse{
Message: fmt.Sprintf("Bad server response "),
},
}
}
if len(bidResp.SeatBid) == 0 {
return nil, []error{
httpBadResponseError(fmt.Sprintf("Array SeatBid cannot be empty ")),
&errortypes.BadServerResponse{
Message: fmt.Sprintf("Array SeatBid cannot be empty "),
},
}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)

if len(bidResp.SeatBid[0].Bid) == 0 {
seatBid := bidResp.SeatBid[0]
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(bidResp.SeatBid[0].Bid))
if len(seatBid.Bid) == 0 {
return nil, []error{
httpBadResponseError(fmt.Sprintf("Bid cannot be empty ")),
&errortypes.BadServerResponse{
Message: fmt.Sprintf("Bid cannot be empty "),
},
}
}
for i := 0; i < len(seatBid.Bid); i++ {
var bidExt bidExt
var bidType openrtb_ext.BidType
bid := seatBid.Bid[i]
if err := json.Unmarshal(bid.Ext, &bidExt); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: "BidExt is required",
}}
}

bid := bidResp.SeatBid[0].Bid[0]
var bidExt bidExt
var bidType openrtb_ext.BidType
bidType, err := getBidType(bidExt)

if err := json.Unmarshal(bid.Ext, &bidExt); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: "BidExt is required",
}}
if err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: "Bid type is invalid",
}}
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
})
}

bidType, err := getBidType(bidExt)

if err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: "Bid type is invalid",
}}
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
})
return bidResponse, nil
}

Expand Down
2 changes: 1 addition & 1 deletion adapters/ownadx/ownadx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderOwnAdx, config.Adapter{
Endpoint: "http://{{.Host}}/bidder/bid/{{.AccountID}}/000?token={{.SourceId}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
Endpoint: "https://pbs.prebid-ownadx.com/bidder/bid/{{.AccountID}}/{{.ZoneID}}?token={{.SourceId}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

assert.NoError(t, buildErr)
adapterstest.RunJSONBidderTest(t, "ownadxtest", bidder)
Expand Down
6 changes: 3 additions & 3 deletions adapters/ownadx/ownadxtest/exemplary/banner.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down Expand Up @@ -46,7 +46,7 @@
"httpCalls": [
{
"expectedRequest": {
"uri": "http://abc.com/bidder/bid/2/000?token=126151698247",
"uri": "https://pbs.prebid-ownadx.com/bidder/bid/2/11?token=126151698247",
"body": {
"id": "id",
"imp": [
Expand All @@ -62,7 +62,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down
6 changes: 3 additions & 3 deletions adapters/ownadx/ownadxtest/exemplary/video.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down Expand Up @@ -66,7 +66,7 @@
"httpCalls": [
{
"expectedRequest": {
"uri": "http://abc.com/bidder/bid/2/000?token=126151698247",
"uri": "https://pbs.prebid-ownadx.com/bidder/bid/2/11?token=126151698247",
"body": {
"id": "id",
"imp": [
Expand Down Expand Up @@ -102,7 +102,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down Expand Up @@ -46,7 +46,7 @@
"httpCalls": [
{
"expectedRequest": {
"uri": "http://abc.com/bidder/bid/2/000?token=126151698247",
"uri": "https://pbs.prebid-ownadx.com/bidder/bid/2/11?token=126151698247",
"body": {
"id": "id",
"imp": [
Expand All @@ -62,7 +62,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down
6 changes: 3 additions & 3 deletions adapters/ownadx/ownadxtest/supplemental/bid-empty-.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down Expand Up @@ -46,7 +46,7 @@
"httpCalls": [
{
"expectedRequest": {
"uri": "http://abc.com/bidder/bid/2/000?token=126151698247",
"uri": "https://pbs.prebid-ownadx.com/bidder/bid/2/11?token=126151698247",
"body": {
"id": "id",
"imp": [
Expand All @@ -62,7 +62,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down
6 changes: 3 additions & 3 deletions adapters/ownadx/ownadxtest/supplemental/bidext-empty-.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down Expand Up @@ -46,7 +46,7 @@
"httpCalls": [
{
"expectedRequest": {
"uri": "http://abc.com/bidder/bid/2/000?token=126151698247",
"uri": "https://pbs.prebid-ownadx.com/bidder/bid/2/11?token=126151698247",
"body": {
"id": "id",
"imp": [
Expand All @@ -62,7 +62,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down
6 changes: 3 additions & 3 deletions adapters/ownadx/ownadxtest/supplemental/bidext-type.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down Expand Up @@ -46,7 +46,7 @@
"httpCalls": [
{
"expectedRequest": {
"uri": "http://abc.com/bidder/bid/2/000?token=126151698247",
"uri": "https://pbs.prebid-ownadx.com/bidder/bid/2/11?token=126151698247",
"body": {
"id": "id",
"imp": [
Expand All @@ -62,7 +62,7 @@
"ext": {
"bidder": {
"tokenId": "126151698247",
"host": "abc.com",
"sspId": "11",
"seatId": "2"
}
}
Expand Down
Loading

0 comments on commit 3bf0199

Please sign in to comment.