forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
100 changed files
with
5,276 additions
and
946 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,94 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id", | ||
"site": { | ||
"page": "https://good.site/url" | ||
}, | ||
"imp": [{ | ||
"id": "test-imp-id", | ||
"native": { | ||
"ver": "1.1", | ||
"request": "{\"ver\":\"1.0\",\"layout\":1,\"adunit\":1,\"plcmttype\":1,\"plcmtcnt\":1,\"seq\":0,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":75}},{\"id\":2,\"required\":1,\"img\":{\"type\":3,\"wmin\":60,\"hmin\":60,\"mimes\":[\"image/jpeg\",\"image/jpg\",\"image/png\"]}},{\"id\":3,\"required\":0,\"data\":{\"type\":2,\"len\":75}},{\"id\":4,\"required\":0,\"data\":{\"type\":6,\"len\":1000}},{\"id\":5,\"required\":0,\"data\":{\"type\":7,\"len\":1000}},{\"id\":6,\"required\":0,\"data\":{\"type\":11,\"len\":1000}}]}" | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"zoneid": 123456 | ||
} | ||
} | ||
}] | ||
}, | ||
|
||
"httpCalls": [{ | ||
"expectedRequest": { | ||
"uri": "https://ssp-bidder.criteo.com/openrtb/pbs/auction/request?profile=230", | ||
"body": { | ||
"id": "test-request-id", | ||
"site": { | ||
"page": "https://good.site/url" | ||
}, | ||
"imp": [{ | ||
"id": "test-imp-id", | ||
"native": { | ||
"ver": "1.1", | ||
"request": "{\"ver\":\"1.0\",\"layout\":1,\"adunit\":1,\"plcmttype\":1,\"plcmtcnt\":1,\"seq\":0,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":75}},{\"id\":2,\"required\":1,\"img\":{\"type\":3,\"wmin\":60,\"hmin\":60,\"mimes\":[\"image/jpeg\",\"image/jpg\",\"image/png\"]}},{\"id\":3,\"required\":0,\"data\":{\"type\":2,\"len\":75}},{\"id\":4,\"required\":0,\"data\":{\"type\":6,\"len\":1000}},{\"id\":5,\"required\":0,\"data\":{\"type\":7,\"len\":1000}},{\"id\":6,\"required\":0,\"data\":{\"type\":11,\"len\":1000}}]}" | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"zoneid": 123456 | ||
} | ||
} | ||
}] | ||
}, | ||
"impIDs":["test-imp-id"] | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id", | ||
"seatbid": [{ | ||
"seat": "grid", | ||
"bid": [{ | ||
"id": "randomid", | ||
"impid": "test-imp-id", | ||
"price": 0.500000, | ||
"adid": "12345678", | ||
"adm": "{\"ver\":\"1.0\",\"layout\":1,\"adunit\":1,\"plcmttype\":1,\"plcmtcnt\":1,\"seq\":0,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":75}},{\"id\":2,\"required\":1,\"img\":{\"type\":3,\"wmin\":60,\"hmin\":60,\"mimes\":[\"image/jpeg\",\"image/jpg\",\"image/png\"]}},{\"id\":3,\"required\":0,\"data\":{\"type\":2,\"len\":75}},{\"id\":4,\"required\":0,\"data\":{\"type\":6,\"len\":1000}},{\"id\":5,\"required\":0,\"data\":{\"type\":7,\"len\":1000}},{\"id\":6,\"required\":0,\"data\":{\"type\":11,\"len\":1000}}]}", | ||
"ext": { | ||
"prebid": { | ||
"type": "native" | ||
} | ||
}, | ||
"cid": "987", | ||
"crid": "12345678", | ||
"h": 250, | ||
"w": 300 | ||
}] | ||
}], | ||
"cur": "USD" | ||
} | ||
} | ||
}], | ||
|
||
"expectedBidResponses": [{ | ||
"currency": "USD", | ||
"bids": [{ | ||
"bid": { | ||
"id": "randomid", | ||
"impid": "test-imp-id", | ||
"price": 0.5, | ||
"adm": "{\"ver\":\"1.0\",\"layout\":1,\"adunit\":1,\"plcmttype\":1,\"plcmtcnt\":1,\"seq\":0,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":75}},{\"id\":2,\"required\":1,\"img\":{\"type\":3,\"wmin\":60,\"hmin\":60,\"mimes\":[\"image/jpeg\",\"image/jpg\",\"image/png\"]}},{\"id\":3,\"required\":0,\"data\":{\"type\":2,\"len\":75}},{\"id\":4,\"required\":0,\"data\":{\"type\":6,\"len\":1000}},{\"id\":5,\"required\":0,\"data\":{\"type\":7,\"len\":1000}},{\"id\":6,\"required\":0,\"data\":{\"type\":11,\"len\":1000}}]}", | ||
"adid": "12345678", | ||
"cid": "987", | ||
"crid": "12345678", | ||
"w": 300, | ||
"h": 250, | ||
"ext": { | ||
"prebid": { | ||
"type": "native" | ||
} | ||
} | ||
}, | ||
"type": "native" | ||
}] | ||
}] | ||
} |
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,188 @@ | ||
package displayio | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"text/template" | ||
|
||
"github.com/prebid/openrtb/v20/openrtb2" | ||
"github.com/prebid/prebid-server/v2/adapters" | ||
"github.com/prebid/prebid-server/v2/config" | ||
"github.com/prebid/prebid-server/v2/errortypes" | ||
"github.com/prebid/prebid-server/v2/macros" | ||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
) | ||
|
||
type adapter struct { | ||
endpoint *template.Template | ||
} | ||
|
||
type reqDioExt struct { | ||
UserSession string `json:"userSession,omitempty"` | ||
PlacementId string `json:"placementId"` | ||
InventoryId string `json:"inventoryId"` | ||
} | ||
|
||
func (adapter *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
headers.Add("Accept", "application/json") | ||
headers.Add("x-openrtb-version", "2.5") | ||
|
||
result := make([]*adapters.RequestData, 0, len(request.Imp)) | ||
errs := make([]error, 0, len(request.Imp)) | ||
|
||
for _, impression := range request.Imp { | ||
var requestExt map[string]interface{} | ||
|
||
if impression.BidFloorCur == "" || impression.BidFloor == 0 { | ||
impression.BidFloorCur = "USD" | ||
} else if impression.BidFloorCur != "USD" { | ||
convertedValue, err := requestInfo.ConvertCurrency(impression.BidFloor, impression.BidFloorCur, "USD") | ||
|
||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
impression.BidFloor = convertedValue | ||
impression.BidFloorCur = "USD" | ||
} | ||
|
||
if len(impression.Ext) == 0 { | ||
errs = append(errs, errors.New("impression extensions required")) | ||
continue | ||
} | ||
|
||
var bidderExt adapters.ExtImpBidder | ||
err := json.Unmarshal(impression.Ext, &bidderExt) | ||
|
||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
var impressionExt openrtb_ext.ExtImpDisplayio | ||
err = json.Unmarshal(bidderExt.Bidder, &impressionExt) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
dioExt := reqDioExt{PlacementId: impressionExt.PlacementId, InventoryId: impressionExt.InventoryId} | ||
|
||
requestCopy := *request | ||
|
||
err = json.Unmarshal(requestCopy.Ext, &requestExt) | ||
if err != nil { | ||
requestExt = make(map[string]interface{}) | ||
} | ||
|
||
requestExt["displayio"] = dioExt | ||
|
||
requestCopy.Ext, err = json.Marshal(requestExt) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
requestCopy.Imp = []openrtb2.Imp{impression} | ||
body, err := json.Marshal(requestCopy) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
url, err := adapter.buildEndpointURL(&impressionExt) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
result = append(result, &adapters.RequestData{ | ||
Method: "POST", | ||
Uri: url, | ||
Body: body, | ||
Headers: headers, | ||
ImpIDs: openrtb_ext.GetImpIDs(requestCopy.Imp), | ||
}) | ||
} | ||
|
||
if len(result) == 0 { | ||
return nil, errs | ||
} | ||
return result, errs | ||
} | ||
|
||
// MakeBids translates Displayio bid response to prebid-server specific format | ||
func (adapter *adapter) MakeBids(internalRequest *openrtb2.BidRequest, _ *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
|
||
if adapters.IsResponseStatusCodeNoContent(responseData) { | ||
return nil, nil | ||
} | ||
|
||
if err := adapters.CheckResponseStatusCodeForErrors(responseData); err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
var bidResp openrtb2.BidResponse | ||
|
||
if err := json.Unmarshal(responseData.Body, &bidResp); err != nil { | ||
msg := fmt.Sprintf("Bad server response: %d", err) | ||
return nil, []error{&errortypes.BadServerResponse{Message: msg}} | ||
} | ||
|
||
if len(bidResp.SeatBid) != 1 { | ||
msg := fmt.Sprintf("Invalid SeatBids count: %d", len(bidResp.SeatBid)) | ||
return nil, []error{&errortypes.BadServerResponse{Message: msg}} | ||
} | ||
|
||
var errs []error | ||
bidResponse := adapters.NewBidderResponse() | ||
|
||
for _, sb := range bidResp.SeatBid { | ||
for i := range sb.Bid { | ||
bidType, err := getBidMediaTypeFromMtype(&sb.Bid[i]) | ||
if err != nil { | ||
errs = append(errs, err) | ||
} else { | ||
b := &adapters.TypedBid{ | ||
Bid: &sb.Bid[i], | ||
BidType: bidType, | ||
} | ||
bidResponse.Bids = append(bidResponse.Bids, b) | ||
} | ||
} | ||
} | ||
|
||
return bidResponse, errs | ||
} | ||
|
||
func Builder(_ openrtb_ext.BidderName, config config.Adapter, _ config.Server) (adapters.Bidder, error) { | ||
endpoint, err := template.New("endpointTemplate").Parse(config.Endpoint) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse endpoint url template: %v", err) | ||
} | ||
|
||
bidder := &adapter{ | ||
endpoint: endpoint, | ||
} | ||
return bidder, nil | ||
} | ||
|
||
func getBidMediaTypeFromMtype(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 | ||
default: | ||
return "", fmt.Errorf("unexpected media type for bid: %s", bid.ImpID) | ||
} | ||
} | ||
|
||
func (adapter *adapter) buildEndpointURL(params *openrtb_ext.ExtImpDisplayio) (string, error) { | ||
endpointParams := macros.EndpointTemplateParams{PublisherID: params.PublisherId} | ||
return macros.ResolveMacros(adapter.endpoint, endpointParams) | ||
} |
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,22 @@ | ||
package displayio | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prebid/prebid-server/v2/adapters/adapterstest" | ||
"github.com/prebid/prebid-server/v2/config" | ||
"github.com/prebid/prebid-server/v2/openrtb_ext" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
bidder, buildErr := Builder(openrtb_ext.BidderDisplayio, | ||
config.Adapter{Endpoint: "https://adapter.endpoint/?macro={{.PublisherID}}"}, | ||
config.Server{ExternalUrl: "https://server.endpoint/"}, | ||
) | ||
|
||
if buildErr != nil { | ||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
} | ||
|
||
adapterstest.RunJSONBidderTest(t, "displayiotest", bidder) | ||
} |
Oops, something went wrong.