-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathresource_collection_client.py
More file actions
82 lines (63 loc) · 2.3 KB
/
resource_collection_client.py
File metadata and controls
82 lines (63 loc) · 2.3 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
from __future__ import annotations
from typing import Any, TypeVar
from apify_client._utils import parse_date_fields, pluck_data
from apify_client.clients.base.base_client import (
BaseClient,
BaseClientAsync,
ListPage,
)
T = TypeVar('T')
class ResourceCollectionClient(BaseClient):
"""Base class for sub-clients manipulating a resource collection."""
def _list(self, **kwargs: Any) -> ListPage:
response = self.http_client.call(
url=self._url(),
method='GET',
params=self._params(**kwargs),
)
return ListPage(parse_date_fields(pluck_data(response.json())))
def _create(self, resource: dict) -> dict:
response = self.http_client.call(
url=self._url(),
method='POST',
params=self._params(),
json=resource,
)
return parse_date_fields(pluck_data(response.json()))
def _get_or_create(self, name: str | None = None, resource: dict | None = None) -> dict:
response = self.http_client.call(
url=self._url(),
method='POST',
params=self._params(name=name),
json=resource,
)
return parse_date_fields(pluck_data(response.json()))
class ResourceCollectionClientAsync(BaseClientAsync):
"""Base class for async sub-clients manipulating a resource collection."""
async def _list(self, **kwargs: Any) -> ListPage:
response = await self.http_client.call(
url=self._url(),
method='GET',
params=self._params(**kwargs),
)
return ListPage(parse_date_fields(pluck_data(response.json())))
async def _create(self, resource: dict) -> dict:
response = await self.http_client.call(
url=self._url(),
method='POST',
params=self._params(),
json=resource,
)
return parse_date_fields(pluck_data(response.json()))
async def _get_or_create(
self,
name: str | None = None,
resource: dict | None = None,
) -> dict:
response = await self.http_client.call(
url=self._url(),
method='POST',
params=self._params(name=name),
json=resource,
)
return parse_date_fields(pluck_data(response.json()))