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

Yeahmobi adapter #1279

Merged
merged 14 commits into from
May 14, 2020
47 changes: 47 additions & 0 deletions adapters/yeahmobi/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package yeahmobi

import (
"encoding/json"
"github.com/prebid/prebid-server/openrtb_ext"
"testing"
)

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.BidderYeahmobi, json.RawMessage(validParam)); err != nil {
t.Errorf("Schema rejected yeahmobi params: %s", validParam)
}
}
}

// TestInvalidParams makes sure that the yeahmobi 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.BidderYeahmobi, json.RawMessage(invalidParam)); err == nil {
t.Errorf("Schema allowed unexpected params: %s", invalidParam)
}
}
}

var validParams = []string{
`{"pubId": "11233", "zoneId": "sin"}`,
`{"pubId": "11244", "zoneId": "iad"}`,
}

var invalidParams = []string{
`{"pubId": "11233"}`,
`{"zoneId": "aaa"}`,
`{"pubId": 123, "zoneId": "sin"}`,
`{"pubId": "", "zoneId": "iad"}`,
`{"pubId": "11233", "zoneId": ""}`,
}
179 changes: 179 additions & 0 deletions adapters/yeahmobi/yeahmobi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package yeahmobi

import (
"encoding/json"
"errors"
"fmt"
"github.com/golang/glog"
"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/macros"
"github.com/prebid/prebid-server/openrtb_ext"
"net/http"
"text/template"
)

type YeahmobiAdapter struct {
EndpointTemplate template.Template
}

func NewYeahmobiBidder(endpointTemplate string) adapters.Bidder {
tpl, err := template.New("endpointTemplate").Parse(endpointTemplate)
if err != nil {
glog.Fatal("Unknow url template")
zhaojp marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
return &YeahmobiAdapter{EndpointTemplate: *tpl}
}

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

adapterRequest, errors := adapter.makeRequest(request)
if adapterRequest != nil {
zhaojp marked this conversation as resolved.
Show resolved Hide resolved
adapterRequests = append(adapterRequests, adapterRequest)
}

return adapterRequests, errors
}

func (adapter *YeahmobiAdapter) makeRequest(request *openrtb.BidRequest) (*adapters.RequestData, []error) {
var errs []error

yeahmobiExt, errs := getYeahmobiExt(request)

if yeahmobiExt == nil {
glog.Fatal("Invalid ExtImpYeahmobi value")
return nil, errs
}

endPoint, err := adapter.getEndpoint(yeahmobiExt)

transform(request)

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

if err != nil {
errs = append(errs, err)
}

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

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

func transform(request *openrtb.BidRequest) {
for i, imp := range request.Imp {
if imp.Native != nil {
var nativeRequest map[string]interface{}
err := json.Unmarshal([]byte(request.Imp[i].Native.Request), &nativeRequest)
if err == nil {
if nativeRequest["native"] != nil {
zhaojp marked this conversation as resolved.
Show resolved Hide resolved
continue
}
request.Imp[i].Native.Request = "{\"native\":" + request.Imp[i].Native.Request + "}"
zhaojp marked this conversation as resolved.
Show resolved Hide resolved
}
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
zhaojp marked this conversation as resolved.
Show resolved Hide resolved

func getYeahmobiExt(request *openrtb.BidRequest) (*openrtb_ext.ExtImpYeahmobi, []error) {
var extImpYeahmobi openrtb_ext.ExtImpYeahmobi
var errs []error

for _, imp := range request.Imp {
var extBidder adapters.ExtImpBidder
err := json.Unmarshal(imp.Ext, &extBidder)
if err != nil {
errs = append(errs, err)
continue
}
err = json.Unmarshal(extBidder.Bidder, &extImpYeahmobi)
if err != nil {
errs = append(errs, err)
continue
}
zhaojp marked this conversation as resolved.
Show resolved Hide resolved

}

return &extImpYeahmobi, errs

}

func (adapter *YeahmobiAdapter) getEndpoint(ext *openrtb_ext.ExtImpYeahmobi) (string, error) {
if ext.ZoneId == "" {
return "", errors.New("param of zoneId not config")
}
zhaojp marked this conversation as resolved.
Show resolved Hide resolved

return macros.ResolveMacros(adapter.EndpointTemplate, macros.EndpointTemplateParams{Host: "gw-" + ext.ZoneId + "-bid.yeahtargeter.com"})
zhaojp marked this conversation as resolved.
Show resolved Hide resolved
}

// MakeBids make the bids for the bid response.
func (a *YeahmobiAdapter) MakeBids(internalRequest *openrtb.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 openrtb.BidResponse

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

bidResponse := adapters.NewBidderResponseWithBidsCapacity(1)

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

}

func getBidType(impId string, imps []openrtb.Imp) openrtb_ext.BidType {
bidType := openrtb_ext.BidTypeBanner
for _, imp := range imps {
if imp.ID == impId {
if imp.Banner != nil {
break
}
if imp.Video != nil {
bidType = openrtb_ext.BidTypeVideo
break
}
if imp.Native != nil {
bidType = openrtb_ext.BidTypeNative
break
}

}
}
return bidType
}
10 changes: 10 additions & 0 deletions adapters/yeahmobi/yeahmobi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package yeahmobi

import (
"github.com/prebid/prebid-server/adapters/adapterstest"
"testing"
)

func TestJsonSamples(t *testing.T) {
adapterstest.RunJSONBidderTest(t, "yeahmobitest", NewYeahmobiBidder("https://{{.Host}}/prebid/bid"))
}
81 changes: 81 additions & 0 deletions adapters/yeahmobi/yeahmobitest/exemplary/simple-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [{"w": 300, "h": 50}]
},
"ext": {
"bidder": {
"pubId": "fake-pub-id",
"zoneId": "sin"
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "https://gw-sin-bid.yeahtargeter.com/prebid/bid",
"body": {
"id": "test-request-id",
"imp": [
{
"id":"test-imp-id",
"banner": {
"format": [{"w": 300, "h": 50}]
},
"ext": {
"bidder": {
"pubId": "fake-pub-id",
"zoneId": "sin"
}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "ttx",
"bid": [{
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 1.2,
"adm": "some-ads",
"crid": "crid_testid"
}]
}
],
"cur": "USD"
}
}
}
],

"expectedBidResponses": [
{
"currency": "USD",
"bids": [
{
"bid": {
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 1.2,
"adm": "some-ads",
"crid": "crid_testid"
},
"type": "banner"
}
]
}
]
}
5 changes: 5 additions & 0 deletions adapters/yeahmobi/yeahmobitest/params/race/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"pubId": "222",
"zoneId": "sin"
}

1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ func SetupViper(v *viper.Viper, filename string) {
v.SetDefault("adapters.yieldmo.endpoint", "https://ads.yieldmo.com/exchange/prebid-server")
v.SetDefault("adapters.yieldone.endpoint", "https://y.one.impact-ad.jp/hbs_imp")
v.SetDefault("adapters.zeroclickfraud.endpoint", "http://{{.Host}}/openrtb2?sid={{.SourceId}}")
v.SetDefault("adapters.yeahmobi.endpoint", "https://{{.Host}}/prebid/bid")
zhaojp marked this conversation as resolved.
Show resolved Hide resolved

v.SetDefault("max_request_size", 1024*256)
v.SetDefault("analytics.file.filename", "")
Expand Down
2 changes: 2 additions & 0 deletions exchange/adapter_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exchange

import (
"fmt"
"github.com/prebid/prebid-server/adapters/yeahmobi"
zhaojp marked this conversation as resolved.
Show resolved Hide resolved
"net/http"
"strings"

Expand Down Expand Up @@ -142,6 +143,7 @@ func newAdapterMap(client *http.Client, cfg *config.Configuration, infos adapter
openrtb_ext.BidderYieldmo: yieldmo.NewYieldmoBidder(cfg.Adapters[string(openrtb_ext.BidderYieldmo)].Endpoint),
openrtb_ext.BidderYieldone: yieldone.NewYieldoneBidder(cfg.Adapters[string(openrtb_ext.BidderYieldone)].Endpoint),
openrtb_ext.BidderZeroClickFraud: zeroclickfraud.NewZeroClickFraudBidder(cfg.Adapters[string(openrtb_ext.BidderZeroClickFraud)].Endpoint),
openrtb_ext.BidderYeahmobi: yeahmobi.NewYeahmobiBidder(cfg.Adapters[string(openrtb_ext.BidderYeahmobi)].Endpoint),
}
zhaojp marked this conversation as resolved.
Show resolved Hide resolved

legacyBidders := map[openrtb_ext.BidderName]adapters.Adapter{
Expand Down
2 changes: 2 additions & 0 deletions openrtb_ext/bidders.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (
BidderYieldmo BidderName = "yieldmo"
BidderYieldone BidderName = "yieldone"
BidderZeroClickFraud BidderName = "zeroclickfraud"
BidderYeahmobi BidderName = "yeahmobi"
)
zhaojp marked this conversation as resolved.
Show resolved Hide resolved

// BidderMap stores all the valid OpenRTB 2.x Bidders in the project. This map *must not* be mutated.
Expand Down Expand Up @@ -151,6 +152,7 @@ var BidderMap = map[string]BidderName{
"yieldmo": BidderYieldmo,
"yieldone": BidderYieldone,
"zeroclickfraud": BidderZeroClickFraud,
"yeahmobi": BidderYeahmobi,
}
zhaojp marked this conversation as resolved.
Show resolved Hide resolved

// BidderList returns the values of the BidderMap
Expand Down
7 changes: 7 additions & 0 deletions openrtb_ext/imp_yeahmobi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package openrtb_ext

// ExtImpYeahmobi defines the contract for bidrequest.imp[i].ext.yeahmobi
type ExtImpYeahmobi struct {
PubId string `json:"pubId"`
ZoneId string `json:"zoneId"`
}
8 changes: 8 additions & 0 deletions static/bidder-info/yeahmobi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
maintainer:
email: "junping.zhao@yeahmobi.com"
capabilities:
app:
mediaTypes:
- banner
- video
- native
Loading