-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f152b0d
commit 17109ca
Showing
19 changed files
with
899 additions
and
1 deletion.
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,189 @@ | ||
package yeahmobi | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"text/template" | ||
|
||
"github.com/prebid/openrtb/v19/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" | ||
) | ||
|
||
type adapter struct { | ||
EndpointTemplate *template.Template | ||
} | ||
|
||
// Builder builds a new instance of the Yeahmobi adapter for the given bidder with the given config. | ||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { | ||
template, 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: template, | ||
} | ||
return bidder, nil | ||
} | ||
|
||
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
var adapterRequests []*adapters.RequestData | ||
|
||
adapterRequest, errs := a.makeRequest(request) | ||
if errs == nil { | ||
adapterRequests = append(adapterRequests, adapterRequest) | ||
} | ||
|
||
return adapterRequests, errs | ||
} | ||
|
||
func (a *adapter) makeRequest(request *openrtb2.BidRequest) (*adapters.RequestData, []error) { | ||
var errs []error | ||
|
||
yeahmobiExt, errs := getYeahmobiExt(request) | ||
|
||
if yeahmobiExt == nil { | ||
return nil, errs | ||
} | ||
endPoint, err := a.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 *openrtb2.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 | ||
} | ||
|
||
nativeCopy := *request.Imp[i].Native | ||
nativeCopy.Request = string(nativeReqByte) | ||
request.Imp[i].Native = &nativeCopy | ||
} | ||
} | ||
} | ||
} | ||
|
||
func getYeahmobiExt(request *openrtb2.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 (a *adapter) getEndpoint(ext *openrtb_ext.ExtImpYeahmobi) (string, error) { | ||
return macros.ResolveMacros(a.EndpointTemplate, macros.EndpointTemplateParams{Host: "gw-" + url.QueryEscape(ext.ZoneId) + "-bid.yeahtargeter.com"}) | ||
} | ||
|
||
// MakeBids make the bids for the bid response. | ||
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 _, 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 []openrtb2.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,28 @@ | ||
package yeahmobi | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/adapters/adapterstest" | ||
"github.com/prebid/prebid-server/config" | ||
"github.com/prebid/prebid-server/openrtb_ext" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
bidder, buildErr := Builder(openrtb_ext.BidderYeahmobi, config.Adapter{ | ||
Endpoint: "https://{{.Host}}/prebid/bid"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||
|
||
if buildErr != nil { | ||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
} | ||
|
||
adapterstest.RunJSONBidderTest(t, "yeahmobitest", bidder) | ||
} | ||
|
||
func TestEndpointTemplateMalformed(t *testing.T) { | ||
_, buildErr := Builder(openrtb_ext.BidderYeahmobi, config.Adapter{ | ||
Endpoint: "{{Malformed}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||
|
||
assert.Error(t, buildErr) | ||
} |
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": [] | ||
|
||
} |
Oops, something went wrong.