-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathuser.py
More file actions
268 lines (218 loc) · 8.81 KB
/
user.py
File metadata and controls
268 lines (218 loc) · 8.81 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
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from pydantic import ValidationError
from apify_client._docs import docs_group
from apify_client._models import (
AccountLimits,
LimitsResponse,
MonthlyUsage,
MonthlyUsageResponse,
PrivateUserDataResponse,
PublicUserDataResponse,
UserPrivateInfo,
UserPublicInfo,
)
from apify_client._resource_clients._resource_client import ResourceClient, ResourceClientAsync
from apify_client._utils import response_to_dict
if TYPE_CHECKING:
from apify_client.types import Timeout
@docs_group('Resource clients')
class UserClient(ResourceClient):
"""Sub-client for managing user account information.
Provides methods to manage user account information, e.g. get user data or monthly usage. Obtain an instance via
an appropriate method on the `ApifyClient` class.
"""
def __init__(
self,
*,
resource_id: str | None = None,
resource_path: str = 'users',
**kwargs: Any,
) -> None:
super().__init__(
resource_id=resource_id or 'me',
resource_path=resource_path,
**kwargs,
)
def get(self, *, timeout: Timeout = 'short') -> UserPublicInfo | UserPrivateInfo | None:
"""Return information about user account.
You receive all or only public info based on your token permissions.
https://docs.apify.com/api/v2#/reference/users
Args:
timeout: Timeout for the API HTTP request.
Returns:
The retrieved user data, or None if the user does not exist.
"""
result = self._get(timeout=timeout)
if result is None:
return None
try:
return PrivateUserDataResponse.model_validate(result).data
except ValidationError:
return PublicUserDataResponse.model_validate(result).data
def monthly_usage(self, *, timeout: Timeout = 'short') -> MonthlyUsage:
"""Return monthly usage of the user account.
This includes a complete usage summary for the current usage cycle, an overall sum, as well as a daily breakdown
of usage. It is the same information which is available on the account's Billing page. The information includes
use of storage, data transfer, and request queue usage.
https://docs.apify.com/api/v2/#/reference/users/monthly-usage
Args:
timeout: Timeout for the API HTTP request.
Returns:
The retrieved monthly usage.
Raises:
NotFoundError: If the user does not exist.
"""
response = self._http_client.call(
url=self._build_url('usage/monthly'),
method='GET',
params=self._build_params(),
timeout=timeout,
)
result = response_to_dict(response)
return MonthlyUsageResponse.model_validate(result).data
def limits(self, *, timeout: Timeout = 'short') -> AccountLimits:
"""Return a complete summary of the user account's limits.
It is the same information which is available on the account's Limits page. The returned data includes
the current usage cycle, a summary of the account's limits, and the current usage.
https://docs.apify.com/api/v2#/reference/users/account-limits/get-account-limits
Args:
timeout: Timeout for the API HTTP request.
Returns:
The account limits.
Raises:
NotFoundError: If the user does not exist.
"""
response = self._http_client.call(
url=self._build_url('limits'),
method='GET',
params=self._build_params(),
timeout=timeout,
)
result = response_to_dict(response)
return LimitsResponse.model_validate(result).data
def update_limits(
self,
*,
max_monthly_usage_usd: int | None = None,
data_retention_days: int | None = None,
timeout: Timeout = 'short',
) -> None:
"""Update the account's limits manageable on your account's Limits page.
Args:
max_monthly_usage_usd: Maximum monthly usage in USD.
data_retention_days: Data retention period in days.
timeout: Timeout for the API HTTP request.
"""
self._http_client.call(
url=self._build_url('limits'),
method='PUT',
params=self._build_params(),
json=self._clean_json_payload(
{
'maxMonthlyUsageUsd': max_monthly_usage_usd,
'dataRetentionDays': data_retention_days,
}
),
timeout=timeout,
)
@docs_group('Resource clients')
class UserClientAsync(ResourceClientAsync):
"""Sub-client for managing user account information.
Provides methods to manage user account information, e.g. get user data or monthly usage. Obtain an instance via
an appropriate method on the `ApifyClientAsync` class.
"""
def __init__(
self,
*,
resource_id: str | None = None,
resource_path: str = 'users',
**kwargs: Any,
) -> None:
super().__init__(
resource_id=resource_id or 'me',
resource_path=resource_path,
**kwargs,
)
async def get(self, *, timeout: Timeout = 'short') -> UserPublicInfo | UserPrivateInfo | None:
"""Return information about user account.
You receive all or only public info based on your token permissions.
https://docs.apify.com/api/v2#/reference/users
Args:
timeout: Timeout for the API HTTP request.
Returns:
The retrieved user data, or None if the user does not exist.
"""
result = await self._get(timeout=timeout)
if result is None:
return None
try:
return PrivateUserDataResponse.model_validate(result).data
except ValidationError:
return PublicUserDataResponse.model_validate(result).data
async def monthly_usage(self, *, timeout: Timeout = 'short') -> MonthlyUsage:
"""Return monthly usage of the user account.
This includes a complete usage summary for the current usage cycle, an overall sum, as well as a daily breakdown
of usage. It is the same information which is available on the account's Billing page. The information includes
use of storage, data transfer, and request queue usage.
https://docs.apify.com/api/v2/#/reference/users/monthly-usage
Args:
timeout: Timeout for the API HTTP request.
Returns:
The retrieved monthly usage.
Raises:
NotFoundError: If the user does not exist.
"""
response = await self._http_client.call(
url=self._build_url('usage/monthly'),
method='GET',
params=self._build_params(),
timeout=timeout,
)
result = response_to_dict(response)
return MonthlyUsageResponse.model_validate(result).data
async def limits(self, *, timeout: Timeout = 'short') -> AccountLimits:
"""Return a complete summary of the user account's limits.
It is the same information which is available on the account's Limits page. The returned data includes
the current usage cycle, a summary of the account's limits, and the current usage.
https://docs.apify.com/api/v2#/reference/users/account-limits/get-account-limits
Args:
timeout: Timeout for the API HTTP request.
Returns:
The account limits.
Raises:
NotFoundError: If the user does not exist.
"""
response = await self._http_client.call(
url=self._build_url('limits'),
method='GET',
params=self._build_params(),
timeout=timeout,
)
result = response_to_dict(response)
return LimitsResponse.model_validate(result).data
async def update_limits(
self,
*,
max_monthly_usage_usd: int | None = None,
data_retention_days: int | None = None,
timeout: Timeout = 'short',
) -> None:
"""Update the account's limits manageable on your account's Limits page.
Args:
max_monthly_usage_usd: Maximum monthly usage in USD.
data_retention_days: Data retention period in days.
timeout: Timeout for the API HTTP request.
"""
await self._http_client.call(
url=self._build_url('limits'),
method='PUT',
params=self._build_params(),
json=self._clean_json_payload(
{
'maxMonthlyUsageUsd': max_monthly_usage_usd,
'dataRetentionDays': data_retention_days,
}
),
timeout=timeout,
)