Skip to content
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

Missena: Update params #4057

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 135 additions & 56 deletions adapters/missena/missena.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,170 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"text/template"

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v3/adapters"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/errortypes"
"github.com/prebid/prebid-server/v3/macros"
"github.com/prebid/prebid-server/v3/openrtb_ext"
"github.com/prebid/prebid-server/v3/util/jsonutil"
"github.com/prebid/prebid-server/v3/version"
)

type adapter struct {
endpoint string
EndpointTemplate *template.Template
}

type MissenaAdRequest struct {
RequestId string `json:"request_id"`
Timeout int `json:"timeout"`
Referer string `json:"referer"`
RefererCanonical string `json:"referer_canonical"`
GDPRConsent string `json:"consent_string"`
GDPR bool `json:"consent_required"`
Placement string `json:"placement"`
TestMode string `json:"test"`
Adunit string `json:"adunit,omitempty"`
COPPA int8 `json:"coppa,omitempty"`
Currency string `json:"currency,omitempty"`
EIDs []openrtb2.EID `json:"userEids,omitempty"`
Floor float64 `json:"floor,omitempty"`
FloorCurrency string `json:"floor_currency,omitempty"`
GDPR bool `json:"consent_required,omitempty"`
GDPRConsent string `json:"consent_string,omitempty"`
IdempotencyKey string `json:"ik,omitempty"`
Referer string `json:"referer,omitempty"`
RefererCanonical string `json:"referer_canonical,omitempty"`
RequestID string `json:"request_id,omitempty"`
SChain *openrtb2.SupplyChain `json:"schain,omitempty"`
Timeout int64 `json:"timeout,omitempty"`
URL string `json:"url,omitempty"`
UserParams UserParams `json:"params"`
USPrivacy string `json:"us_privacy,omitempty"`
Version string `json:"version,omitempty"`
}

type MissenaBidServerResponse struct {
type BidServerResponse struct {
Ad string `json:"ad"`
Cpm float64 `json:"cpm"`
Currency string `json:"currency"`
RequestId string `json:"requestId"`
RequestID string `json:"requestId"`
}

type MissenaInternalParams struct {
ApiKey string
RequestId string
Timeout int
Referer string
RefererCanonical string
GDPRConsent string
GDPR bool
Placement string
TestMode string
type UserParams struct {
Formats []string `json:"formats,omitempty"`
Placement string `json:"placement,omitempty" default:"sticky"`
Sample string `json:"sample,omitempty"`
Settings map[string]any `json:"settings,omitempty"`
}

type MissenaAdapter struct {
EndpointTemplate *template.Template
}

var defaultCur = "USD"

// Builder builds a new instance of the Foo adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
endpoint, err := template.New("endpointTemplate").Parse(config.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to parse endpoint url template: %v", err)
}
bidder := &adapter{
endpoint: config.Endpoint,
EndpointTemplate: endpoint,
}
return bidder, nil
}

func (a *adapter) makeRequest(missenaParams MissenaInternalParams, reqInfo *adapters.ExtraRequestInfo, impID string, request *openrtb2.BidRequest) (*adapters.RequestData, error) {
url := a.endpoint + "?t=" + missenaParams.ApiKey
func getCurrency(currencies []string) (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is better to use MAP:

    currencySet := make(map[string]struct{}, len(currencies))
    for _, cur := range currencies {
        currencySet[cur] = struct{}{}
    }

    if _, found := currencySet[defaultCur]; found {
        return defaultCur, nil
    }
    if _, found := currencySet["EUR"]; found {
        return "EUR", nil
    }
    return "", fmt.Errorf("no currency supported %v", currencies)
}```
what Do You think? 

eurAvailable := false
for _, cur := range currencies {
if cur == defaultCur {
return defaultCur, nil
}
if cur == "EUR" {
eurAvailable = true
}
}
if eurAvailable {
return "EUR", nil
}
return "", fmt.Errorf("no currency supported %v", currencies)
}

func (a *adapter) getEndPoint(ext *openrtb_ext.ExtImpMissena) (string, error) {
endPointParams := macros.EndpointTemplateParams{
PublisherID: url.PathEscape(ext.APIKey),
}
return macros.ResolveMacros(a.EndpointTemplate, endPointParams)
}

func (a *adapter) makeRequest(imp openrtb2.Imp, request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo, params *openrtb_ext.ExtImpMissena, gdprApplies bool, consentString string) (*adapters.RequestData, error) {
url, err := a.getEndPoint(params)
if err != nil {
return nil, err
}
cur, err := getCurrency(request.Cur)
if err != nil {
cur = defaultCur
}

var floor float64
var floorCur string
if imp.BidFloor != 0 {
floor = imp.BidFloor
floorCur, err = getCurrency(request.Cur)
if err != nil {
floorCur = defaultCur
floor, err = requestInfo.ConvertCurrency(imp.BidFloor, imp.BidFloorCur, floorCur)
if err != nil {
return nil, err
}
}
}

var schain *openrtb2.SupplyChain
if request.Source != nil {
schain = request.Source.SChain
}

var eids []openrtb2.EID
if request.User != nil {
eids = request.User.EIDs
}

var coppa int8
if request.Regs != nil {
coppa = request.Regs.COPPA
}

var page, domain string
if request.Site != nil {
page = request.Site.Page
domain = request.Site.Domain
}

missenaRequest := MissenaAdRequest{
RequestId: request.ID,
Timeout: 2000,
Referer: request.Site.Page,
RefererCanonical: request.Site.Domain,
GDPRConsent: missenaParams.GDPRConsent,
GDPR: missenaParams.GDPR,
Placement: missenaParams.Placement,
TestMode: missenaParams.TestMode,
Adunit: imp.ID,
COPPA: coppa,
Currency: cur,
EIDs: eids,
Floor: floor,
FloorCurrency: floorCur,
GDPR: gdprApplies,
GDPRConsent: consentString,
IdempotencyKey: request.ID,
Referer: page,
RefererCanonical: domain,
RequestID: request.ID,
SChain: schain,
Timeout: request.TMax,
UserParams: UserParams{
Formats: params.Formats,
Placement: params.Placement,
Settings: params.Settings,
},
Version: version.Ver,
}

body, errm := json.Marshal(missenaRequest)
if errm != nil {
return nil, errm
body, err := json.Marshal(missenaRequest)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use jsonutil.Marshal as a drop in replacement for json.Marshal.

if err != nil {
return nil, err
}

headers := http.Header{}
Expand All @@ -100,21 +191,15 @@ func (a *adapter) makeRequest(missenaParams MissenaInternalParams, reqInfo *adap
Uri: url,
Headers: headers,
Body: body,
ImpIDs: []string{impID},
ImpIDs: []string{imp.ID},
}, nil
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {

var httpRequests []*adapters.RequestData
var errors []error
gdprApplies, consentString := readGDPR(request)

missenaInternalParams := MissenaInternalParams{
GDPR: gdprApplies,
GDPRConsent: consentString,
}

for _, imp := range request.Imp {
var bidderExt adapters.ExtImpBidder
if err := jsonutil.Unmarshal(imp.Ext, &bidderExt); err != nil {
Expand All @@ -124,26 +209,20 @@ func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapte
continue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIP:
Message: fmt.Sprintf("Error parsing bidderExt object: %v, input: %s", err, string(imp.Ext)),

}

var missenaExt openrtb_ext.ExtImpMissena
var missenaExt *openrtb_ext.ExtImpMissena
if err := jsonutil.Unmarshal(bidderExt.Bidder, &missenaExt); err != nil {
errors = append(errors, &errortypes.BadInput{
Message: "Error parsing missenaExt parameters",
})
continue
}

missenaInternalParams.ApiKey = missenaExt.ApiKey
missenaInternalParams.Placement = missenaExt.Placement
missenaInternalParams.TestMode = missenaExt.TestMode

newHttpRequest, err := a.makeRequest(missenaInternalParams, requestInfo, imp.ID, request)
newHttpRequest, err := a.makeRequest(imp, request, requestInfo, missenaExt, gdprApplies, consentString)
if err != nil {
errors = append(errors, err)
continue
}

httpRequests = append(httpRequests, newHttpRequest)

break
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break in the loop causes only the first imp to be processed. If this is intentional, it's worth making it clearer in the code, e.g., with a comment or better naming.

}

Expand Down Expand Up @@ -189,28 +268,28 @@ func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.R
return nil, []error{err}
}

var missenaResponse MissenaBidServerResponse
var missenaResponse BidServerResponse
if err := jsonutil.Unmarshal(responseData.Body, &missenaResponse); err != nil {
return nil, []error{err}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)
bidResponse.Currency = missenaResponse.Currency
bidRes := adapters.NewBidderResponseWithBidsCapacity(1)
bidRes.Currency = missenaResponse.Currency

responseBid := &openrtb2.Bid{
ID: request.ID,
Price: float64(missenaResponse.Cpm),
ImpID: request.Imp[0].ID,
AdM: missenaResponse.Ad,
CrID: missenaResponse.RequestId,
CrID: missenaResponse.RequestID,
}

b := &adapters.TypedBid{
Bid: responseBid,
BidType: openrtb_ext.BidTypeBanner,
}

bidResponse.Bids = append(bidResponse.Bids, b)
bidRes.Bids = append(bidRes.Bids, b)

return bidResponse, nil
return bidRes, nil
}
2 changes: 1 addition & 1 deletion adapters/missena/missena_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.BidderMissena, config.Adapter{
Endpoint: "http://example.com/"},
Endpoint: "http://example.com/?t={{.PublisherID}}"},
config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

if buildErr != nil {
Expand Down
Loading
Loading