Skip to content

Commit

Permalink
New Adapter: Smile Wanted (prebid#1877)
Browse files Browse the repository at this point in the history
* New Adapter: Smile Wanted

* Implementations of changes requested by : prebid#1877 (review)

* Improvement of test coverage as requested.

* Implementations of changes requested by : prebid#1877 (review)
  • Loading branch information
MaxSmileWanted authored and jizeyopera committed Oct 13, 2021
1 parent 54f13c2 commit de1b5c2
Show file tree
Hide file tree
Showing 20 changed files with 702 additions and 0 deletions.
58 changes: 58 additions & 0 deletions adapters/smilewanted/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package smilewanted

import (
"encoding/json"
"testing"

"github.com/prebid/prebid-server/openrtb_ext"
)

// This file actually intends to test static/bidder-params/smilewanted.json
//
// These also validate the format of the external API: request.imp[i].ext.smilewanted

// TestValidParams makes sure that the smilewanted schema accepts all imp.ext fields which we intend to support.
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.BidderSmileWanted, json.RawMessage(validParam)); err != nil {
t.Errorf("Schema rejected SmileWanted params: %s", validParam)
}
}
}

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

var validParams = []string{
`{"zoneId": "zone_code"}`,
}

var invalidParams = []string{
`{"zoneId": 100}`,
`{"zoneId": true}`,
`{"zoneId": 123}`,
`{"zoneID": "1"}`,
``,
`null`,
`true`,
`9`,
`1.2`,
`[]`,
`{}`,
}
106 changes: 106 additions & 0 deletions adapters/smilewanted/smilewanted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package smilewanted

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

"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/openrtb_ext"
)

type adapter struct {
URI string
}

func (a *adapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {

request.AT = 1 //Defaulting to first price auction for all prebid requests

reqJSON, err := json.Marshal(request)
if err != nil {
return nil, []error{&errortypes.BadInput{
Message: fmt.Sprintf("Json not encoded. err: %s", 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")
headers.Add("sw-integration-type", "prebid_server")

return []*adapters.RequestData{{
Method: "POST",
Uri: a.URI,
Body: reqJSON,
Headers: headers,
}}, []error{}
}

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. Run with request.debug = 1 for more info.", response.StatusCode),
}}
}

var bidResp openrtb2.BidResponse
if err := json.Unmarshal(response.Body, &bidResp); err != nil {
return nil, []error{&errortypes.BadServerResponse{
Message: fmt.Sprintf("Bad server response: %s.", err),
}}
}

var bidReq openrtb2.BidRequest
if err := json.Unmarshal(externalRequest.Body, &bidReq); err != nil {
return nil, []error{err}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(bidResp.SeatBid[0].Bid))
sb := bidResp.SeatBid[0]
for i := 0; i < len(sb.Bid); i++ {
bid := sb.Bid[i]
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: getMediaTypeForImp(bid.ImpID, internalRequest.Imp),
})
}
return bidResponse, nil
}

// getMediaTypeForImp figures out which media type this bid is for.
func getMediaTypeForImp(impId string, imps []openrtb2.Imp) openrtb_ext.BidType {
mediaType := openrtb_ext.BidTypeBanner //default type
for _, imp := range imps {
if imp.ID == impId {
if imp.Video != nil {
mediaType = openrtb_ext.BidTypeVideo
}
return mediaType
}
}
return mediaType
}

// Builder builds a new instance of the SmileWanted adapter for the given bidder with the given config.
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter) (adapters.Bidder, error) {
bidder := &adapter{
URI: config.Endpoint,
}
return bidder, nil
}
20 changes: 20 additions & 0 deletions adapters/smilewanted/smilewanted_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package smilewanted

import (
"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.BidderSmileWanted, config.Adapter{
Endpoint: "http://example.com"})

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

adapterstest.RunJSONBidderTest(t, "smilewantedtest", bidder)
}
94 changes: 94 additions & 0 deletions adapters/smilewanted/smilewantedtest/exemplary/simple-banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"at" : 1,
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"bidder": {
"zoneId": "zone_code_test_display"
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "http://example.com",
"body": {
"id": "test-request-id",
"at" : 1,
"imp": [
{
"id": "test-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
}
]
},
"ext": {
"bidder": {
"zoneId": "zone_code_test_display"
}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"seatbid": [
{
"seat": "smilewanted",
"bid": [{
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 0.5,
"adm": "some-test-ad",
"crid": "crid_10",
"h": 250,
"w": 300
}]
}
],
"cur": "USD"
}
}
}
],

"expectedBidResponses": [
{
"bids": [
{
"bid": {
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 0.5,
"adm": "some-test-ad",
"crid": "crid_10",
"h": 250,
"w": 300
},
"type": "banner"
}
]
}
]
}
87 changes: 87 additions & 0 deletions adapters/smilewanted/smilewantedtest/exemplary/simple-video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"mockBidRequest": {
"id": "test-request-id",
"imp": [
{
"id": "test-imp-id",
"video": {
"mimes": ["video/mp4"],
"protocols": [2, 5],
"w": 1024,
"h": 576
},
"ext":{
"bidder":{
"zoneId": "zone_code_test_video"
}
}
}
]
},

"httpCalls": [
{
"expectedRequest": {
"uri": "http://example.com",
"body": {
"id": "test-request-id",
"at": 1,
"imp": [
{
"id": "test-imp-id",
"video": {
"mimes": ["video/mp4"],
"protocols": [2, 5],
"w": 1024,
"h": 576
},
"ext": {
"bidder": {
"zoneId": "zone_code_test_video"
}
}
}
]
}
},
"mockResponse": {
"status": 200,
"body": {
"id": "test-request-id",
"cur": "USD",
"seatbid": [
{
"seat": "smilewanted",
"bid": [{
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 0.5,
"adm": "some-test-ad",
"crid": "crid_10",
"w": 1024,
"h": 576
}]
}
]
}
}
}
],

"expectedBidResponses": [
{
"bids": [{
"bid": {
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800",
"impid": "test-imp-id",
"price": 0.5,
"adm": "some-test-ad",
"crid": "crid_10",
"w": 1024,
"h": 576
},
"type": "video"
}]
}
]
}
3 changes: 3 additions & 0 deletions adapters/smilewanted/smilewantedtest/params/race/banner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zoneId": "zone_code_test_display"
}
3 changes: 3 additions & 0 deletions adapters/smilewanted/smilewantedtest/params/race/video.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zoneId": "zone_code_test_video"
}
Loading

0 comments on commit de1b5c2

Please sign in to comment.