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

New Adapter: Mediasquare #3994

Open
wants to merge 16 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
131 changes: 131 additions & 0 deletions adapters/mediasquare/mediasquare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package mediasquare

import (
"fmt"
"net/http"

"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/openrtb_ext"
"github.com/prebid/prebid-server/v3/util/jsonutil"
)

type adapter struct {
endpoint string
}

func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
return &adapter{
endpoint: config.Endpoint,
}, nil
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var (
requestData []*adapters.RequestData
errs []error
)
if request == nil || request.Imp == nil {
errs = append(errs, errorWriter("<MakeRequests> request", nil, true))
return nil, errs
}

msqParams := initMsqParams(request)
msqParams.Test = (request.Test == int8(1))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I haven't seen this behavior before. Is this added because the msqParams.Test field is required by the MediaSquare backend to recognize the request as a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is.

for _, imp := range request.Imp {
var (
bidderExt adapters.ExtImpBidder
msqExt openrtb_ext.ImpExtMediasquare
currentCode = msqParametersCodes{
AdUnit: imp.TagID,
AuctionId: request.ID,
BidId: imp.ID,
}
)

if err := jsonutil.Unmarshal(imp.Ext, &bidderExt); err != nil {
errs = append(errs, errorWriter("<MakeRequests> imp[ext]", err, len(imp.Ext) == 0))
continue
}
if err := jsonutil.Unmarshal(bidderExt.Bidder, &msqExt); err != nil {
errs = append(errs, errorWriter("<MakeRequests> imp-bidder[ext]", err, len(bidderExt.Bidder) == 0))
continue
}
currentCode.Owner = msqExt.Owner
currentCode.Code = msqExt.Code

if currentCode.setContent(imp) {
msqParams.Codes = append(msqParams.Codes, currentCode)
}
}

req, err := a.makeRequest(request, &msqParams)
if err != nil {
errs = append(errs, err)
} else if req != nil {
requestData = append(requestData, req)
}
return requestData, errs
}

func (a *adapter) makeRequest(request *openrtb2.BidRequest, msqParams *msqParameters) (requestData *adapters.RequestData, err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why You have two makeRequest func()?
parsers.go structs.go utils.go these files should not exists.
please check one more time instruction how to build a adapter in Go.
https://docs.prebid.org/prebid-server/developers/add-new-bidder-go.html

Copy link

@matthieularere-msq matthieularere-msq Jan 21, 2025

Choose a reason for hiding this comment

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

I guess you mean why we do have a makeRequest and a MakeRequests as we can't have twice a same function otherwise it would not compile ? If so, for the very same reasons other bidders do it (mgid, openx, taboola, improvedigital and many more), to split the MakeRequests func and make it easier to read. Is that an issue ? Is there any restriction in the amount of functions and their spelling ?

Can you elaborate on the the reason(s) why the files parsers.go, structs.go and utils.go should not exists ? If you referer to the file check list from https://docs.prebid.org/prebid-server/developers/add-new-bidder-go.html#file-checklist then my understanding is that this section is about the files that are mandatory not about the only files that we are allowed to create. For instance, appnexus adapter also has files iab_categories.go and models.go in its adapter. The reason why we have these files is to split business logic from the main mediasquare adapter file. If what you want to mean is that we should just merge the content of these files inside mediasquare.go please confirm, though it seems to us that it make this file less readable.

Copy link
Contributor

Choose a reason for hiding this comment

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

I will confirm with the team, but I think it's totally ok to have extra files (within the adapter directory) to make code more readable and structured. This adapter accepts and returns it's own format, not OpenRTB format. Having all the structures and converters in the mediasquare.go will make it difficult to understand and navigate.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed. For simple adapters a single file should suffice. However, in this case, the adapter is rather complex so I do appreciate and prefer the code organization across multiple files.

var requestJsonBytes []byte
if msqParams == nil {
err = errorWriter("<makeRequest> msqParams", nil, true)
return
}
if requestJsonBytes, err = jsonutil.Marshal(msqParams); err == nil {
var headers http.Header = headerList
requestData = &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: requestJsonBytes,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(request.Imp),
}
} else {
err = errorWriter("<makeRequest> jsonutil.Marshal", err, false)
}

return
}

func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var (
bidderResponse *adapters.BidderResponse
errs []error
)
if response.StatusCode != http.StatusOK {
switch response.StatusCode {
case http.StatusBadRequest:
errs = []error{&errortypes.BadInput{Message: fmt.Sprintf("<MakeBids> Unexpected status code: %d.", response.StatusCode)}}
default:
errs = []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("<MakeBids> Unexpected status code: %d. Run with request.debug = 1 for more info.", response.StatusCode),
}}
}
return bidderResponse, errs
}
Copy link
Contributor

@VeronikaSolovei9 VeronikaSolovei9 Jan 22, 2025

Choose a reason for hiding this comment

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

Would you consider to clean up error handling using CheckResponseStatusCodeForErrors and IsResponseStatusCodeNoContent functions? Please refer to "app nexus.go"->MakeBids.

I also noticed an interesting thing. When I run the request to mediasquare and bids are not returned - you still return http 200 with error message. This is not typical behavior. If bids are not returned we usually get http 204. If this is something you cannot change, please consider to add a check for empty responses after jsonutil.Unmarshal(response.Body, &msqResp) so we can exit earlier.


var msqResp msqResponse
if err := jsonutil.Unmarshal(response.Body, &msqResp); err != nil {
errs = []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("<MakeBids> Unexpected status code: %d. Bad server response: %s.",
http.StatusNotAcceptable, err.Error())},
}
return bidderResponse, errs
}
if len(msqResp.Responses) == 0 {
errs = []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("<MakeBids> Unexpected status code: %d. No responses found into body content.",
http.StatusNoContent)},
}
return bidderResponse, errs
}
bidderResponse = adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
msqResp.getContent(bidderResponse)

return bidderResponse, errs
}
44 changes: 44 additions & 0 deletions adapters/mediasquare/mediasquare_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package mediasquare

import (
"testing"

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v3/adapters"
"github.com/prebid/prebid-server/v3/adapters/adapterstest"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/openrtb_ext"
"github.com/stretchr/testify/assert"
)

func TestBidderMediasquare(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderMediasquare, config.Adapter{
Endpoint: "https://pbs-front.mediasquare.fr/msq_prebid"},
config.Server{ExternalUrl: "https://pbs-front.mediasquare.fr/msq_prebid", GvlID: 1, DataCenter: "2"})
if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}
adapterstest.RunJSONBidderTest(t, "mediasquaretest", bidder)
}

func TestMakeRequests(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderMediasquare, config.Adapter{
Endpoint: "https://pbs-front.mediasquare.fr/msq_prebid"},
config.Server{ExternalUrl: "https://pbs-front.mediasquare.fr/msq_prebid", GvlID: 1, DataCenter: "2"})
if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}

// MakeRequests : case request is empty.
resp, errs := bidder.MakeRequests(nil, nil)
expectingErrors := []error{errorWriter("<MakeRequests> request", nil, true)}
assert.Equal(t, []*adapters.RequestData(nil), resp, "resp, was supposed to be empty result.")
assert.Equal(t, expectingErrors, errs, "errs, was supposed to be :", expectingErrors)

// MakeRequests : case request.Imp is empty.
bidResquest := openrtb2.BidRequest{ID: "id-test", Imp: nil}
resp, errs = bidder.MakeRequests(&bidResquest, nil)
expectingErrors = []error{errorWriter("<MakeRequests> request", nil, true)}
assert.Equal(t, []*adapters.RequestData(nil), resp, "resp, was supposed to be empty result.")
assert.Equal(t, expectingErrors, errs, "errs, was supposed to be :", expectingErrors)
}
Loading
Loading