Skip to content

Commit 93d0afe

Browse files
authored
feat: add monthlyUsage() and limits() methods to UserClient (#183)
These endpoints have been around for some quite time and now we're exposing them via the Python client.
1 parent 4c9c72e commit 93d0afe

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## [1.6.4](../../releases/tag/v1.6.4) - Not released yet
44

5-
...
5+
### Added
6+
7+
- added `monthlyUsage()` and `limits()` methods to `UserClient`
68

79
## [1.6.3](../../releases/tag/v1.6.3) - 2023-01-16
810

src/apify_client/clients/resource_clients/user.py

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
from typing import Any
44

5-
from apify_shared.utils import ignore_docs
5+
from apify_shared.utils import ignore_docs, parse_date_fields
66

7+
from apify_client._errors import ApifyApiError
8+
from apify_client._utils import catch_not_found_or_throw, pluck_data
79
from apify_client.clients.base import ResourceClient, ResourceClientAsync
810

911

@@ -27,10 +29,59 @@ def get(self: UserClient) -> dict | None:
2729
https://docs.apify.com/api/v2#/reference/users
2830
2931
Returns:
30-
dict, optional: The retrieved user data, or None if the user does not exist.
32+
The retrieved user data, or None if the user does not exist.
3133
"""
3234
return self._get()
3335

36+
def monthly_usage(self: UserClient) -> dict | None:
37+
"""Return monthly usage of the user account.
38+
39+
This includes a complete usage summary for the current usage cycle, an overall sum, as well as a daily breakdown
40+
of usage. It is the same information which is available on the account's Billing page. The information includes
41+
use of storage, data transfer, and request queue usage.
42+
43+
https://docs.apify.com/api/v2/#/reference/users/monthly-usage
44+
45+
Returns:
46+
The retrieved request, or None, if it did not exist.
47+
"""
48+
try:
49+
response = self.http_client.call(
50+
url=self._url('usage/monthly'),
51+
method='GET',
52+
params=self._params(),
53+
)
54+
return parse_date_fields(pluck_data(response.json()))
55+
56+
except ApifyApiError as exc:
57+
catch_not_found_or_throw(exc)
58+
59+
return None
60+
61+
def limits(self: UserClient) -> dict | None:
62+
"""Returns a complete summary of the user account's limits.
63+
64+
It is the same information which is available on the account's Limits page. The returned data includes the current
65+
usage cycle, a summary of the account's limits, and the current usage.
66+
67+
https://docs.apify.com/api/v2#/reference/request-queues/request/get-request
68+
69+
Returns:
70+
The retrieved request, or None, if it did not exist.
71+
"""
72+
try:
73+
response = self.http_client.call(
74+
url=self._url('limits'),
75+
method='GET',
76+
params=self._params(),
77+
)
78+
return parse_date_fields(pluck_data(response.json()))
79+
80+
except ApifyApiError as exc:
81+
catch_not_found_or_throw(exc)
82+
83+
return None
84+
3485

3586
class UserClientAsync(ResourceClientAsync):
3687
"""Async sub-client for querying user data."""
@@ -52,6 +103,55 @@ async def get(self: UserClientAsync) -> dict | None:
52103
https://docs.apify.com/api/v2#/reference/users
53104
54105
Returns:
55-
dict, optional: The retrieved user data, or None if the user does not exist.
106+
The retrieved user data, or None if the user does not exist.
56107
"""
57108
return await self._get()
109+
110+
async def monthly_usage(self: UserClientAsync) -> dict | None:
111+
"""Return monthly usage of the user account.
112+
113+
This includes a complete usage summary for the current usage cycle, an overall sum, as well as a daily breakdown
114+
of usage. It is the same information which is available on the account's Billing page. The information includes
115+
use of storage, data transfer, and request queue usage.
116+
117+
https://docs.apify.com/api/v2/#/reference/users/monthly-usage
118+
119+
Returns:
120+
The retrieved request, or None, if it did not exist.
121+
"""
122+
try:
123+
response = await self.http_client.call(
124+
url=self._url('usage/monthly'),
125+
method='GET',
126+
params=self._params(),
127+
)
128+
return parse_date_fields(pluck_data(response.json()))
129+
130+
except ApifyApiError as exc:
131+
catch_not_found_or_throw(exc)
132+
133+
return None
134+
135+
async def limits(self: UserClientAsync) -> dict | None:
136+
"""Returns a complete summary of the user account's limits.
137+
138+
It is the same information which is available on the account's Limits page. The returned data includes the current
139+
usage cycle, a summary of the account's limits, and the current usage.
140+
141+
https://docs.apify.com/api/v2#/reference/request-queues/request/get-request
142+
143+
Returns:
144+
The retrieved request, or None, if it did not exist.
145+
"""
146+
try:
147+
response = await self.http_client.call(
148+
url=self._url('limits'),
149+
method='GET',
150+
params=self._params(),
151+
)
152+
return parse_date_fields(pluck_data(response.json()))
153+
154+
except ApifyApiError as exc:
155+
catch_not_found_or_throw(exc)
156+
157+
return None

0 commit comments

Comments
 (0)