@@ -11,10 +11,11 @@ use crate::client::ad_response::{
1111 pop_request_hash_from_url, AdImage , AdResponse , AdResponseValue , AdSpoc , AdTile ,
1212} ;
1313use crate :: client:: config:: AdsClientConfig ;
14- use crate :: client:: telemetry:: { AdsTelemetry , ClientOperationEvent } ;
14+ use crate :: client:: telemetry:: ClientOperationEvent ;
1515use crate :: error:: { RecordClickError , RecordImpressionError , ReportAdError , RequestAdsError } ;
16- use crate :: http_cache:: { HttpCache , RequestCachePolicy } ;
16+ use crate :: http_cache:: { CacheOutcome , HttpCache , HttpCacheBuilderError , RequestCachePolicy } ;
1717use crate :: mars:: MARSClient ;
18+ use crate :: telemetry:: Telemetry ;
1819use ad_request:: { AdPlacementRequest , AdRequest } ;
1920use context_id:: { ContextIDComponent , DefaultContextIdCallback } ;
2021use url:: Url ;
@@ -30,14 +31,34 @@ pub mod telemetry;
3031const DEFAULT_TTL_SECONDS : u64 = 300 ;
3132const DEFAULT_MAX_CACHE_SIZE_MIB : u64 = 10 ;
3233
33- pub struct AdsClient {
34- client : MARSClient ,
34+ pub struct AdsClient < T >
35+ where
36+ T : Telemetry < CacheOutcome >
37+ + Telemetry < ClientOperationEvent >
38+ + Telemetry < HttpCacheBuilderError >
39+ + Telemetry < RecordClickError >
40+ + Telemetry < RecordImpressionError >
41+ + Telemetry < ReportAdError >
42+ + Telemetry < RequestAdsError >
43+ + Telemetry < serde_json:: Error > ,
44+ {
45+ client : MARSClient < T > ,
3546 context_id_component : ContextIDComponent ,
36- telemetry : Arc < dyn AdsTelemetry > ,
47+ telemetry : Arc < T > ,
3748}
3849
39- impl AdsClient {
40- pub fn new ( client_config : AdsClientConfig ) -> Self {
50+ impl < T > AdsClient < T >
51+ where
52+ T : Telemetry < CacheOutcome >
53+ + Telemetry < serde_json:: Error >
54+ + Telemetry < ClientOperationEvent >
55+ + Telemetry < HttpCacheBuilderError >
56+ + Telemetry < RecordClickError >
57+ + Telemetry < RecordImpressionError >
58+ + Telemetry < ReportAdError >
59+ + Telemetry < RequestAdsError > ,
60+ {
61+ pub fn new ( client_config : AdsClientConfig < T > ) -> Self {
4162 let context_id = Uuid :: new_v4 ( ) . to_string ( ) ;
4263 let context_id_component = ContextIDComponent :: new (
4364 & context_id,
@@ -97,35 +118,19 @@ impl AdsClient {
97118 client
98119 }
99120
100- #[ cfg( test) ]
101- pub fn new_with_mars_client ( client : MARSClient ) -> Self {
102- use crate :: client:: telemetry:: PrintAdsTelemetry ;
103-
104- let context_id_component = ContextIDComponent :: new (
105- & uuid:: Uuid :: new_v4 ( ) . to_string ( ) ,
106- 0 ,
107- false ,
108- Box :: new ( DefaultContextIdCallback ) ,
109- ) ;
110- Self {
111- context_id_component,
112- client,
113- telemetry : Arc :: new ( PrintAdsTelemetry ) ,
114- }
115- }
116-
117- fn request_ads < T > (
121+ fn request_ads < A > (
118122 & self ,
119123 ad_placement_requests : Vec < AdPlacementRequest > ,
120124 options : Option < RequestCachePolicy > ,
121- ) -> Result < AdResponse < T > , RequestAdsError >
125+ ) -> Result < AdResponse < A > , RequestAdsError >
122126 where
123- T : AdResponseValue ,
127+ A : AdResponseValue ,
124128 {
125129 let context_id = self . get_context_id ( ) ?;
126130 let ad_request = AdRequest :: build ( context_id, ad_placement_requests) ?;
127131 let cache_policy = options. unwrap_or_default ( ) ;
128- let ( mut response, request_hash) = self . client . fetch_ads ( & ad_request, & cache_policy) ?;
132+ let ( mut response, request_hash) =
133+ self . client . fetch_ads :: < A > ( & ad_request, & cache_policy) ?;
129134 response. add_request_hash_to_callbacks ( & request_hash) ;
130135 Ok ( response)
131136 }
@@ -237,18 +242,30 @@ impl AdsClient {
237242mod tests {
238243 use crate :: {
239244 client:: config:: Environment ,
240- client:: telemetry:: PrintAdsTelemetry ,
241245 test_utils:: {
242246 get_example_happy_image_response, get_example_happy_spoc_response,
243- get_example_happy_uatile_response, make_happy_placement_requests,
247+ get_example_happy_uatile_response, make_happy_placement_requests, PrintAdsTelemetry ,
244248 } ,
245249 } ;
246250
247251 use super :: * ;
248252
253+ fn new_with_mars_client ( client : MARSClient < PrintAdsTelemetry > ) -> AdsClient < PrintAdsTelemetry > {
254+ let context_id_component = ContextIDComponent :: new (
255+ & uuid:: Uuid :: new_v4 ( ) . to_string ( ) ,
256+ 0 ,
257+ false ,
258+ Box :: new ( DefaultContextIdCallback ) ,
259+ ) ;
260+ AdsClient {
261+ context_id_component,
262+ client,
263+ telemetry : Arc :: new ( PrintAdsTelemetry ) ,
264+ }
265+ }
266+
249267 #[ test]
250268 fn test_get_context_id ( ) {
251- use crate :: client:: telemetry:: PrintAdsTelemetry ;
252269 use std:: sync:: Arc ;
253270 let config = AdsClientConfig {
254271 environment : Environment :: Test ,
@@ -262,7 +279,6 @@ mod tests {
262279
263280 #[ test]
264281 fn test_cycle_context_id ( ) {
265- use crate :: client:: telemetry:: PrintAdsTelemetry ;
266282 use std:: sync:: Arc ;
267283 let config = AdsClientConfig {
268284 environment : Environment :: Test ,
@@ -288,8 +304,9 @@ mod tests {
288304 . with_body ( serde_json:: to_string ( & expected_response. data ) . unwrap ( ) )
289305 . create ( ) ;
290306
291- let mars_client = MARSClient :: new ( Environment :: Test , None , Arc :: new ( PrintAdsTelemetry ) ) ;
292- let ads_client = AdsClient :: new_with_mars_client ( mars_client) ;
307+ let telemetry = Arc :: new ( PrintAdsTelemetry ) ;
308+ let mars_client = MARSClient :: new ( Environment :: Test , None , telemetry. clone ( ) ) ;
309+ let ads_client = new_with_mars_client ( mars_client) ;
293310
294311 let ad_placement_requests = make_happy_placement_requests ( ) ;
295312
@@ -309,8 +326,9 @@ mod tests {
309326 . with_body ( serde_json:: to_string ( & expected_response. data ) . unwrap ( ) )
310327 . create ( ) ;
311328
312- let mars_client = MARSClient :: new ( Environment :: Test , None , Arc :: new ( PrintAdsTelemetry ) ) ;
313- let ads_client = AdsClient :: new_with_mars_client ( mars_client) ;
329+ let telemetry = Arc :: new ( PrintAdsTelemetry ) ;
330+ let mars_client = MARSClient :: new ( Environment :: Test , None , telemetry. clone ( ) ) ;
331+ let ads_client = new_with_mars_client ( mars_client) ;
314332
315333 let ad_placement_requests = make_happy_placement_requests ( ) ;
316334
@@ -330,8 +348,9 @@ mod tests {
330348 . with_body ( serde_json:: to_string ( & expected_response. data ) . unwrap ( ) )
331349 . create ( ) ;
332350
333- let mars_client = MARSClient :: new ( Environment :: Test , None , Arc :: new ( PrintAdsTelemetry ) ) ;
334- let ads_client = AdsClient :: new_with_mars_client ( mars_client) ;
351+ let telemetry = Arc :: new ( PrintAdsTelemetry ) ;
352+ let mars_client = MARSClient :: new ( Environment :: Test , None , telemetry. clone ( ) ) ;
353+ let ads_client = new_with_mars_client ( mars_client) ;
335354
336355 let ad_placement_requests = make_happy_placement_requests ( ) ;
337356
@@ -346,9 +365,9 @@ mod tests {
346365 let cache = HttpCache :: builder ( "test_record_click_invalidates_cache" )
347366 . build ( )
348367 . unwrap ( ) ;
349- let mars_client =
350- MARSClient :: new ( Environment :: Test , Some ( cache) , Arc :: new ( PrintAdsTelemetry ) ) ;
351- let ads_client = AdsClient :: new_with_mars_client ( mars_client) ;
368+ let telemetry = Arc :: new ( PrintAdsTelemetry ) ;
369+ let mars_client = MARSClient :: new ( Environment :: Test , Some ( cache) , telemetry . clone ( ) ) ;
370+ let ads_client = new_with_mars_client ( mars_client) ;
352371
353372 let response = get_example_happy_image_response ( ) ;
354373
0 commit comments