Skip to content

Commit

Permalink
New adapter: PGAMSSP (#3035)
Browse files Browse the repository at this point in the history
  • Loading branch information
PGAMSSP authored Sep 15, 2023
1 parent 06a847e commit 23342ee
Show file tree
Hide file tree
Showing 17 changed files with 1,197 additions and 0 deletions.
47 changes: 47 additions & 0 deletions adapters/pgamssp/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package pgamssp

import (
"encoding/json"
"testing"

"github.com/prebid/prebid-server/openrtb_ext"
)

func TestValidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json schema. %v", err)
}

for _, p := range validParams {
if err := validator.Validate(openrtb_ext.BidderPGAMSsp, json.RawMessage(p)); err != nil {
t.Errorf("Schema rejected valid params: %s", p)
}
}
}

func TestInvalidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json schema. %v", err)
}

for _, p := range invalidParams {
if err := validator.Validate(openrtb_ext.BidderPGAMSsp, json.RawMessage(p)); err == nil {
t.Errorf("Schema allowed invalid params: %s", p)
}
}
}

var validParams = []string{
`{"placementId": "test"}`,
`{"placementId": "1"}`,
`{"endpointId": "test"}`,
`{"endpointId": "1"}`,
}

var invalidParams = []string{
`{"placementId": 42}`,
`{"endpointId": 42}`,
`{"placementId": "1", "endpointId": "1"}`,
}
145 changes: 145 additions & 0 deletions adapters/pgamssp/pgamssp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package pgamssp

import (
"encoding/json"
"fmt"
"net/http"

"github.com/prebid/openrtb/v19/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"
)

type adapter struct {
endpoint string
}

type reqBodyExt struct {
PGAMSspBidderExt reqBodyExtBidder `json:"bidder"`
}

type reqBodyExtBidder struct {
Type string `json:"type"`
PlacementID string `json:"placementId,omitempty"`
EndpointID string `json:"endpointId,omitempty"`
}

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

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
var err error
var adapterRequests []*adapters.RequestData

reqCopy := *request
for _, imp := range request.Imp {
reqCopy.Imp = []openrtb2.Imp{imp}

var bidderExt adapters.ExtImpBidder
var pgamExt openrtb_ext.ImpExtPgamSsp

if err = json.Unmarshal(reqCopy.Imp[0].Ext, &bidderExt); err != nil {
return nil, []error{err}
}
if err = json.Unmarshal(bidderExt.Bidder, &pgamExt); err != nil {
return nil, []error{err}
}

temp := reqBodyExt{PGAMSspBidderExt: reqBodyExtBidder{}}

if pgamExt.PlacementID != "" {
temp.PGAMSspBidderExt.PlacementID = pgamExt.PlacementID
temp.PGAMSspBidderExt.Type = "publisher"
} else if pgamExt.EndpointID != "" {
temp.PGAMSspBidderExt.EndpointID = pgamExt.EndpointID
temp.PGAMSspBidderExt.Type = "network"
}

finalyImpExt, err := json.Marshal(temp)
if err != nil {
return nil, []error{err}
}

reqCopy.Imp[0].Ext = finalyImpExt

adapterReq, err := a.makeRequest(&reqCopy)
if err != nil {
return nil, []error{err}
}

if adapterReq != nil {
adapterRequests = append(adapterRequests, adapterReq)
}
}
return adapterRequests, nil
}

func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) {
reqJSON, err := json.Marshal(request)
if err != nil {
return nil, err
}

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
return &adapters.RequestData{
Method: "POST",
Uri: a.endpoint,
Body: reqJSON,
Headers: headers,
}, err
}

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

if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil {
return nil, []error{err}
}

var response openrtb2.BidResponse
if err := json.Unmarshal(responseData.Body, &response); err != nil {
return nil, []error{err}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp))
bidResponse.Currency = response.Cur
for _, seatBid := range response.SeatBid {
for i := range seatBid.Bid {
bidType, err := getBidMediaType(&seatBid.Bid[i])
if err != nil {
return nil, []error{err}
}

b := &adapters.TypedBid{
Bid: &seatBid.Bid[i],
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, b)
}
}
return bidResponse, nil
}

func getBidMediaType(bid *openrtb2.Bid) (openrtb_ext.BidType, error) {
switch bid.MType {
case openrtb2.MarkupBanner:
return openrtb_ext.BidTypeBanner, nil
case openrtb2.MarkupVideo:
return openrtb_ext.BidTypeVideo, nil
case openrtb2.MarkupAudio:
return openrtb_ext.BidTypeAudio, nil
case openrtb2.MarkupNative:
return openrtb_ext.BidTypeNative, nil
default:
return "", fmt.Errorf("Unable to fetch mediaType in multi-format: %s", bid.ImpID)
}
}
20 changes: 20 additions & 0 deletions adapters/pgamssp/pgamssp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pgamssp

import (
"testing"

"github.com/prebid/prebid-server/adapters/adapterstest"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"
)

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderPGAMSsp, config.Adapter{
Endpoint: "http://test.com/pserver"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
}

adapterstest.RunJSONBidderTest(t, "pgamssptest", bidder)
}
125 changes: 125 additions & 0 deletions adapters/pgamssp/pgamssptest/exemplary/endpointId.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"mockBidRequest": {
"id": "test-request-id",
"device": {
"ip": "123.123.123.123",
"ua": "iPad"
},
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"imp": [
{
"id": "test-imp-id",
"tagid": "test",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"endpointId": "test"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://test.com/pserver",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"tagid": "test",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"bidder": {
"endpointId": "test",
"type": "network"
}
}
}
],
"app": {
"id": "1",
"bundle": "com.wls.testwlsapplication"
},
"device": {
"ip": "123.123.123.123",
"ua": "iPad"
}
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"http://test.com/pserver&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"mtype": 1
}
],
"seat": "pgamssp"
}
],
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"bids": [
{
"bid": {
"id": "test_bid_id",
"impid": "test-imp-id",
"price": 0.27543,
"adm": "<iframe id=\"adm-banner-16\" width=\"300\" height=\"250\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"{overflow:hidden}\" src=\"http://test.com/pserver&k=882b2510ed6d6c94fa69c99aa522a708\"></iframe>",
"cid": "test_cid",
"crid": "test_crid",
"dealid": "test_dealid",
"w": 300,
"h": 250,
"mtype": 1
},
"type": "banner"
}
]
}
]
}
Loading

0 comments on commit 23342ee

Please sign in to comment.