-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path_resource_client.py
More file actions
531 lines (456 loc) · 19.4 KB
/
Copy path_resource_client.py
File metadata and controls
531 lines (456 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
from __future__ import annotations
import asyncio
import time
from datetime import UTC, datetime, timedelta
from functools import cached_property
from typing import TYPE_CHECKING, Any
from apify_client._consts import DEFAULT_WAIT_FOR_FINISH, DEFAULT_WAIT_WHEN_JOB_NOT_EXIST, TERMINAL_STATUSES
from apify_client._docs import docs_group
from apify_client._logging import WithLogDetailsClient
from apify_client._types import ActorJobResponse
from apify_client._utils import catch_not_found_or_throw, response_to_dict, to_safe_id, to_seconds
from apify_client.errors import ApifyApiError
if TYPE_CHECKING:
from apify_client._client_registry import ClientRegistry, ClientRegistryAsync
from apify_client._http_clients import HttpClient, HttpClientAsync
from apify_client._types import Timeout
class ResourceClientBase(metaclass=WithLogDetailsClient):
"""Base class with shared implementation for sync and async resource clients.
Provides URL building, parameter handling, and client creation utilities.
"""
def __init__(
self,
*,
base_url: str,
public_base_url: str,
http_client: Any,
resource_path: str,
client_registry: Any,
resource_id: str | None = None,
params: dict | None = None,
) -> None:
"""Initialize the resource client.
Args:
base_url: API base URL.
public_base_url: Public CDN base URL.
http_client: HTTP client for making requests.
resource_path: Resource endpoint path (e.g., 'actors', 'datasets').
client_registry: Bundle of client classes for dependency injection.
resource_id: Optional resource ID for single-resource clients.
params: Optional default parameters for all requests.
"""
if resource_path.endswith('/'):
raise ValueError('resource_path must not end with "/"')
self._base_url = base_url
self._public_base_url = public_base_url
self._http_client = http_client
self._default_params = params or {}
self._resource_path = resource_path
self._resource_id = resource_id
self._client_registry = client_registry
@property
def resource_id(self) -> str | None:
"""Get the resource ID."""
return self._resource_id
@property
def _resource_url(self) -> str:
"""Build the full resource URL from base URL, path, and optional ID."""
url = f'{self._base_url}/{self._resource_path}'
if self._resource_id is not None:
url = f'{url}/{to_safe_id(self._resource_id)}'
return url
@cached_property
def _base_client_kwargs(self) -> dict[str, Any]:
"""Base kwargs for creating nested/child clients.
Returns dict with base_url, public_base_url, http_client, and client_registry. Caller adds
resource_path, resource_id, and params as needed.
"""
return {
'base_url': self._resource_url,
'public_base_url': self._public_base_url,
'http_client': self._http_client,
'client_registry': self._client_registry,
}
def _build_url(
self,
path: str | None = None,
*,
public: bool = False,
) -> str:
"""Build complete URL for API request.
Args:
path: Optional path segment to append (e.g., 'runs', 'items').
public: Whether to use public CDN URL instead of API URL.
Returns:
Complete URL with optional path and query string.
"""
url = f'{self._resource_url}/{path}' if path is not None else self._resource_url
if public:
if not url.startswith(self._base_url):
raise ValueError(f'URL {url} does not start with base URL {self._base_url}')
url = url.replace(self._base_url, self._public_base_url, 1)
return url
def _build_params(self, **kwargs: Any) -> dict:
"""Merge default params with method params, filtering out None values.
Args:
**kwargs: Method-specific parameters to merge.
Returns:
Merged parameters with None values removed.
"""
merged = {**self._default_params, **kwargs}
return {k: v for k, v in merged.items() if v is not None}
@staticmethod
def _clean_json_payload(data: dict) -> dict:
"""Remove None values and empty nested dicts from an API request payload.
The Apify API ignores missing fields but may reject fields explicitly set to None.
Nested sub-models serialized by Pydantic may produce empty dicts when all their
fields are None — these are also removed.
Uses an iterative stack-based approach, analogous to _build_params for query params.
"""
result: dict = {}
stack: list[tuple[dict, dict]] = [(data, result)]
while stack:
source, target = stack.pop()
for key, val in source.items():
if val is None:
continue
if isinstance(val, dict):
nested: dict = {}
target[key] = nested
stack.append((val, nested))
else:
target[key] = val
# Remove dicts that became empty after None filtering
def _remove_empty(d: dict) -> None:
for key in [k for k, v in d.items() if isinstance(v, dict)]:
_remove_empty(d[key])
if not d[key]:
del d[key]
_remove_empty(result)
return result
@docs_group('Resource clients')
class ResourceClient(ResourceClientBase):
"""Base class for synchronous resource clients."""
def __init__(
self,
*,
base_url: str,
public_base_url: str,
http_client: HttpClient,
resource_path: str,
client_registry: ClientRegistry,
resource_id: str | None = None,
params: dict | None = None,
) -> None:
"""Initialize the resource client.
Args:
base_url: API base URL.
public_base_url: Public CDN base URL.
http_client: HTTP client for making requests.
resource_path: Resource endpoint path (e.g., 'actors', 'datasets').
client_registry: Bundle of client classes for dependency injection.
resource_id: Optional resource ID for single-resource clients.
params: Optional default parameters for all requests.
"""
super().__init__(
base_url=base_url,
public_base_url=public_base_url,
http_client=http_client,
resource_path=resource_path,
client_registry=client_registry,
resource_id=resource_id,
params=params,
)
def _get(self, *, timeout: Timeout) -> dict | None:
"""Perform a GET request for this resource, returning the parsed response or None if not found.
404s collapse to `None` only when this client targets a specific resource by ID. For chained clients
without a `resource_id` (e.g. `run.dataset()`), a 404 could mean either the parent or the default
sub-resource is missing and the API body cannot disambiguate, so `NotFoundError` propagates to the caller.
"""
try:
response = self._http_client.call(
url=self._build_url(),
method='GET',
params=self._build_params(),
timeout=timeout,
)
return response_to_dict(response)
except ApifyApiError as exc:
if self._resource_id is None:
raise
catch_not_found_or_throw(exc)
return None
def _update(self, *, timeout: Timeout, **kwargs: Any) -> dict:
"""Perform a PUT request to update this resource with the given fields."""
response = self._http_client.call(
url=self._build_url(),
method='PUT',
params=self._build_params(),
json=self._clean_json_payload(kwargs),
timeout=timeout,
)
return response_to_dict(response)
def _delete(self, *, timeout: Timeout) -> None:
"""Perform a DELETE request to delete this resource, ignoring 404 errors."""
try:
self._http_client.call(
url=self._build_url(),
method='DELETE',
params=self._build_params(),
timeout=timeout,
)
except ApifyApiError as exc:
catch_not_found_or_throw(exc)
def _list(self, *, timeout: Timeout, **kwargs: Any) -> dict:
"""Perform a GET request to list resources."""
response = self._http_client.call(
url=self._build_url(),
method='GET',
params=self._build_params(**kwargs),
timeout=timeout,
)
return response_to_dict(response)
def _create(self, *, timeout: Timeout, **kwargs: Any) -> dict:
"""Perform a POST request to create a resource."""
response = self._http_client.call(
url=self._build_url(),
method='POST',
params=self._build_params(),
json=self._clean_json_payload(kwargs),
timeout=timeout,
)
return response_to_dict(response)
def _get_or_create(
self,
*,
name: str | None = None,
resource_fields: dict | None = None,
timeout: Timeout,
) -> dict:
"""Perform a POST request to get or create a named resource."""
response = self._http_client.call(
url=self._build_url(),
method='POST',
params=self._build_params(name=name),
json=self._clean_json_payload(resource_fields) if resource_fields is not None else None,
timeout=timeout,
)
return response_to_dict(response)
def _wait_for_finish(
self,
*,
url: str,
params: dict,
timeout: Timeout,
wait_duration: timedelta | None = None,
) -> dict | None:
"""Wait synchronously for an Actor job (run or build) to finish.
Polls the job status until it reaches a terminal state or timeout.
Handles 404 errors gracefully (job might not exist yet in replicas).
Args:
url: Full URL to the job endpoint.
params: Base query parameters to include in each request.
timeout: Timeout for each individual HTTP request.
wait_duration: Maximum time to wait (None = indefinite).
Returns:
Job data dict when finished, or None if job doesn't exist after
DEFAULT_WAIT_WHEN_JOB_NOT_EXIST seconds.
Raises:
ApifyApiError: If API returns errors other than 404.
"""
now = datetime.now(UTC)
deadline = (now + wait_duration) if wait_duration is not None else None
not_found_deadline = now + DEFAULT_WAIT_WHEN_JOB_NOT_EXIST
actor_job: dict = {}
while True:
if deadline is not None:
remaining_secs = max(0, int(to_seconds(deadline - datetime.now(UTC))))
wait_for_finish = remaining_secs
else:
wait_for_finish = to_seconds(DEFAULT_WAIT_FOR_FINISH, as_int=True)
try:
response = self._http_client.call(
url=url,
method='GET',
params={**params, 'waitForFinish': wait_for_finish},
timeout=timeout,
)
result = response_to_dict(response)
actor_job_response = ActorJobResponse.model_validate(result)
actor_job = actor_job_response.data.model_dump()
is_terminal = actor_job_response.data.status in TERMINAL_STATUSES
is_timed_out = deadline is not None and datetime.now(UTC) >= deadline
if is_terminal or is_timed_out:
break
except ApifyApiError as exc:
catch_not_found_or_throw(exc)
# If there are still not found errors after DEFAULT_WAIT_WHEN_JOB_NOT_EXIST, we give up
# and return None. In such case, the requested record probably really doesn't exist.
if datetime.now(UTC) > not_found_deadline:
return None
# It might take some time for database replicas to get up-to-date so sleep a bit before retrying
time.sleep(0.25)
return actor_job
@docs_group('Resource clients')
class ResourceClientAsync(ResourceClientBase):
"""Base class for asynchronous resource clients."""
def __init__(
self,
*,
base_url: str,
public_base_url: str,
http_client: HttpClientAsync,
resource_path: str,
client_registry: ClientRegistryAsync,
resource_id: str | None = None,
params: dict | None = None,
) -> None:
"""Initialize the resource client.
Args:
base_url: API base URL.
public_base_url: Public CDN base URL.
http_client: HTTP client for making requests.
resource_path: Resource endpoint path (e.g., 'actors', 'datasets').
client_registry: Bundle of client classes for dependency injection.
resource_id: Optional resource ID for single-resource clients.
params: Optional default parameters for all requests.
"""
super().__init__(
base_url=base_url,
public_base_url=public_base_url,
http_client=http_client,
resource_path=resource_path,
client_registry=client_registry,
resource_id=resource_id,
params=params,
)
async def _get(self, *, timeout: Timeout) -> dict | None:
"""Perform a GET request for this resource, returning the parsed response or None if not found.
404s collapse to `None` only when this client targets a specific resource by ID. For chained clients
without a `resource_id` (e.g. `run.dataset()`), a 404 could mean either the parent or the default
sub-resource is missing and the API body cannot disambiguate, so `NotFoundError` propagates to the caller.
"""
try:
response = await self._http_client.call(
url=self._build_url(),
method='GET',
params=self._build_params(),
timeout=timeout,
)
return response_to_dict(response)
except ApifyApiError as exc:
if self._resource_id is None:
raise
catch_not_found_or_throw(exc)
return None
async def _update(self, *, timeout: Timeout, **kwargs: Any) -> dict:
"""Perform a PUT request to update this resource with the given fields."""
response = await self._http_client.call(
url=self._build_url(),
method='PUT',
params=self._build_params(),
json=self._clean_json_payload(kwargs),
timeout=timeout,
)
return response_to_dict(response)
async def _delete(self, *, timeout: Timeout) -> None:
"""Perform a DELETE request to delete this resource, ignoring 404 errors."""
try:
await self._http_client.call(
url=self._build_url(),
method='DELETE',
params=self._build_params(),
timeout=timeout,
)
except ApifyApiError as exc:
catch_not_found_or_throw(exc)
async def _list(self, *, timeout: Timeout, **kwargs: Any) -> dict:
"""Perform a GET request to list resources."""
response = await self._http_client.call(
url=self._build_url(),
method='GET',
params=self._build_params(**kwargs),
timeout=timeout,
)
return response_to_dict(response)
async def _create(self, *, timeout: Timeout, **kwargs: Any) -> dict:
"""Perform a POST request to create a resource."""
response = await self._http_client.call(
url=self._build_url(),
method='POST',
params=self._build_params(),
json=self._clean_json_payload(kwargs),
timeout=timeout,
)
return response_to_dict(response)
async def _get_or_create(
self,
*,
name: str | None = None,
resource_fields: dict | None = None,
timeout: Timeout,
) -> dict:
"""Perform a POST request to get or create a named resource."""
response = await self._http_client.call(
url=self._build_url(),
method='POST',
params=self._build_params(name=name),
json=self._clean_json_payload(resource_fields) if resource_fields is not None else None,
timeout=timeout,
)
return response_to_dict(response)
async def _wait_for_finish(
self,
*,
url: str,
params: dict,
timeout: Timeout,
wait_duration: timedelta | None = None,
) -> dict | None:
"""Wait asynchronously for an Actor job (run or build) to finish.
Polls the job status until it reaches a terminal state or timeout.
Handles 404 errors gracefully (job might not exist yet in replicas).
Args:
url: Full URL to the job endpoint.
params: Base query parameters to include in each request.
timeout: Timeout for each individual HTTP request.
wait_duration: Maximum time to wait (None = indefinite).
Returns:
Job data dict when finished, or None if job doesn't exist after
DEFAULT_WAIT_WHEN_JOB_NOT_EXIST seconds.
Raises:
ApifyApiError: If API returns errors other than 404.
"""
now = datetime.now(UTC)
deadline = (now + wait_duration) if wait_duration is not None else None
not_found_deadline = now + DEFAULT_WAIT_WHEN_JOB_NOT_EXIST
actor_job: dict = {}
while True:
if deadline is not None:
remaining_secs = max(0, int(to_seconds(deadline - datetime.now(UTC))))
wait_for_finish = remaining_secs
else:
wait_for_finish = to_seconds(DEFAULT_WAIT_FOR_FINISH, as_int=True)
try:
response = await self._http_client.call(
url=url,
method='GET',
params={**params, 'waitForFinish': wait_for_finish},
timeout=timeout,
)
result = response_to_dict(response)
actor_job_response = ActorJobResponse.model_validate(result)
actor_job = actor_job_response.data.model_dump()
is_terminal = actor_job_response.data.status in TERMINAL_STATUSES
is_timed_out = deadline is not None and datetime.now(UTC) >= deadline
if is_terminal or is_timed_out:
break
except ApifyApiError as exc:
catch_not_found_or_throw(exc)
# If there are still not found errors after DEFAULT_WAIT_WHEN_JOB_NOT_EXIST, we give up
# and return None. In such case, the requested record probably really doesn't exist.
if datetime.now(UTC) > not_found_deadline:
return None
# It might take some time for database replicas to get up-to-date so sleep a bit before retrying
await asyncio.sleep(0.25)
return actor_job