-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Teads Adapter with Custom rendering handling
- Loading branch information
1 parent
4ec839e
commit eb1a213
Showing
10 changed files
with
620 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,178 @@ | ||
package teads | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"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" | ||
) | ||
|
||
// Builder builds a new instance of the Teads adapter for the given bidder with the given config. | ||
func Builder(_ 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 := &TeadsAdapter{ | ||
endpointTemplate: template, | ||
} | ||
return bidder, nil | ||
} | ||
|
||
func (a *TeadsAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) { | ||
if len(request.Imp) == 0 { | ||
return nil, []error{&errortypes.BadInput{ | ||
Message: "No impression in the bid request", | ||
}} | ||
} | ||
|
||
endpointURL, err := a.buildEndpointURL() | ||
if endpointURL == "" { | ||
return nil, []error{err} | ||
} | ||
|
||
if err := updateImpObject(request.Imp); err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
reqJSON, err := json.Marshal(request) | ||
if err != nil { | ||
return nil, []error{&errortypes.BadInput{ | ||
Message: "Error parsing BidRequest object", | ||
}} | ||
} | ||
|
||
headers := http.Header{} | ||
headers.Add("Content-Type", "application/json;charset=utf-8") | ||
return []*adapters.RequestData{{ | ||
Method: "POST", | ||
Uri: endpointURL, | ||
Body: reqJSON, | ||
Headers: headers, | ||
}}, []error{} | ||
} | ||
|
||
func updateImpObject(imps []openrtb2.Imp) error { | ||
for i := range imps { | ||
imp := &imps[i] | ||
|
||
if imp.Banner != nil { | ||
if len(imp.Banner.Format) != 0 { | ||
bannerCopy := *imp.Banner | ||
bannerCopy.H = &imp.Banner.Format[0].H | ||
bannerCopy.W = &imp.Banner.Format[0].W | ||
imp.Banner = &bannerCopy | ||
} | ||
} | ||
|
||
var defaultImpExt DefaultBidderImpExtension | ||
if err := json.Unmarshal(imp.Ext, &defaultImpExt); err != nil { | ||
return &errortypes.BadInput{ | ||
Message: "Error parsing Imp.Ext object", | ||
} | ||
} | ||
imp.TagID = strconv.Itoa(defaultImpExt.Bidder.PlacementId) | ||
teadsImpExt := &TeadsImpExtension{ | ||
KV: TeadsKV{ | ||
PlacementId: defaultImpExt.Bidder.PlacementId, | ||
}, | ||
} | ||
if extJson, err := json.Marshal(teadsImpExt); err != nil { | ||
return &errortypes.BadInput{ | ||
Message: "Error stringify Imp.Ext object", | ||
} | ||
} else { | ||
imp.Ext = extJson | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Builds enpoint url based on adapter-specific pub settings from imp.ext | ||
func (a *TeadsAdapter) buildEndpointURL() (string, error) { | ||
endpointParams := macros.EndpointTemplateParams{} | ||
host, err := macros.ResolveMacros(a.endpointTemplate, endpointParams) | ||
|
||
if err != nil { | ||
return "", &errortypes.BadInput{ | ||
Message: "Unable to parse endpoint url template: " + err.Error(), | ||
} | ||
} | ||
|
||
thisURI, err := url.Parse(host) | ||
if err != nil { | ||
return "", &errortypes.BadInput{ | ||
Message: "Malformed URL: " + err.Error(), | ||
} | ||
} | ||
|
||
return thisURI.String(), nil | ||
} | ||
|
||
func (a *TeadsAdapter) MakeBids(internalRequest *openrtb2.BidRequest, _ *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. Run with request.debug = 1 for more info", response.StatusCode), | ||
}} | ||
} | ||
if response.StatusCode != http.StatusOK { | ||
return nil, []error{fmt.Errorf("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{err} | ||
} | ||
|
||
bidResponse := adapters.NewBidderResponseWithBidsCapacity(len(bidResp.SeatBid)) | ||
|
||
for _, sb := range bidResp.SeatBid { | ||
for i := 0; i < len(sb.Bid); i++ { | ||
bid := sb.Bid[i] | ||
|
||
var bidExtTeads TeadsBidExt | ||
if err := json.Unmarshal(bid.Ext, &bidExtTeads); err != nil { | ||
return nil, []error{err} | ||
} | ||
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{ | ||
Bid: &bid, | ||
BidMeta: &openrtb_ext.ExtBidPrebidMeta{ | ||
RendererName: bidExtTeads.Prebid.Meta.RendererName, | ||
RendererVersion: bidExtTeads.Prebid.Meta.RendererVersion, | ||
}, | ||
BidType: getMediaTypeForImp(bid.ImpID, internalRequest.Imp), | ||
}) | ||
} | ||
} | ||
if bidResp.Cur != "" { | ||
bidResponse.Currency = bidResp.Cur | ||
} | ||
return bidResponse, []error{} | ||
} | ||
|
||
func getMediaTypeForImp(impID string, imps []openrtb2.Imp) openrtb_ext.BidType { | ||
for _, imp := range imps { | ||
if imp.ID == impID { | ||
if imp.Video != nil { | ||
return openrtb_ext.BidTypeVideo | ||
} else if imp.Native != nil { | ||
return openrtb_ext.BidTypeNative | ||
} | ||
return openrtb_ext.BidTypeBanner | ||
} | ||
} | ||
return openrtb_ext.BidTypeBanner | ||
} |
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,27 @@ | ||
package teads | ||
|
||
import ( | ||
"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" | ||
"testing" | ||
) | ||
|
||
func TestJsonSamples(t *testing.T) { | ||
bidder, buildErr := Builder(openrtb_ext.BidderTeads, config.Adapter{ | ||
Endpoint: "https://r.teads.tv/rich/"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) | ||
|
||
if buildErr != nil { | ||
t.Fatalf("Builder returned unexpected error %v", buildErr) | ||
} | ||
|
||
adapterstest.RunJSONBidderTest(t, "teadstest", bidder) | ||
} | ||
|
||
func TestEndpointTemplateMalformed(t *testing.T) { | ||
_, buildErr := Builder(openrtb_ext.BidderTeads, 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,40 @@ | ||
package teads | ||
|
||
import ( | ||
"encoding/json" | ||
"text/template" | ||
) | ||
|
||
type TeadsAdapter struct { | ||
endpointTemplate *template.Template | ||
} | ||
|
||
type DefaultBidderImpExtension struct { | ||
Bidder Bidder `json:"bidder"` | ||
} | ||
|
||
type Bidder struct { | ||
PlacementId int `json:"placementId"` | ||
} | ||
|
||
type TeadsImpExtension struct { | ||
KV TeadsKV `json:"kv"` | ||
} | ||
|
||
type TeadsKV struct { | ||
PlacementId int `json:"placementId"` | ||
} | ||
|
||
type TeadsBidExt struct { | ||
Prebid TeadsPrebidExt `json:"prebid"` | ||
} | ||
|
||
type TeadsPrebidExt struct { | ||
Meta TeadsPrebidMeta `json:"meta"` | ||
} | ||
|
||
type TeadsPrebidMeta struct { | ||
RendererName string `json:"rendererName"` | ||
RendererVersion string `json:"rendererVersion"` | ||
RendererData json.RawMessage `json:"rendererData"` | ||
} |
Oops, something went wrong.