-
Notifications
You must be signed in to change notification settings - Fork 752
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
base: master
Are you sure you want to change the base?
Missena: Update params #4057
Changes from all commits
9144aec
091ffa9
9398c8a
75e77ba
7c92744
dd7c2c1
8f5c3f5
5b606eb
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 |
---|---|---|
|
@@ -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) { | ||
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) | ||
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. Please use |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
headers := http.Header{} | ||
|
@@ -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 { | ||
|
@@ -124,26 +209,20 @@ func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapte | |
continue | ||
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. NIP: |
||
} | ||
|
||
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 | ||
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. 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. |
||
} | ||
|
||
|
@@ -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 | ||
} |
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 think is better to use MAP: