-
Notifications
You must be signed in to change notification settings - Fork 739
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: Faruk Çam <mail@farukix.com>
- Loading branch information
Showing
18 changed files
with
1,203 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,138 @@ | ||
package admatic | ||
|
||
import ( | ||
"encoding/json" | ||
"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 | ||
} | ||
|
||
func Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) { | ||
endpointTemplate, err := template.New("endpointTemplate").Parse(config.Endpoint) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse endpoint template: %v", err) | ||
} | ||
|
||
bidder := &adapter{ | ||
endpoint: endpointTemplate, | ||
} | ||
return bidder, nil | ||
} | ||
|
||
func (a *adapter) MakeRequests(request *openrtb2.BidRequest, requestInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
var requests []*adapters.RequestData | ||
var errs []error | ||
|
||
requestCopy := *request | ||
for _, imp := range request.Imp { | ||
requestCopy.Imp = []openrtb2.Imp{imp} | ||
|
||
endpoint, err := a.buildEndpointFromRequest(&imp) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
requestJSON, err := json.Marshal(requestCopy) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
request := &adapters.RequestData{ | ||
Method: http.MethodPost, | ||
Body: requestJSON, | ||
Uri: endpoint, | ||
ImpIDs: openrtb_ext.GetImpIDs(requestCopy.Imp), | ||
} | ||
|
||
requests = append(requests, request) | ||
} | ||
|
||
return requests, errs | ||
} | ||
|
||
func (a *adapter) buildEndpointFromRequest(imp *openrtb2.Imp) (string, error) { | ||
var impExt adapters.ExtImpBidder | ||
if err := json.Unmarshal(imp.Ext, &impExt); err != nil { | ||
return "", &errortypes.BadInput{ | ||
Message: fmt.Sprintf("Failed to deserialize bidder impression extension: %v", err), | ||
} | ||
} | ||
|
||
var admaticExt openrtb_ext.ImpExtAdmatic | ||
if err := json.Unmarshal(impExt.Bidder, &admaticExt); err != nil { | ||
return "", &errortypes.BadInput{ | ||
Message: fmt.Sprintf("Failed to deserialize AdMatic extension: %v", err), | ||
} | ||
} | ||
|
||
endpointParams := macros.EndpointTemplateParams{ | ||
Host: admaticExt.Host, | ||
} | ||
|
||
return macros.ResolveMacros(a.endpoint, endpointParams) | ||
} | ||
|
||
func (a *adapter) MakeBids(request *openrtb2.BidRequest, requestData *adapters.RequestData, responseData *adapters.ResponseData) (*adapters.BidderResponse, []error) { | ||
if adapters.IsResponseStatusCodeNoContent(responseData) { | ||
return nil, nil | ||
} | ||
|
||
err := adapters.CheckResponseStatusCodeForErrors(responseData) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
var response openrtb2.BidResponse | ||
if err := json.Unmarshal(responseData.Body, &response); err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(request.Imp)) | ||
if len(response.Cur) != 0 { | ||
bidResponse.Currency = response.Cur | ||
} | ||
|
||
for _, seatBid := range response.SeatBid { | ||
for i := range seatBid.Bid { | ||
|
||
bidMediaType, err := getMediaTypeForBid(seatBid.Bid[i].ImpID, request.Imp) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
Bid: &seatBid.Bid[i], | ||
BidType: bidMediaType, | ||
}) | ||
} | ||
} | ||
return bidResponse, nil | ||
} | ||
|
||
func getMediaTypeForBid(impID string, imps []openrtb2.Imp) (openrtb_ext.BidType, error) { | ||
for _, imp := range imps { | ||
if imp.ID == impID { | ||
if imp.Banner != nil { | ||
return openrtb_ext.BidTypeBanner, nil | ||
} else if imp.Video != nil { | ||
return openrtb_ext.BidTypeVideo, nil | ||
} else if imp.Native != nil { | ||
return openrtb_ext.BidTypeNative, nil | ||
} | ||
} | ||
} | ||
return "", &errortypes.BadServerResponse{ | ||
Message: fmt.Sprintf("The impression with ID %s is not present into the request", impID), | ||
} | ||
} |
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,29 @@ | ||
package admatic | ||
|
||
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" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
bidder, buildErr := Builder(openrtb_ext.BidderAdmatic, config.Adapter{ | ||
Endpoint: "http://pbs.admatic.com.tr?host={{.Host}}"}, | ||
config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1281, DataCenter: "2"}) | ||
|
||
if buildErr != nil { | ||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
} | ||
|
||
adapterstest.RunJSONBidderTest(t, "admatictest", bidder) | ||
} | ||
|
||
func TestEndpointTemplateMalformed(t *testing.T) { | ||
_, buildErr := Builder(openrtb_ext.BidderAdmatic, config.Adapter{ | ||
Endpoint: "host={{Host}}"}, config.Server{ExternalUrl: "http://hosturl.com"}) | ||
|
||
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,96 @@ | ||
{ | ||
"mockBidRequest": { | ||
"id": "test-request-id-banner", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id-banner", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 728, | ||
"h": 90 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"host": "layer.serve.admatic.com.tr", | ||
"networkId": 12345 | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"httpCalls": [ | ||
{ | ||
"expectedRequest": { | ||
"uri": "http://pbs.admatic.com.tr?host=layer.serve.admatic.com.tr", | ||
"body": { | ||
"id": "test-request-id-banner", | ||
"imp": [ | ||
{ | ||
"id": "test-imp-id-banner", | ||
"banner": { | ||
"format": [ | ||
{ | ||
"w": 728, | ||
"h": 90 | ||
} | ||
] | ||
}, | ||
"ext": { | ||
"bidder": { | ||
"host": "layer.serve.admatic.com.tr", | ||
"networkId": 12345 | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"impIDs": ["test-imp-id-banner"] | ||
}, | ||
"mockResponse": { | ||
"status": 200, | ||
"body": { | ||
"id": "test-request-id-banner", | ||
"seatbid": [ | ||
{ | ||
"seat": "admatic", | ||
"bid": [ | ||
{ | ||
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800", | ||
"impid": "test-imp-id-banner", | ||
"price": 0.5, | ||
"adm": "some-test-ad-banner", | ||
"crid": "crid_10", | ||
"w": 728, | ||
"h": 90 | ||
} | ||
] | ||
} | ||
], | ||
"cur": "USD" | ||
} | ||
} | ||
} | ||
], | ||
"expectedBidResponses": [ | ||
{ | ||
"currency": "USD", | ||
"bids": [ | ||
{ | ||
"bid": { | ||
"id": "8ee514f1-b2b8-4abb-89fd-084437d1e800", | ||
"impid": "test-imp-id-banner", | ||
"price": 0.5, | ||
"adm": "some-test-ad-banner", | ||
"crid": "crid_10", | ||
"w": 728, | ||
"h": 90 | ||
}, | ||
"type": "banner" | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.