This repository has been archived by the owner on Dec 22, 2022. It is now read-only.
forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: junping.zhao <[email protected]>
- Loading branch information
Showing
21 changed files
with
881 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": ""}`, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package yeahmobi | ||
|
||
import ( | ||
"encoding/json" | ||
"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" | ||
"net/url" | ||
"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("Unable parse url template err:" + err.Error()) | ||
return nil | ||
} | ||
return &YeahmobiAdapter{EndpointTemplate: *tpl} | ||
} | ||
|
||
func (adapter *YeahmobiAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
var adapterRequests []*adapters.RequestData | ||
|
||
adapterRequest, errs := adapter.makeRequest(request) | ||
if errs == nil { | ||
adapterRequests = append(adapterRequests, adapterRequest) | ||
} | ||
|
||
return adapterRequests, errs | ||
} | ||
|
||
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) | ||
if err != nil { | ||
return nil, append(errs, err) | ||
} | ||
transform(request) | ||
reqBody, err := json.Marshal(request) | ||
|
||
if err != nil { | ||
return nil, 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{} | ||
nativeCopyRequest := make(map[string]interface{}) | ||
err := json.Unmarshal([]byte(request.Imp[i].Native.Request), &nativeRequest) | ||
//just ignore the bad native request | ||
if err == nil { | ||
_, exists := nativeRequest["native"] | ||
if exists { | ||
continue | ||
} | ||
|
||
nativeCopyRequest["native"] = nativeRequest | ||
nativeReqByte, err := json.Marshal(nativeCopyRequest) | ||
//just ignore the bad native request | ||
if err != nil { | ||
continue | ||
} | ||
|
||
request.Imp[i].Native.Request = string(nativeReqByte) | ||
} | ||
} | ||
} | ||
} | ||
|
||
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 | ||
} | ||
break | ||
} | ||
|
||
return &extImpYeahmobi, errs | ||
|
||
} | ||
|
||
func (adapter *YeahmobiAdapter) getEndpoint(ext *openrtb_ext.ExtImpYeahmobi) (string, error) { | ||
return macros.ResolveMacros(adapter.EndpointTemplate, macros.EndpointTemplateParams{Host: "gw-" + url.QueryEscape(ext.ZoneId) + "-bid.yeahtargeter.com"}) | ||
} | ||
|
||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"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": 204, | ||
"body": {} | ||
} | ||
} | ||
], | ||
|
||
"expectedBidResponses": [] | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
adapters/yeahmobi/yeahmobitest/exemplary/simple-banner.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.