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

Updating pulsepoint adapter #1663

Merged
merged 9 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
58 changes: 58 additions & 0 deletions adapters/pulsepoint/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package pulsepoint

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-schemas. %v", err)
}

for _, validParam := range validParams {
if err := validator.Validate(openrtb_ext.BidderPulsepoint, json.RawMessage(validParam)); err != nil {
t.Errorf("Schema rejected pulsepoint params: %s \n Error: %s", validParam, err)
}
}
}

// TestInvalidParams makes sure that the pubmatic schema rejects all the imp.ext fields we don't support.
func TestInvalidParams(t *testing.T) {
validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
if err != nil {
t.Fatalf("Failed to fetch the json-schemas. %v", err)
}

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

var validParams = []string{
`{"cp":1000, "ct": 2000}`,
`{"cp":1001, "ct": 2001}`,
}
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved

var invalidParams = []string{
``,
`null`,
`true`,
`5`,
`4.2`,
`[]`,
`{}`,
`{"cp":"1000"}`,
`{"ct":"1000"}`,
`{"cp":1000}`,
`{"ct":1000}`,
`{"cp":1000, "ct":"1000"}`,
`{"cp":1000, "ct": "abcd"}`,
`{"cp":"abcd", "ct": 1000}`,
`{"cp":"1000.2", "ct": "1000.1"}`,
}
179 changes: 163 additions & 16 deletions adapters/pulsepoint/pulsepoint.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,196 @@
package pulsepoint

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"

"bytes"
"context"
"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/pbs"
"golang.org/x/net/context/ctxhttp"
"io/ioutil"
"strings"
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved
)

type PulsePointAdapter struct {
http *adapters.HTTPAdapter
URI string
}

// Builds an instance of PulsePointAdapter
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &PulsePointAdapter{
URI: config.Endpoint,
}
return bidder, nil
}
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved

func (a *PulsePointAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
errs := make([]error, 0, len(request.Imp))

var err error
pubID := ""

// No impressions given.
if len(request.Imp) == 0 {
return nil, errs
}
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved

for i := 0; i < len(request.Imp); i++ {
imp := request.Imp[i]
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved
var bidderExt adapters.ExtImpBidder
if err = json.Unmarshal(imp.Ext, &bidderExt); err != nil {
errs = append(errs, &errortypes.BadInput{
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved
Message: err.Error(),
})
continue
}
var pulsepointExt openrtb_ext.ExtImpPulsePoint
if err = json.Unmarshal(bidderExt.Bidder, &pulsepointExt); err != nil {
errs = append(errs, &errortypes.BadInput{
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved
Message: err.Error(),
})
continue
}
// parse pubid and keep it for reference
if pubID == "" && pulsepointExt.PubID > 0 {
pubID = strconv.Itoa(pulsepointExt.PubID)
}
// tag id to be sent
request.Imp[i].TagID = strconv.Itoa(pulsepointExt.TagID)
}

// add the publisher id from ext to the site.pub.id or app.pub.id
if request.Site != nil {
site := *request.Site
if site.Publisher != nil {
publisher := *site.Publisher
publisher.ID = pubID
site.Publisher = &publisher
} else {
site.Publisher = &openrtb.Publisher{ID: pubID}
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved
}
request.Site = &site
} else if request.App != nil {
app := *request.App
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved
if app.Publisher != nil {
publisher := *app.Publisher
publisher.ID = pubID
app.Publisher = &publisher
} else {
app.Publisher = &openrtb.Publisher{ID: pubID}
}
request.App = &app
}

uri := a.URI
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved

reqJSON, err := json.Marshal(request)
if err != nil {
errs = append(errs, err)
return nil, errs
}

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
return []*adapters.RequestData{{
Method: "POST",
Uri: uri,
Body: reqJSON,
Headers: headers,
}}, errs
}

func (a *PulsePointAdapter) MakeBids(internalRequest *openrtb.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
// passback
if response.StatusCode == http.StatusNoContent {
return nil, nil
}
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved
// error
if response.StatusCode != http.StatusOK {
return nil, []error{fmt.Errorf("Unexpected status code: %d.", response.StatusCode)}
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved
}
// parse response
var bidResp openrtb.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{err}
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(5)
// map imps by id
impsByID := make(map[string]openrtb.Imp)
for i := 0; i < len(internalRequest.Imp); i++ {
impsByID[internalRequest.Imp[i].ID] = internalRequest.Imp[i]
}

var errs []error
for _, sb := range bidResp.SeatBid {
for i := 0; i < len(sb.Bid); i++ {
bid := sb.Bid[i]
imp := impsByID[bid.ImpID]
bidType := getBidType(imp)
if &imp != nil && bidType != "" {
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: bidType,
})
}
}
}
return bidResponse, errs
}

func getBidType(imp openrtb.Imp) openrtb_ext.BidType {
// derive the bidtype purely from the impression itself
anand-venkatraman marked this conversation as resolved.
Show resolved Hide resolved
if imp.Banner != nil {
return openrtb_ext.BidTypeBanner
} else if imp.Video != nil {
return openrtb_ext.BidTypeVideo
} else if imp.Audio != nil {
return openrtb_ext.BidTypeAudio
} else if imp.Native != nil {
return openrtb_ext.BidTypeNative
}
return ""
}

/////////////////////////////////
// Legacy implementation: Start
/////////////////////////////////

func NewPulsePointLegacyAdapter(config *adapters.HTTPAdapterConfig, uri string) *PulsePointAdapter {
a := adapters.NewHTTPAdapter(config)

return &PulsePointAdapter{
http: a,
URI: uri,
}
}

// used for cookies and such
func (a *PulsePointAdapter) Name() string {
return "pulsepoint"
}

func (a *PulsePointAdapter) SkipNoCookies() bool {
return false
}

// parameters for pulsepoint adapter.
type PulsepointParams struct {
PublisherId int `json:"cp"`
TagId int `json:"ct"`
AdSize string `json:"cf"`
}

func (a *PulsePointAdapter) SkipNoCookies() bool {
return false
}

func (a *PulsePointAdapter) Call(ctx context.Context, req *pbs.PBSRequest, bidder *pbs.PBSBidder) (pbs.PBSBidSlice, error) {
mediaTypes := []pbs.MediaType{pbs.MEDIA_TYPE_BANNER}
ppReq, err := adapters.MakeOpenRTBGeneric(req, bidder, a.Name(), mediaTypes)
Expand Down Expand Up @@ -195,11 +347,6 @@ func (a *PulsePointAdapter) Call(ctx context.Context, req *pbs.PBSRequest, bidde
return bids, nil
}

func NewPulsePointLegacyAdapter(config *adapters.HTTPAdapterConfig, uri string) *PulsePointAdapter {
a := adapters.NewHTTPAdapter(config)

return &PulsePointAdapter{
http: a,
URI: uri,
}
}
/////////////////////////////////
// Legacy implementation: End
/////////////////////////////////
Loading