Skip to content

Commit

Permalink
Merge branch 'ci' of github.com:PubMatic-OpenWrap/prebid-server into …
Browse files Browse the repository at this point in the history
…OTT-1768
  • Loading branch information
Pubmatic-Supriya-Patil committed Aug 5, 2024
2 parents 4acaca2 + 8efe208 commit 7441033
Show file tree
Hide file tree
Showing 59 changed files with 5,758 additions and 310 deletions.
2 changes: 1 addition & 1 deletion adapters/ortbbidder/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ const (
// constants to set values in adapter response
const (
currencyKey = "Currency"
typeBidKey = "Bid"
typedbidKey = "Bid"
bidsKey = "Bids"
)
27 changes: 0 additions & 27 deletions adapters/ortbbidder/errors.go

This file was deleted.

9 changes: 5 additions & 4 deletions adapters/ortbbidder/multi_request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/util"
"github.com/prebid/prebid-server/v2/util/jsonutil"
)

Expand All @@ -17,7 +18,7 @@ type multiRequestBuilder struct {
// parseRequest parse the incoming request and populates intermediate fields required for building requestData object
func (rb *multiRequestBuilder) parseRequest(request *openrtb2.BidRequest) (err error) {
if len(request.Imp) == 0 {
return errImpMissing
return util.ErrImpMissing
}

//get rawrequests without impression objects
Expand Down Expand Up @@ -56,7 +57,7 @@ func (rb *multiRequestBuilder) makeRequest() (requestData []*adapters.RequestDat
//step 1: clone request
if requestCloneRequired {
if newRequest, err = cloneRequest(rb.rawRequest); err != nil {
errs = append(errs, newBadInputError(err.Error()))
errs = append(errs, util.NewBadInputError(err.Error()))
continue
}
}
Expand All @@ -67,7 +68,7 @@ func (rb *multiRequestBuilder) makeRequest() (requestData []*adapters.RequestDat

//step 3: get endpoint
if endpoint, err = rb.getEndpoint(bidderParams); err != nil {
errs = append(errs, newBadInputError(err.Error()))
errs = append(errs, util.NewBadInputError(err.Error()))
continue
}

Expand All @@ -81,7 +82,7 @@ func (rb *multiRequestBuilder) makeRequest() (requestData []*adapters.RequestDat
}
//step 5: append new request data
if requestData, err = appendRequestData(requestData, newRequest, endpoint, []string{imp[idKey].(string)}); err != nil {
errs = append(errs, newBadInputError(err.Error()))
errs = append(errs, util.NewBadInputError(err.Error()))
}
}
return requestData, errs
Expand Down
5 changes: 3 additions & 2 deletions adapters/ortbbidder/multi_request_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/bidderparams"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/util"
"github.com/stretchr/testify/assert"
)

Expand All @@ -35,7 +36,7 @@ func TestMultiRequestBuilderParseRequest(t *testing.T) {
},
},
want: want{
err: errImpMissing,
err: util.ErrImpMissing,
rawRequest: nil,
imps: nil,
},
Expand Down Expand Up @@ -214,7 +215,7 @@ func TestMultiRequestBuilderMakeRequest(t *testing.T) {

want: want{
requestData: nil,
errs: []error{newBadInputError("failed to replace macros in endpoint, err:template: endpointTemplate:1:2: " +
errs: []error{util.NewBadInputError("failed to replace macros in endpoint, err:template: endpointTemplate:1:2: " +
"executing \"endpointTemplate\" at <errorFunc>: error calling errorFunc: intentional error")},
},
},
Expand Down
26 changes: 13 additions & 13 deletions adapters/ortbbidder/ortbbidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/bidderparams"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/util"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/errortypes"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/prebid/prebid-server/v2/util/jsonutil"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server co
// MakeRequests prepares oRTB bidder-specific request information using which prebid server make call(s) to bidder.
func (o *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
if o.bidderParamsConfig == nil {
return nil, []error{newBadInputError(errNilBidderParamCfg.Error())}
return nil, []error{util.NewBadInputError(util.ErrNilBidderParamCfg.Error())}
}

requestBuilder := newRequestBuilder(
Expand All @@ -72,7 +73,7 @@ func (o *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapte
o.bidderParamsConfig.GetRequestParams(o.bidderName.String()))

if err := requestBuilder.parseRequest(request); err != nil {
return nil, []error{newBadInputError(err.Error())}
return nil, []error{util.NewBadInputError(err.Error())}
}

return requestBuilder.makeRequest()
Expand All @@ -88,25 +89,24 @@ func (o *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.R
return nil, []error{err}
}

response, err := o.makeBids(request, responseData.Body)
if err != nil {
return nil, []error{newBadServerResponseError(err.Error())}
}

return response, nil
return o.makeBids(request, responseData.Body)
}

// makeBids converts the bidderResponseBytes to a BidderResponse
// It retrieves response parameters, creates a response builder, parses the response, and builds the response.
// Finally, it converts the response builder's internal representation to an AdapterResponse and returns it.
func (o *adapter) makeBids(request *openrtb2.BidRequest, bidderResponseBytes json.RawMessage) (*adapters.BidderResponse, error) {
func (o *adapter) makeBids(request *openrtb2.BidRequest, bidderResponseBytes json.RawMessage) (*adapters.BidderResponse, []error) {
responseParmas := o.bidderParamsConfig.GetResponseParams(o.bidderName.String())
rb := newResponseBuilder(responseParmas, request)

err := rb.setPrebidBidderResponse(bidderResponseBytes)
if err != nil {
return nil, err
errs := rb.setPrebidBidderResponse(bidderResponseBytes)
if errortypes.ContainsFatalError(errs) {
return nil, errs
}

return rb.buildAdapterResponse()
bidderResponse, err := rb.buildAdapterResponse()
if err != nil {
errs = append(errs, err)
}
return bidderResponse, errs
}
7 changes: 4 additions & 3 deletions adapters/ortbbidder/ortbbidder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/bidderparams"
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/util"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/errortypes"
"github.com/prebid/prebid-server/v2/openrtb_ext"
Expand Down Expand Up @@ -148,7 +149,7 @@ func TestMakeRequests(t *testing.T) {
bidderCfg: bidderparams.NewBidderConfig(),
},
want: want{
errors: []error{newBadInputError(errImpMissing.Error())},
errors: []error{util.NewBadInputError(util.ErrImpMissing.Error())},
},
},
{
Expand All @@ -162,7 +163,7 @@ func TestMakeRequests(t *testing.T) {
bidderCfg: nil,
},
want: want{
errors: []error{newBadInputError("found nil bidderParamsConfig")},
errors: []error{util.NewBadInputError("found nil bidderParamsConfig")},
},
},
{
Expand Down Expand Up @@ -619,7 +620,7 @@ func TestMakeBids(t *testing.T) {
bc := bidderparams.NewBidderConfig()
bc.BidderConfigMap["owortb_testbidder"] = &bidderparams.Config{
ResponseParams: map[string]bidderparams.BidderParamMapper{
"bidtype": {Location: "seatbid.#.bid.#.ext.bidtype"},
"bidType": {Location: "seatbid.#.bid.#.ext.bidtype"},
"currency": {Location: "ext.currency"},
},
}
Expand Down
152 changes: 152 additions & 0 deletions adapters/ortbbidder/resolver/bidVideo_resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package resolver

import (
"github.com/prebid/prebid-server/v2/adapters/ortbbidder/util"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/prebid/prebid-server/v2/util/jsonutil"
)

// bidVideoResolver determines the duration of the bid by retrieving the video field using the bidder param location.
// The determined video field is subsequently assigned to adapterresponse.typedbid.bidvideo
type bidVideoResolver struct {
paramResolver
}

func (b *bidVideoResolver) retrieveFromBidderParamLocation(responseNode map[string]any, path string) (any, error) {
value, found := util.GetValueFromLocation(responseNode, path)
if !found {
return nil, NewDefaultValueError("no value sent by bidder at [%s] for [bid.ext.prebid.video]", path)
}
video, err := validateBidVideo(value)
if err != nil {
return nil, NewValidationFailedError("invalid value sent by bidder at [%s] for [bid.ext.prebid.video]", path)
}
return video, nil
}

func validateBidVideo(value any) (any, error) {
bidVideoBytes, err := jsonutil.Marshal(value)
if err != nil {
return nil, err
}

var bidVideo openrtb_ext.ExtBidPrebidVideo
err = jsonutil.UnmarshalValid(bidVideoBytes, &bidVideo)
if err != nil {
return nil, err
}

var bidVideoMap map[string]any
err = jsonutil.UnmarshalValid(bidVideoBytes, &bidVideoMap)
if err != nil {
return nil, err
}
return bidVideoMap, nil
}

func (b *bidVideoResolver) setValue(adapterBid map[string]any, value any) error {
adapterBid[bidVideoKey] = value
return nil
}

// bidVideoDurationResolver determines the duration of the bid based on the following hierarchy:
// 1. It first attempts to retrieve the bid type from the response.seat.bid.dur location.
// 2. If not found, it then tries to retrieve the duration using the bidder param location.
// The determined bid duration is subsequently assigned to adapterresponse.typedbid.bidvideo.dur
type bidVideoDurationResolver struct {
paramResolver
}

func (b *bidVideoDurationResolver) getFromORTBObject(bid map[string]any) (any, error) {
value, ok := bid[ortbFieldDuration]
if !ok {
return nil, NewDefaultValueError("no value sent by bidder at [bid.dur] for [bid.ext.prebid.video.duration]")
}

duration, ok := validateNumber[int64](value)
if !ok {
return nil, NewValidationFailedError("invalid value sent by bidder at [bid.dur] for [bid.ext.prebid.video.duration]")
}

if duration == 0 {
return nil, NewDefaultValueError("default value sent by bidder at [bid.dur] for [bid.ext.prebid.video.duration]")
}
return duration, nil
}

func (b *bidVideoDurationResolver) retrieveFromBidderParamLocation(responseNode map[string]any, path string) (any, error) {
value, found := util.GetValueFromLocation(responseNode, path)
if !found {
return nil, NewDefaultValueError("no value sent by bidder at [%s] for [bid.ext.prebid.video.duration]", path)
}
duration, ok := validateNumber[int64](value)
if !ok {
return nil, NewValidationFailedError("invalid value sent by bidder at [%s] for [bid.ext.prebid.video.duration]", path)
}
return duration, nil
}

func (b *bidVideoDurationResolver) setValue(adapterBid map[string]any, value any) error {
return setKeyValueInBidVideo(adapterBid, bidVideoDurationKey, value)
}

// bidVideoPrimaryCategoryResolver determines the primary-category of the bid based on the following hierarchy:
// 1. It first attempts to retrieve the bid category from the response.seat.bid.cat[0] location.
// 2. If not found, it then tries to retrieve the primary category using the bidder param location.
// The determined category is subsequently assigned to adapterresponse.typedbid.bidvideo.primary_category
type bidVideoPrimaryCategoryResolver struct {
paramResolver
}

func (b *bidVideoPrimaryCategoryResolver) getFromORTBObject(bid map[string]any) (any, error) {
value, found := bid[ortbFieldCategory]
if !found {
return nil, NewDefaultValueError("no value sent by bidder at [bid.cat] for [bid.ext.prebid.video.primary_category]")
}

categories, ok := value.([]any)
if !ok {
return nil, NewValidationFailedError("invalid value sent by bidder at [bid.cat] for [bid.ext.prebid.video.primary_category]")
}

if len(categories) == 0 {
return nil, NewDefaultValueError("default value sent by bidder at [bid.cat] for [bid.ext.prebid.video.primary_category]")
}

category, _ := categories[0].(string)
if len(category) == 0 {
return nil, NewValidationFailedError("invalid value sent by bidder at [bid.cat[0]] for [bid.ext.prebid.video.primary_category]")
}

return category, nil
}

func (b *bidVideoPrimaryCategoryResolver) retrieveFromBidderParamLocation(responseNode map[string]any, path string) (any, error) {
value, found := util.GetValueFromLocation(responseNode, path)
if !found {
return nil, NewDefaultValueError("no value sent by bidder at [%s] for [bid.ext.prebid.video.primary_category]", path)
}
category, ok := value.(string)
if !ok {
return nil, NewValidationFailedError("invalid value sent by bidder at [%s] for [bid.ext.prebid.video.primary_category]", path)
}
return category, nil
}

func (b *bidVideoPrimaryCategoryResolver) setValue(adapterBid map[string]any, value any) error {
return setKeyValueInBidVideo(adapterBid, bidVideoPrimaryCategoryKey, value)
}

func setKeyValueInBidVideo(adapterBid map[string]any, key string, value any) error {
video, found := adapterBid[bidVideoKey]
if !found {
video = map[string]any{}
adapterBid[bidVideoKey] = video
}
videoTyped, ok := video.(map[string]any)
if !ok || videoTyped == nil {
return NewValidationFailedError("failed to set key:[%s] in BidVideo, error:[incorrect data type]", key)
}
videoTyped[key] = value
return nil
}
Loading

0 comments on commit 7441033

Please sign in to comment.