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: AlgoriX #1861

Merged
merged 12 commits into from
Jun 9, 2021
163 changes: 163 additions & 0 deletions adapters/algorix/algorix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package algorix

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

"github.com/mxmCherry/openrtb/v15/openrtb2"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/macros"
"github.com/prebid/prebid-server/openrtb_ext"
Bugxyb marked this conversation as resolved.
Show resolved Hide resolved
)

type adapter struct {
EndpointTemplate template.Template
}

// Builder builds a new instance of the AlgoriX adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (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{
EndpointTemplate: *endpoint,
}
return bidder, nil
}

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

adapterRequest, err := a.makeRequest(request)
if err == nil {
adapterRequests = append(adapterRequests, adapterRequest)
} else {
errs = append(errs, err)
}
return adapterRequests, errs
}

func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, error) {
algorixExt, err := getImpAlgoriXExt(&request.Imp[0])

if err != nil {
return nil, &errortypes.BadInput{Message: "Invalid ExtImpAlgoriX value"}
}

endPoint, err := a.getEndPoint(algorixExt)
if err != nil {
return nil, err
}

preProcess(request)
reqBody, 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")
headers.Add("x-openrtb-version", "2.5")

return &adapters.RequestData{
Method: "POST",
Uri: endPoint,
Body: reqBody,
Headers: headers,
}, nil
}

// get ImpAlgoriXExt From First Imp. Only check and get first Imp.Ext.Bidder to ExtImpAlgorix
func getImpAlgoriXExt(imp *openrtb2.Imp) (*openrtb_ext.ExtImpAlgorix, error) {
var extImpAlgoriX openrtb_ext.ExtImpAlgorix
var extBidder adapters.ExtImpBidder
err := json.Unmarshal(imp.Ext, &extBidder)
if err != nil {
return nil, err
}
err = json.Unmarshal(extBidder.Bidder, &extImpAlgoriX)
if err != nil {
return nil, err
}
return &extImpAlgoriX, nil
}
Bugxyb marked this conversation as resolved.
Show resolved Hide resolved

func (a *adapter) getEndPoint(ext *openrtb_ext.ExtImpAlgorix) (string, error) {
endPointParams := macros.EndpointTemplateParams{SourceId: ext.Sid, AccountID: ext.Token}
Bugxyb marked this conversation as resolved.
Show resolved Hide resolved
return macros.ResolveMacros(a.EndpointTemplate, endPointParams)
}

func preProcess(request *openrtb2.BidRequest) {
for i := range request.Imp {
if request.Imp[i].Banner != nil {
banner := *request.Imp[i].Banner
if (banner.W == nil || banner.H == nil || *banner.W == 0 || *banner.H == 0) && len(banner.Format) > 0 {
firstFormat := banner.Format[0]
banner.W = &firstFormat.W
banner.H = &firstFormat.H
request.Imp[i].Banner = &banner
}
}
}
}

func (a *adapter) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
if response.StatusCode == http.StatusNoContent {
return nil, nil
}

if response.StatusCode == http.StatusBadRequest {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Unexpected status code: %d.", response.StatusCode),
}}
}

if response.StatusCode != http.StatusOK {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Unexpected status code: %d.", response.StatusCode),
}}
}

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

bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)

for _, seatBid := range bidResp.SeatBid {
for idx := range seatBid.Bid {
mediaType := getBidType(seatBid.Bid[idx].ImpID, internalRequest.Imp)
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &seatBid.Bid[idx],
BidType: mediaType,
})
}
}
return bidResponse, nil
}

func getBidType(impId string, imps []openrtb2.Imp) openrtb_ext.BidType {
mediaType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impId {
if imp.Banner != nil {
return openrtb_ext.BidTypeBanner
}
if imp.Native != nil {
return openrtb_ext.BidTypeNative
}
if imp.Video != nil {
return openrtb_ext.BidTypeVideo
}
}
}
return mediaType
Bugxyb marked this conversation as resolved.
Show resolved Hide resolved
}
27 changes: 27 additions & 0 deletions adapters/algorix/algorix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package algorix

import (
"github.com/stretchr/testify/assert"
"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.BidderAlgorix, config.Adapter{
Endpoint: "https://test.com?sid={{.SourceId}}&token={{.AccountID}}"})

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

adapterstest.RunJSONBidderTest(t, "algorixtest", bidder)
}

func TestEndpointTemplateMalformed(t *testing.T) {
_, buildErr := Builder(openrtb_ext.BidderAlgorix, config.Adapter{Endpoint: "{{Malformed}}"})

assert.Error(t, buildErr)
}
83 changes: 83 additions & 0 deletions adapters/algorix/algorixtest/exemplary/sample-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [{"w": 320, "h": 50}]
},
"ext": {
"bidder": {
"sid": "testSid",
"token": "testToken"
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://test.com?sid=testSid&token=testToken",
"body": {
"id": "test-request-id",
"imp": [
{
"id":"test-imp-id",
"banner": {
"format": [{"w": 320, "h": 50}],
"w": 320,
"h": 50
},
"ext": {
"bidder": {
"sid": "testSid",
"token": "testToken"
}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "ttx",
"bid": [{
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 0.5,
"adm": "some-ads",
"crid": "crid_testid"
}]
}
],
"cur": "USD"
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 0.5,
"adm": "some-ads",
"crid": "crid_testid"
},
"type": "banner"
}
]
}
]
}
82 changes: 82 additions & 0 deletions adapters/algorix/algorixtest/exemplary/sample-native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"native": {
"request": "{\"ver\":\"1.2\",\"context\":1,\"plcmttype\":4,\"plcmtcnt\":1,\"assets\":[{\"id\":2,\"required\":1,\"title\":{\"len\":90}},{\"id\":6,\"required\":1,\"img\":{\"type\":3,\"wmin\":128,\"hmin\":128,\"mimes\":[\"image/jpg\",\"image/jpeg\",\"image/png\"]}},{\"id\":7,\"required\":1,\"data\":{\"type\":2,\"len\":120}}]}",
"ver": "1.2"
},
"ext": {
"bidder": {
"sid": "testSid",
"token": "testToken"
}
}
}
]
},
"httpcalls": [
{
"expectedRequest": {
"uri": "https://test.com?sid=testSid&token=testToken",
"body": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"native": {
"request": "{\"ver\":\"1.2\",\"context\":1,\"plcmttype\":4,\"plcmtcnt\":1,\"assets\":[{\"id\":2,\"required\":1,\"title\":{\"len\":90}},{\"id\":6,\"required\":1,\"img\":{\"type\":3,\"wmin\":128,\"hmin\":128,\"mimes\":[\"image/jpg\",\"image/jpeg\",\"image/png\"]}},{\"id\":7,\"required\":1,\"data\":{\"type\":2,\"len\":120}}]}",
"ver": "1.2"
},
"ext": {
"bidder": {
"sid": "testSid",
"token": "testToken"
}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"bid": [
{
"id": "8400d766-58b3-47d4-80d7-6658b337d403",
"impid": "test-imp-id",
"price": 1.2,
"adm": "some ads",
"crid": "crid_testid"
}
]
}
]
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "8400d766-58b3-47d4-80d7-6658b337d403",
"impid": "test-imp-id",
"price": 1.2,
"adm": "some ads",
"crid": "crid_testid"

},
"type": "native"
}
]
}
]
}
Loading