Skip to content

Commit

Permalink
zentotem: fix after review
Browse files Browse the repository at this point in the history
fix after review
  • Loading branch information
GermanBogatov committed Aug 22, 2024
1 parent 6f3f47c commit 536f118
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 151 deletions.
62 changes: 12 additions & 50 deletions adapters/zentotem/zentotem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/errortypes"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"net/http"
)

type adapter struct {
Expand All @@ -24,13 +22,6 @@ func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server co
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
if request == nil {
return nil, []error{&errortypes.BadInput{Message: "empty request"}}
}
if len(request.Imp) == 0 {
return nil, []error{&errortypes.BadInput{Message: "empty request.Imp"}}
}

requests := make([]*adapters.RequestData, 0, len(request.Imp))
var errors []error

Expand All @@ -55,48 +46,27 @@ func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapte
return requests, errors
}

func getMediaTypeForBid(imp openrtb2.Imp) (openrtb_ext.BidType, error) {
if imp.Native != nil {
return openrtb_ext.BidTypeNative, nil
}

if imp.Banner != nil {
func getMediaTypeForBid(bid openrtb2.Bid) (openrtb_ext.BidType, error) {
switch bid.MType {
case openrtb2.MarkupBanner:
return openrtb_ext.BidTypeBanner, nil
}

if imp.Video != nil {
case openrtb2.MarkupVideo:
return openrtb_ext.BidTypeVideo, nil
case openrtb2.MarkupNative:
return openrtb_ext.BidTypeNative, nil
}

return "", &errortypes.BadInput{
Message: fmt.Sprintf("Processing an invalid impression; cannot resolve impression type for imp #%s", imp.ID),
}
return "", fmt.Errorf("could not define media type for impression: %s", bid.ImpID)
}

func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if responseData.StatusCode == http.StatusNoContent {
if adapters.IsResponseStatusCodeNoContent(responseData) {
return nil, nil
}

if responseData.StatusCode == http.StatusBadRequest {
err := &errortypes.BadInput{
Message: "Unexpected status code: 400. Bad request from publisher. Run with request.debug = 1 for more info.",
}
return nil, []error{err}
}

if responseData.StatusCode != http.StatusOK {
err := &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d. Run with request.debug = 1 for more info.", responseData.StatusCode),
}
if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
return nil, []error{err}
}

impMap := map[string]openrtb2.Imp{}
for _, imp := range request.Imp {
impMap[imp.ID] = imp
}

var response openrtb2.BidResponse
if err := json.Unmarshal(responseData.Body, &response); err != nil {
return nil, []error{err}
Expand All @@ -106,23 +76,15 @@ func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.R
bidResponse.Currency = response.Cur
var errors []error
for _, seatBid := range response.SeatBid {
for i, bid := range seatBid.Bid {
imp, ok := impMap[bid.ImpID]
if !ok {
errors = append(errors, &errortypes.BadInput{
Message: fmt.Sprintf("Invalid bid imp ID #%s does not match any imp IDs from the original bid request", bid.ImpID),
})
continue
}

bidType, err := getMediaTypeForBid(imp)
for _, bid := range seatBid.Bid {
bidType, err := getMediaTypeForBid(bid)
if err != nil {
errors = append(errors, err)
continue
}

bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[i],
Bid: &bid,
BidType: bidType,
})
}
Expand Down
27 changes: 13 additions & 14 deletions adapters/zentotem/zentotem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,44 @@ func TestJsonSamples(t *testing.T) {
func TestGetMediaTypeForBid(t *testing.T) {
tests := []struct {
name string
imp openrtb2.Imp
bid openrtb2.Bid
wantType openrtb_ext.BidType
wantErr bool
}{
{
name: "get bid native type",
imp: openrtb2.Imp{
Native: &openrtb2.Native{
Request: "test",
},
bid: openrtb2.Bid{
MType: 4,
},
wantType: openrtb_ext.BidTypeNative,
wantErr: false,
},
{
name: "get bid banner type",
imp: openrtb2.Imp{
Banner: &openrtb2.Banner{
ID: "test",
},
bid: openrtb2.Bid{
MType: 1,
},
wantType: openrtb_ext.BidTypeBanner,
wantErr: false,
},
{
name: "get bid video type",
imp: openrtb2.Imp{
Video: &openrtb2.Video{
PodID: "test",
},
bid: openrtb2.Bid{
MType: 2,
},
wantType: openrtb_ext.BidTypeVideo,
wantErr: false,
},
{
name: "fail",
wantType: "",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bType, err := getMediaTypeForBid(tt.imp)
bType, err := getMediaTypeForBid(tt.bid)
if (err != nil) != tt.wantErr {
t.Errorf("getMediaTypeForBid error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
32 changes: 19 additions & 13 deletions adapters/zentotem/zentotemtest/exemplary/banner-200-212.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@
"adomain": [
"app.apple.com"
],
"crid": "36"
"crid": "36",
"mtype": 1
}
],
"seat": "boldwin"
Expand All @@ -172,19 +173,24 @@
],
"expectedBidResponses": [
{
"bid": [
"currency": "USD",
"bids": [
{
"id": "36",
"impid": "1",
"price": 150,
"nurl": "some-test-nurl",
"burl": "some-test-burl",
"adm": "some-test-adm",
"adid": "36",
"adomain": [
"app.apple.com"
],
"crid": "36"
"bid": {
"id": "36",
"impid": "1",
"price": 150,
"nurl": "some-test-nurl",
"burl": "some-test-burl",
"adm": "some-test-adm",
"adid": "36",
"adomain": [
"app.apple.com"
],
"crid": "36",
"mtype": 1
},
"type": "banner"
}
]
}
Expand Down
32 changes: 19 additions & 13 deletions adapters/zentotem/zentotemtest/exemplary/banner-201-202-203.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@
"adomain": [
"app.apple.com"
],
"crid": "38"
"crid": "38",
"mtype": 1
}
],
"seat": "boldwin"
Expand All @@ -170,19 +171,24 @@
],
"expectedBidResponses": [
{
"bid": [
"currency": "USD",
"bids": [
{
"id": "38",
"impid": "1",
"price": 150,
"nurl": "some-test-nurl",
"burl": "some-test-burl",
"adm": "some-test-adm",
"adid": "38",
"adomain": [
"app.apple.com"
],
"crid": "38"
"bid": {
"id": "38",
"impid": "1",
"price": 150,
"nurl": "some-test-nurl",
"burl": "some-test-burl",
"adm": "some-test-adm",
"adid": "38",
"adomain": [
"app.apple.com"
],
"crid": "38",
"mtype": 1
},
"type": "banner"
}
]
}
Expand Down
36 changes: 21 additions & 15 deletions adapters/zentotem/zentotemtest/exemplary/banner-207.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@
],
"crid": "40",
"w": 320,
"h": 50
"h": 50,
"mtype": 1
}
],
"seat": "boldwin"
Expand All @@ -176,21 +177,26 @@
],
"expectedBidResponses": [
{
"bid": [
"currency": "USD",
"bids": [
{
"id": "40",
"impid": "1",
"price": 150,
"nurl": "some-test-nurl",
"burl": "some-test-burl",
"adm": "some-test-adm",
"adid": "40",
"adomain": [
"app.apple.com"
],
"crid": "40",
"w": 320,
"h": 50
"bid": {
"id": "40",
"impid": "1",
"price": 150,
"nurl": "some-test-nurl",
"burl": "some-test-burl",
"adm": "some-test-adm",
"adid": "40",
"adomain": [
"app.apple.com"
],
"crid": "40",
"w": 320,
"h": 50,
"mtype": 1
},
"type": "banner"
}
]
}
Expand Down
36 changes: 21 additions & 15 deletions adapters/zentotem/zentotemtest/exemplary/native.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
],
"crid": "40",
"w": 320,
"h": 50
"h": 50,
"mtype": 4
}
],
"seat": "boldwin"
Expand All @@ -138,21 +139,26 @@
],
"expectedBidResponses": [
{
"bid": [
"currency": "USD",
"bids": [
{
"id": "40",
"impid": "1",
"price": 150,
"nurl": "some-test-nurl",
"burl": "some-test-burl",
"adm": "some-test-adm",
"adid": "40",
"adomain": [
"app.apple.com"
],
"crid": "40",
"w": 320,
"h": 50
"bid": {
"id": "40",
"impid": "1",
"price": 150,
"nurl": "some-test-nurl",
"burl": "some-test-burl",
"adm": "some-test-adm",
"adid": "40",
"adomain": [
"app.apple.com"
],
"crid": "40",
"w": 320,
"h": 50,
"mtype": 4
},
"type": "native"
}
]
}
Expand Down
Loading

0 comments on commit 536f118

Please sign in to comment.