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: Loopme #4023

Merged
merged 6 commits into from
Feb 12, 2025
Merged
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
116 changes: 116 additions & 0 deletions adapters/loopme/loopme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package loopme

import (
"encoding/json"
"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"
)

type adapter struct {
endpoint string
}

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

reqDatas := make([]*adapters.RequestData, 0, len(request.Imp))

ambalashov marked this conversation as resolved.
Show resolved Hide resolved
for _, imp := range request.Imp {
requestCopy := *request
requestCopy.Imp = []openrtb2.Imp{imp}
reqJSON, err := json.Marshal(requestCopy)
if err != nil {
errs = append(errs, err)
continue
}

headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")
reqDatas = append(reqDatas, &adapters.RequestData{
Method: http.MethodPost,
Uri: a.endpoint,
Body: reqJSON,
Headers: headers,
ImpIDs: openrtb_ext.GetImpIDs(requestCopy.Imp),
})

}
return reqDatas, errs
}

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

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

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

if len(bidResp.SeatBid) == 0 || len(bidResp.SeatBid[0].Bid) == 0 {
return nil, nil
}

errs := make([]error, 0, len(bidResp.SeatBid[0].Bid))
resp := adapters.NewBidderResponseWithBidsCapacity(len(bidResp.SeatBid[0].Bid))

for _, sb := range bidResp.SeatBid {
for i := range sb.Bid {
bid := &sb.Bid[i]
bidType, err := getBidType(bid)
if err != nil {
errs = append(errs, err)
continue
}
resp.Bids = append(resp.Bids, &adapters.TypedBid{
Bid: bid,
BidType: bidType,
})
}
}

if len(resp.Bids) == 0 {
return nil, errs
}
if len(bidResp.Cur) != 0 {
resp.Currency = bidResp.Cur
}
return resp, errs
}

func getBidType(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
case openrtb2.MarkupAudio:
return openrtb_ext.BidTypeAudio, nil
default:
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Unsupported MType %d", bid.MType),
}
}
}

// Builder builds a new instance of the Loopme adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, cfg config.Adapter, serverCfg config.Server) (adapters.Bidder, error) {
bidder := &adapter{
endpoint: cfg.Endpoint,
}
return bidder, nil
}
29 changes: 29 additions & 0 deletions adapters/loopme/loopme_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package loopme

import (
"testing"

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

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(
openrtb_ext.BidderLoopme,
config.Adapter{
Endpoint: "http://loopme.example.com",
},
config.Server{
ExternalUrl: "http://hosturl.com",
GvlID: 109,
DataCenter: "2",
},
)

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

adapterstest.RunJSONBidderTest(t, "loopmetest", bidder)
}
152 changes: 152 additions & 0 deletions adapters/loopme/loopmetest/exemplary/app-formats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"mockBidRequest": {
"id": "test-request-id",
"app": {
"bundle": "com.prebid"
},
"device": {
"ifa": "ec943cb9-61ec-460f-a925-6489c3fcc4e3"
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 728,
"h": 90
}
]
},
"video": {
"mimes": [
"video/mp4"
],
"protocols": [
2,
3,
5,
6
],
"w": 1024,
"h": 576
},
"native": {
"request": "{json string 1}",
"ver": "1.2"
},
"ext": {
"bidder": {
"publisherId": "10000000"
}
}
}
]
},
"httpCalls": [
{
"expectedRequest": {
"uri": "http://loopme.example.com",
"body": {
"id": "test-request-id",
"app": {
"bundle": "com.prebid"
},
"device": {
"ifa": "ec943cb9-61ec-460f-a925-6489c3fcc4e3"
},
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 728,
"h": 90
}
]
},
"video": {
"mimes": [
"video/mp4"
],
"protocols": [
2,
3,
5,
6
],
"w": 1024,
"h": 576
},
"native": {
"request": "{json string 1}",
"ver": "1.2"
},
"ext": {
"bidder": {
"publisherId": "10000000"
}
}
}
]
},
"impIDs": [
"test-imp-id"
]
},
"mockResponse": {
"status": 200,
"headers": {
"Accept": [
"application/json"
],
"Content-Type": [
"application/json;charset=utf-8"
]
},
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "loopme",
"bid": [
{
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 0.500000,
"adm": "some-test-ad",
"crid": "test-crid",
"w": 728,
"h": 90,
"mtype": 1
}
]
}
],
"cur": "USD"
}
}
}
],
"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 0.5,
"adm": "some-test-ad",
"crid": "test-crid",
"w": 728,
"h": 90,
"mtype": 1
},
"type": "banner"
}
]
}
]
}
Loading
Loading