Skip to content

Commit ef6b9e7

Browse files
committed
Utilize to_seconds helper
1 parent e793417 commit ef6b9e7

15 files changed

Lines changed: 79 additions & 65 deletions

File tree

src/apify_client/_http_clients/_async.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from apify_client._consts import DEFAULT_MAX_RETRIES, DEFAULT_MIN_DELAY_BETWEEN_RETRIES, DEFAULT_TIMEOUT
1313
from apify_client._http_clients._base import BaseHttpClient
1414
from apify_client._logging import log_context, logger_name
15+
from apify_client._utils import to_seconds
1516
from apify_client.errors import ApifyApiError
1617

1718
if TYPE_CHECKING:
@@ -57,7 +58,7 @@ def __init__(
5758
self._impit_async_client = impit.AsyncClient(
5859
headers=self._headers,
5960
follow_redirects=True,
60-
timeout=self._timeout.total_seconds(),
61+
timeout=to_seconds(self._timeout),
6162
)
6263

6364
async def call(
@@ -230,7 +231,7 @@ def stop_retrying() -> None:
230231
raise
231232

232233
random_sleep_factor = random.uniform(1, 1 + random_factor)
233-
backoff_base_secs = backoff_base.total_seconds()
234+
backoff_base_secs = to_seconds(backoff_base)
234235
backoff_exp_factor = backoff_factor ** (attempt - 1)
235236

236237
sleep_time_secs = random_sleep_factor * backoff_base_secs * backoff_exp_factor

src/apify_client/_http_clients/_base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from apify_client._consts import DEFAULT_MAX_RETRIES, DEFAULT_MIN_DELAY_BETWEEN_RETRIES, DEFAULT_TIMEOUT
1515
from apify_client._statistics import ClientStatistics
16+
from apify_client._utils import to_seconds
1617
from apify_client.errors import InvalidResponseBodyError
1718

1819
if TYPE_CHECKING:
@@ -149,6 +150,6 @@ def _build_url_with_params(self, url: str, params: dict[str, Any] | None = None)
149150

150151
def _calculate_timeout(self, attempt: int, timeout: timedelta | None = None) -> float:
151152
"""Calculate timeout for a request attempt with exponential increase, bounded by client timeout."""
152-
timeout_secs = (timeout or self._timeout).total_seconds()
153-
client_timeout_secs = self._timeout.total_seconds()
153+
timeout_secs = to_seconds(timeout or self._timeout)
154+
client_timeout_secs = to_seconds(self._timeout)
154155
return min(client_timeout_secs, timeout_secs * 2 ** (attempt - 1))

src/apify_client/_http_clients/_sync.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from apify_client._consts import DEFAULT_MAX_RETRIES, DEFAULT_MIN_DELAY_BETWEEN_RETRIES, DEFAULT_TIMEOUT
1313
from apify_client._http_clients._base import BaseHttpClient
1414
from apify_client._logging import log_context, logger_name
15+
from apify_client._utils import to_seconds
1516
from apify_client.errors import ApifyApiError
1617

1718
if TYPE_CHECKING:
@@ -57,7 +58,7 @@ def __init__(
5758
self._impit_client = impit.Client(
5859
headers=self._headers,
5960
follow_redirects=True,
60-
timeout=self._timeout.total_seconds(),
61+
timeout=to_seconds(self._timeout),
6162
)
6263

6364
def call(
@@ -230,7 +231,7 @@ def stop_retrying() -> None:
230231
raise
231232

232233
random_sleep_factor = random.uniform(1, 1 + random_factor)
233-
backoff_base_secs = backoff_base.total_seconds()
234+
backoff_base_secs = to_seconds(backoff_base)
234235
backoff_exp_factor = backoff_factor ** (attempt - 1)
235236

236237
sleep_time_secs = random_sleep_factor * backoff_base_secs * backoff_exp_factor

src/apify_client/_representations.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
from typing import TYPE_CHECKING, Any
1010

11-
from typing_extensions import overload
12-
13-
from apify_client._utils import enum_to_value
11+
from apify_client._utils import enum_to_value, to_seconds
1412

1513
if TYPE_CHECKING:
1614
from datetime import timedelta
@@ -19,24 +17,6 @@
1917
from apify_client._models import ActorPermissionLevel, VersionSourceType
2018

2119

22-
@overload
23-
def to_seconds(td: timedelta) -> int: ...
24-
@overload
25-
def to_seconds(td: None) -> None: ...
26-
27-
28-
def to_seconds(td: timedelta | None) -> int | None:
29-
"""Convert timedelta to seconds.
30-
31-
Args:
32-
td: The timedelta to convert, or None.
33-
34-
Returns:
35-
The total seconds as an integer, or None if input is None.
36-
"""
37-
return int(td.total_seconds()) if td is not None else None
38-
39-
4020
def build_actor_standby_dict(
4121
*,
4222
is_enabled: bool | None = None,

src/apify_client/_resource_clients/_resource_client.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
from apify_client._consts import DEFAULT_WAIT_FOR_FINISH, DEFAULT_WAIT_WHEN_JOB_NOT_EXIST, ActorJobStatus
1111
from apify_client._logging import WithLogDetailsClient
12-
from apify_client._representations import to_seconds
13-
from apify_client._utils import catch_not_found_or_throw, response_to_dict, to_safe_id
12+
from apify_client._utils import catch_not_found_or_throw, response_to_dict, to_safe_id, to_seconds
1413
from apify_client.errors import ApifyApiError, ApifyClientError
1514

1615
if TYPE_CHECKING:
@@ -171,7 +170,7 @@ def _wait_for_finish(
171170
)
172171
job_response = response_to_dict(response)
173172
job = job_response.get('data') if isinstance(job_response, dict) else job_response
174-
seconds_elapsed = (datetime.now(timezone.utc) - started_at).total_seconds()
173+
seconds_elapsed = to_seconds(datetime.now(timezone.utc) - started_at)
175174

176175
if not isinstance(job, dict):
177176
raise ApifyClientError(
@@ -193,7 +192,7 @@ def _wait_for_finish(
193192

194193
# If there are still not found errors after DEFAULT_WAIT_WHEN_JOB_NOT_EXIST, we give up
195194
# and return None. In such case, the requested record probably really doesn't exist.
196-
if seconds_elapsed > DEFAULT_WAIT_WHEN_JOB_NOT_EXIST.total_seconds():
195+
if seconds_elapsed > to_seconds(DEFAULT_WAIT_WHEN_JOB_NOT_EXIST):
197196
return None
198197

199198
# It might take some time for database replicas to get up-to-date so sleep a bit before retrying
@@ -340,10 +339,10 @@ async def _wait_for_finish(
340339
should_repeat = True
341340
job: dict | None = None
342341
seconds_elapsed = 0.0
343-
wait_secs = wait_duration.total_seconds() if wait_duration is not None else None
342+
wait_secs = to_seconds(wait_duration)
344343

345344
while should_repeat:
346-
wait_for_finish = int(DEFAULT_WAIT_FOR_FINISH.total_seconds())
345+
wait_for_finish = to_seconds(DEFAULT_WAIT_FOR_FINISH)
347346
if wait_secs is not None:
348347
wait_for_finish = int(wait_secs - seconds_elapsed)
349348

@@ -362,7 +361,7 @@ async def _wait_for_finish(
362361
f'Expected dict with "status" field, got: {type(job).__name__}'
363362
)
364363

365-
seconds_elapsed = (datetime.now(timezone.utc) - started_at).total_seconds()
364+
seconds_elapsed = to_seconds(datetime.now(timezone.utc) - started_at)
366365
is_terminal = ActorJobStatus(job['status']).is_terminal
367366
is_timed_out = wait_secs is not None and seconds_elapsed >= wait_secs
368367
if is_terminal or is_timed_out:
@@ -377,7 +376,7 @@ async def _wait_for_finish(
377376

378377
# If there are still not found errors after DEFAULT_WAIT_WHEN_JOB_NOT_EXIST, we give up
379378
# and return None. In such case, the requested record probably really doesn't exist.
380-
if seconds_elapsed > DEFAULT_WAIT_WHEN_JOB_NOT_EXIST.total_seconds():
379+
if seconds_elapsed > to_seconds(DEFAULT_WAIT_WHEN_JOB_NOT_EXIST):
381380
return None
382381

383382
# It might take some time for database replicas to get up-to-date so sleep a bit before retrying

src/apify_client/_resource_clients/actor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
enum_to_value,
2323
filter_none_values,
2424
response_to_dict,
25+
to_seconds,
2526
)
2627
from apify_client.errors import ApifyApiError
2728

@@ -266,7 +267,7 @@ def start(
266267
maxTotalChargeUsd=max_total_charge_usd,
267268
restartOnError=restart_on_error,
268269
memory=memory_mbytes,
269-
timeout=int(timeout.total_seconds()) if timeout is not None else None,
270+
timeout=to_seconds(timeout),
270271
waitForFinish=wait_for_finish,
271272
forcePermissionLevel=force_permission_level.value if force_permission_level is not None else None,
272273
webhooks=encode_webhook_list_to_base64(webhooks) if webhooks is not None else None,
@@ -744,7 +745,7 @@ async def start(
744745
maxTotalChargeUsd=max_total_charge_usd,
745746
restartOnError=restart_on_error,
746747
memory=memory_mbytes,
747-
timeout=int(timeout.total_seconds()) if timeout is not None else None,
748+
timeout=to_seconds(timeout),
748749
waitForFinish=wait_for_finish,
749750
forcePermissionLevel=force_permission_level.value if force_permission_level is not None else None,
750751
webhooks=encode_webhook_list_to_base64(webhooks) if webhooks is not None else None,

src/apify_client/_resource_clients/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ def create_items_public_url(
685685
signature = create_storage_content_signature(
686686
resource_id=dataset.id,
687687
url_signing_secret_key=dataset.url_signing_secret_key,
688-
expires_in_millis=int(expires_in.total_seconds() * 1000) if expires_in is not None else None,
688+
expires_in=expires_in,
689689
)
690690
request_params['signature'] = signature
691691

@@ -1233,7 +1233,7 @@ async def create_items_public_url(
12331233
signature = create_storage_content_signature(
12341234
resource_id=dataset.id,
12351235
url_signing_secret_key=dataset.url_signing_secret_key,
1236-
expires_in_millis=int(expires_in.total_seconds() * 1000) if expires_in is not None else None,
1236+
expires_in=expires_in,
12371237
)
12381238
request_params['signature'] = signature
12391239

src/apify_client/_resource_clients/key_value_store.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def create_keys_public_url(
456456
signature = create_storage_content_signature(
457457
resource_id=metadata.id,
458458
url_signing_secret_key=metadata.url_signing_secret_key,
459-
expires_in_millis=int(expires_in.total_seconds() * 1000) if expires_in is not None else None,
459+
expires_in=expires_in,
460460
)
461461
request_params['signature'] = signature
462462

@@ -865,7 +865,7 @@ async def create_keys_public_url(
865865
signature = create_storage_content_signature(
866866
resource_id=metadata.id,
867867
url_signing_secret_key=metadata.url_signing_secret_key,
868-
expires_in_millis=int(expires_in.total_seconds() * 1000) if expires_in is not None else None,
868+
expires_in=expires_in,
869869
)
870870
request_params['signature'] = signature
871871

src/apify_client/_resource_clients/log.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from typing_extensions import Self
1515

1616
from apify_client._resource_clients._resource_client import ResourceClient, ResourceClientAsync
17-
from apify_client._utils import catch_not_found_or_throw
17+
from apify_client._utils import catch_not_found_or_throw, to_seconds
1818
from apify_client.errors import ApifyApiError
1919

2020
if TYPE_CHECKING:
@@ -403,7 +403,7 @@ def __init__(self, *, to_logger: logging.Logger, check_period: timedelta = timed
403403
if self._force_propagate:
404404
to_logger.propagate = True
405405
self._to_logger = to_logger
406-
self._check_period = check_period.total_seconds()
406+
self._check_period = to_seconds(check_period)
407407
self._last_status_message = ''
408408

409409
def _log_run_data(self, run_data: Run | None) -> bool:

src/apify_client/_resource_clients/request_queue.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
UnlockRequestsResult,
3737
)
3838
from apify_client._resource_clients._resource_client import ResourceClient, ResourceClientAsync
39-
from apify_client._utils import catch_not_found_or_throw, filter_none_values, response_to_dict
39+
from apify_client._utils import catch_not_found_or_throw, filter_none_values, response_to_dict, to_seconds
4040
from apify_client.errors import ApifyApiError
4141

4242
if TYPE_CHECKING:
@@ -169,9 +169,7 @@ def list_and_lock_head(self, *, lock_duration: timedelta, limit: int | None = No
169169
Returns:
170170
The desired number of locked requests from the beginning of the queue.
171171
"""
172-
request_params = self._build_params(
173-
lockSecs=int(lock_duration.total_seconds()), limit=limit, clientKey=self.client_key
174-
)
172+
request_params = self._build_params(lockSecs=to_seconds(lock_duration), limit=limit, clientKey=self.client_key)
175173

176174
response = self._http_client.call(
177175
url=self._build_url('head/lock'),
@@ -297,7 +295,7 @@ def prolong_request_lock(
297295
lock_duration: By how much to prolong the lock.
298296
"""
299297
request_params = self._build_params(
300-
clientKey=self.client_key, forefront=forefront, lockSecs=int(lock_duration.total_seconds())
298+
clientKey=self.client_key, forefront=forefront, lockSecs=to_seconds(lock_duration)
301299
)
302300

303301
response = self._http_client.call(
@@ -598,9 +596,7 @@ async def list_and_lock_head(self, *, lock_duration: timedelta, limit: int | Non
598596
Returns:
599597
The desired number of locked requests from the beginning of the queue.
600598
"""
601-
request_params = self._build_params(
602-
lockSecs=int(lock_duration.total_seconds()), limit=limit, clientKey=self.client_key
603-
)
599+
request_params = self._build_params(lockSecs=to_seconds(lock_duration), limit=limit, clientKey=self.client_key)
604600

605601
response = await self._http_client.call(
606602
url=self._build_url('head/lock'),
@@ -724,7 +720,7 @@ async def prolong_request_lock(
724720
lock_duration: By how much to prolong the lock.
725721
"""
726722
request_params = self._build_params(
727-
clientKey=self.client_key, forefront=forefront, lockSecs=int(lock_duration.total_seconds())
723+
clientKey=self.client_key, forefront=forefront, lockSecs=to_seconds(lock_duration)
728724
)
729725

730726
response = await self._http_client.call(

0 commit comments

Comments
 (0)