Skip to content

Commit

Permalink
New Adapter: Driftpixel (#3684)
Browse files Browse the repository at this point in the history
  • Loading branch information
driftpixelai authored Jun 17, 2024
1 parent fb15da3 commit 4dc4005
Show file tree
Hide file tree
Showing 24 changed files with 2,857 additions and 0 deletions.
159 changes: 159 additions & 0 deletions adapters/driftpixel/driftpixel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package driftpixel

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

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/errortypes"
"github.com/prebid/prebid-server/v2/macros"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)

type bidType struct {
Type string `json:"type"`
}

type bidExt struct {
Prebid bidType `json:"prebid"`
}

type adapter struct {
endpoint *template.Template
}

func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {
tmpl, 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: tmpl,
}

return bidder, nil
}

func (a *adapter) buildEndpointFromRequest(imp *openrtb2.Imp) (string, error) {
var impExt adapters.ExtImpBidder
if err := json.Unmarshal(imp.Ext, &impExt); err != nil {
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to deserialize bidder impression extension: %v", err),
}
}

var driftpixelExt openrtb_ext.ExtDriftPixel
if err := json.Unmarshal(impExt.Bidder, &driftpixelExt); err != nil {
return "", &errortypes.BadInput{
Message: fmt.Sprintf("Failed to deserialize DriftPixel extension: %v", err),
}
}

endpointParams := macros.EndpointTemplateParams{
Host: driftpixelExt.Env,
SourceId: driftpixelExt.Pid,
}

return macros.ResolveMacros(a.endpoint, endpointParams)
}

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

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")

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

endpoint, err := a.buildEndpointFromRequest(&imp)
if err != nil {
errs = append(errs, err)
continue
}

requestJSON, err := json.Marshal(requestCopy)
if err != nil {
errs = append(errs, err)
continue
}

request := &adapters.RequestData{
Method: http.MethodPost,
Body: requestJSON,
Uri: endpoint,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(requestCopy.Imp),
}

requests = append(requests, request)
}

return requests, errs
}

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

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

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

if len(bidResp.SeatBid) == 0 {
return nil, []error{&errortypes.BadServerResponse{
Message: "Array SeatBid cannot be empty",
}}
}

return prepareBidResponse(bidResp.SeatBid)
}

func prepareBidResponse(seats []openrtb2.SeatBid) (*adapters.BidderResponse, []error) {
var errs []error
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(seats))

for _, seatBid := range seats {
for idx, bid := range seatBid.Bid {
bidType, err := getMediaTypeForBid(bid)
if err != nil {
errs = append(errs, err)
continue
}

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

return bidResponse, errs
}

func getMediaTypeForBid(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.MarkupNative:
return openrtb_ext.BidTypeNative, nil
default:
return "", fmt.Errorf("failed to parse bid mtype (%d) for impression id \"%s\"", bid.MType, bid.ImpID)
}
}
34 changes: 34 additions & 0 deletions adapters/driftpixel/driftpixel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package driftpixel

import (
"testing"

"github.com/prebid/prebid-server/v2/adapters/adapterstest"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/stretchr/testify/assert"
)

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(
openrtb_ext.BidderDriftPixel,
config.Adapter{
Endpoint: "http://rtb.driftpixel.live/?pid={{.SourceId}}&host={{.Host}}&pbs=1",
},
config.Server{
ExternalUrl: "http://hosturl.com",
GvlID: 1,
DataCenter: "2",
},
)

assert.NoError(t, buildErr)
adapterstest.RunJSONBidderTest(t, "driftpixeltest", bidder)
}

func TestEndpointTemplateMalformed(t *testing.T) {
_, buildErr := Builder(openrtb_ext.BidderDriftPixel, config.Adapter{
Endpoint: "{{Malformed}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

assert.Error(t, buildErr)
}
Loading

0 comments on commit 4dc4005

Please sign in to comment.