Skip to content

Commit d1a59c3

Browse files
committed
More polishment
1 parent 891bc2f commit d1a59c3

35 files changed

+546
-572
lines changed

src/apify_client/_apify_client.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,14 @@ def __init__(
8787
min_delay_between_retries_millis=min_delay_between_retries_millis,
8888
timeout_secs=timeout_secs,
8989
)
90-
91-
self.stats = ClientStatistics()
92-
self.http_client = HttpClient(config=self._config, stats=self.stats)
90+
self._statistics = ClientStatistics()
91+
self._http_client = HttpClient(config=self._config, statistics=self._statistics)
9392

9493
def _options(self) -> dict:
9594
return {
9695
'base_url': self._config.base_url,
9796
'public_base_url': self._config.public_base_url,
98-
'http_client': self.http_client,
97+
'http_client': self._http_client,
9998
}
10099

101100
def actor(self, actor_id: str) -> ActorClient:
@@ -274,15 +273,14 @@ def __init__(
274273
min_delay_between_retries_millis=min_delay_between_retries_millis,
275274
timeout_secs=timeout_secs,
276275
)
277-
278-
self.stats = ClientStatistics()
279-
self.http_client = HttpClientAsync(config=self._config, stats=self.stats)
276+
self._statistics = ClientStatistics()
277+
self._http_client = HttpClientAsync(config=self._config, statistics=self._statistics)
280278

281279
def _options(self) -> dict:
282280
return {
283281
'base_url': self._config.base_url,
284282
'public_base_url': self._config.public_base_url,
285-
'http_client': self.http_client,
283+
'http_client': self._http_client,
286284
}
287285

288286
def actor(self, actor_id: str) -> ActorClientAsync:

src/apify_client/_http_client.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
class _BaseHttpClient:
3434
"""Base class for HTTP clients with shared configuration and utilities."""
3535

36-
def __init__(self, config: ClientConfig, stats: ClientStatistics | None = None) -> None:
36+
def __init__(self, config: ClientConfig, statistics: ClientStatistics | None = None) -> None:
3737
"""Initialize HTTP client with configuration.
3838
3939
Args:
4040
config: Immutable client configuration.
41-
stats: Optional statistics tracker.
41+
statistics: Optional statistics tracker.
4242
"""
43-
self.config = config
44-
self.stats = stats or ClientStatistics()
43+
self._config = config
44+
self._statistics = statistics or ClientStatistics()
4545

4646
# Build headers
4747
headers = {'Accept': 'application/json, */*'}
@@ -161,7 +161,7 @@ def call(
161161
log_context.method.set(method)
162162
log_context.url.set(url)
163163

164-
self.stats.calls += 1
164+
self._statistics.calls += 1
165165

166166
headers, params, content = self._prepare_request_call(headers, params, data, json)
167167

@@ -171,11 +171,13 @@ def _make_request(stop_retrying: Callable, attempt: int) -> impit.Response:
171171
log_context.attempt.set(attempt)
172172
logger.debug('Sending request')
173173

174-
self.stats.requests += 1
174+
self._statistics.requests += 1
175175

176176
try:
177177
# Increase timeout with each attempt. Max timeout is bounded by the client timeout.
178-
timeout = min(self.config.timeout_secs, (timeout_secs or self.config.timeout_secs) * 2 ** (attempt - 1))
178+
timeout = min(
179+
self._config.timeout_secs, (timeout_secs or self._config.timeout_secs) * 2 ** (attempt - 1)
180+
)
179181

180182
url_with_params = self._build_url_with_params(url, params)
181183

@@ -195,7 +197,7 @@ def _make_request(stop_retrying: Callable, attempt: int) -> impit.Response:
195197
return response
196198

197199
if response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
198-
self.stats.add_rate_limit_error(attempt)
200+
self._statistics.add_rate_limit_error(attempt)
199201

200202
except Exception as exc:
201203
logger.debug('Request threw exception', exc_info=exc)
@@ -217,8 +219,8 @@ def _make_request(stop_retrying: Callable, attempt: int) -> impit.Response:
217219

218220
return retry_with_exp_backoff(
219221
_make_request,
220-
max_retries=self.config.max_retries,
221-
backoff_base_millis=self.config.min_delay_between_retries_millis,
222+
max_retries=self._config.max_retries,
223+
backoff_base_millis=self._config.min_delay_between_retries_millis,
222224
backoff_factor=DEFAULT_BACKOFF_EXPONENTIAL_FACTOR,
223225
random_factor=DEFAULT_BACKOFF_RANDOM_FACTOR,
224226
)
@@ -240,7 +242,7 @@ async def call(
240242
log_context.method.set(method)
241243
log_context.url.set(url)
242244

243-
self.stats.calls += 1
245+
self._statistics.calls += 1
244246

245247
headers, params, content = self._prepare_request_call(headers, params, data, json)
246248

@@ -251,7 +253,9 @@ async def _make_request(stop_retrying: Callable, attempt: int) -> impit.Response
251253
logger.debug('Sending request')
252254
try:
253255
# Increase timeout with each attempt. Max timeout is bounded by the client timeout.
254-
timeout = min(self.config.timeout_secs, (timeout_secs or self.config.timeout_secs) * 2 ** (attempt - 1))
256+
timeout = min(
257+
self._config.timeout_secs, (timeout_secs or self._config.timeout_secs) * 2 ** (attempt - 1)
258+
)
255259

256260
url_with_params = self._build_url_with_params(url, params)
257261

@@ -271,7 +275,7 @@ async def _make_request(stop_retrying: Callable, attempt: int) -> impit.Response
271275
return response
272276

273277
if response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
274-
self.stats.add_rate_limit_error(attempt)
278+
self._statistics.add_rate_limit_error(attempt)
275279

276280
except Exception as exc:
277281
logger.debug('Request threw exception', exc_info=exc)
@@ -293,8 +297,8 @@ async def _make_request(stop_retrying: Callable, attempt: int) -> impit.Response
293297

294298
return await retry_with_exp_backoff_async(
295299
_make_request,
296-
max_retries=self.config.max_retries,
297-
backoff_base_millis=self.config.min_delay_between_retries_millis,
300+
max_retries=self._config.max_retries,
301+
backoff_base_millis=self._config.min_delay_between_retries_millis,
298302
backoff_factor=DEFAULT_BACKOFF_EXPONENTIAL_FACTOR,
299303
random_factor=DEFAULT_BACKOFF_RANDOM_FACTOR,
300304
)

src/apify_client/_logging.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _injects_client_details_to_log_context(fun: Callable) -> Callable:
115115
@functools.wraps(fun)
116116
async def async_wrapper(resource_client: _BaseClient, *args: Any, **kwargs: Any) -> Any:
117117
log_context.client_method.set(fun.__qualname__) # ty: ignore[unresolved-attribute]
118-
log_context.resource_id.set(resource_client.resource_id)
118+
log_context.resource_id.set(resource_client._resource_id) # noqa: SLF001
119119

120120
return await fun(resource_client, *args, **kwargs)
121121

@@ -126,7 +126,7 @@ async def async_wrapper(resource_client: _BaseClient, *args: Any, **kwargs: Any)
126126
@functools.wraps(fun)
127127
async def async_generator_wrapper(resource_client: _BaseClient, *args: Any, **kwargs: Any) -> Any:
128128
log_context.client_method.set(fun.__qualname__) # ty: ignore[unresolved-attribute]
129-
log_context.resource_id.set(resource_client.resource_id)
129+
log_context.resource_id.set(resource_client._resource_id) # noqa: SLF001
130130

131131
async for item in fun(resource_client, *args, **kwargs):
132132
yield item
@@ -136,7 +136,7 @@ async def async_generator_wrapper(resource_client: _BaseClient, *args: Any, **kw
136136
@functools.wraps(fun)
137137
def wrapper(resource_client: _BaseClient, *args: Any, **kwargs: Any) -> Any:
138138
log_context.client_method.set(fun.__qualname__) # ty: ignore[unresolved-attribute]
139-
log_context.resource_id.set(resource_client.resource_id)
139+
log_context.resource_id.set(resource_client._resource_id) # noqa: SLF001
140140

141141
return fun(resource_client, *args, **kwargs)
142142

src/apify_client/_resource_clients/_resource_client.py

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,18 @@ def __init__(
6161
self._default_params = params or {}
6262
self._resource_path = resource_path
6363
self._resource_id = resource_id
64-
self._resource_url = f'{self._base_url}/{self._resource_path}'
65-
if self._resource_id is not None:
66-
self._safe_id = to_safe_id(self._resource_id)
67-
self._resource_url = f'{self._resource_url}/{self._safe_id}'
68-
69-
@property
70-
def http_client(self) -> HttpClient:
71-
"""HTTP client for making API requests."""
72-
return self._http_client
7364

7465
@property
75-
def url(self) -> str:
76-
"""Full URL of this resource."""
77-
return self._resource_url
66+
def _resource_url(self) -> str:
67+
resource_url = f'{self._base_url}/{self._resource_path}'
7868

79-
@property
80-
def params(self) -> dict:
81-
"""Default parameters for requests."""
82-
return self._default_params
69+
if self._resource_id is not None:
70+
safe_id = to_safe_id(self._resource_id)
71+
resource_url = f'{resource_url}/{safe_id}'
8372

84-
@property
85-
def resource_id(self) -> str | None:
86-
"""ID of the manipulated resource."""
87-
return self._resource_id
73+
return resource_url
8874

89-
def _url(
75+
def _build_url(
9076
self,
9177
path: str | None = None,
9278
*,
@@ -251,32 +237,18 @@ def __init__(
251237
self._default_params = params or {}
252238
self._resource_path = resource_path
253239
self._resource_id = resource_id
254-
self._resource_url = f'{self._base_url}/{self._resource_path}'
255-
if self._resource_id is not None:
256-
self._safe_id = to_safe_id(self._resource_id)
257-
self._resource_url = f'{self._resource_url}/{self._safe_id}'
258-
259-
@property
260-
def http_client(self) -> HttpClientAsync:
261-
"""HTTP client for making API requests."""
262-
return self._http_client
263240

264241
@property
265-
def url(self) -> str:
266-
"""Full URL of this resource."""
267-
return self._resource_url
242+
def _resource_url(self) -> str:
243+
resource_url = f'{self._base_url}/{self._resource_path}'
268244

269-
@property
270-
def params(self) -> dict:
271-
"""Default parameters for requests."""
272-
return self._default_params
245+
if self._resource_id is not None:
246+
safe_id = to_safe_id(self._resource_id)
247+
resource_url = f'{resource_url}/{safe_id}'
273248

274-
@property
275-
def resource_id(self) -> str | None:
276-
"""ID of the manipulated resource."""
277-
return self._resource_id
249+
return resource_url
278250

279-
def _url(
251+
def _build_url(
280252
self,
281253
path: str | None = None,
282254
*,

0 commit comments

Comments
 (0)