|
2 | 2 |
|
3 | 3 | import warnings |
4 | 4 | from functools import cached_property |
5 | | -from typing import TYPE_CHECKING, ClassVar |
| 5 | +from typing import TYPE_CHECKING, ClassVar, overload |
6 | 6 |
|
7 | 7 | from apify_client._client_registry import ClientRegistry, ClientRegistryAsync |
8 | 8 | from apify_client._consts import ( |
@@ -113,6 +113,29 @@ class ApifyClient: |
113 | 113 |
|
114 | 114 | _OVERRIDABLE_DEFAULT_HEADERS: ClassVar[set[str]] = {'Accept', 'Authorization', 'Accept-Encoding', 'User-Agent'} |
115 | 115 |
|
| 116 | + @overload |
| 117 | + def __init__( |
| 118 | + self, |
| 119 | + token: str | None = None, |
| 120 | + *, |
| 121 | + api_url: str = DEFAULT_API_URL, |
| 122 | + api_public_url: str | None = DEFAULT_API_URL, |
| 123 | + max_retries: int = DEFAULT_MAX_RETRIES, |
| 124 | + min_delay_between_retries: timedelta = DEFAULT_MIN_DELAY_BETWEEN_RETRIES, |
| 125 | + timeout: timedelta = DEFAULT_TIMEOUT, |
| 126 | + headers: dict[str, str] | None = None, |
| 127 | + ) -> None: ... |
| 128 | + |
| 129 | + @overload |
| 130 | + def __init__( |
| 131 | + self, |
| 132 | + token: str | None = None, |
| 133 | + *, |
| 134 | + api_url: str = DEFAULT_API_URL, |
| 135 | + api_public_url: str | None = DEFAULT_API_URL, |
| 136 | + http_client: HttpClient, |
| 137 | + ) -> None: ... |
| 138 | + |
116 | 139 | def __init__( |
117 | 140 | self, |
118 | 141 | token: str | None = None, |
@@ -144,13 +167,33 @@ def __init__( |
144 | 167 | headers: Additional HTTP headers to include in all API requests. Only used when `http_client` is not |
145 | 168 | provided. |
146 | 169 | http_client: A custom HTTP client instance extending `HttpClient`. When provided, the `max_retries`, |
147 | | - `min_delay_between_retries`, `timeout`, and `headers` parameters are ignored, as the custom |
| 170 | + `min_delay_between_retries`, `timeout`, and `headers` parameters must not be set, as the custom |
148 | 171 | client is responsible for its own configuration. |
| 172 | +
|
| 173 | + Raises: |
| 174 | + ValueError: If `http_client` is provided together with `max_retries`, `min_delay_between_retries`, |
| 175 | + `timeout`, or `headers`. |
149 | 176 | """ |
150 | 177 | # We need to do this because of mocking in tests and default mutable arguments. |
151 | 178 | api_url = DEFAULT_API_URL if api_url is None else api_url |
152 | 179 | api_public_url = DEFAULT_API_URL if api_public_url is None else api_public_url |
153 | 180 |
|
| 181 | + if http_client is not None: |
| 182 | + conflicting = [] |
| 183 | + if max_retries != DEFAULT_MAX_RETRIES: |
| 184 | + conflicting.append('max_retries') |
| 185 | + if min_delay_between_retries != DEFAULT_MIN_DELAY_BETWEEN_RETRIES: |
| 186 | + conflicting.append('min_delay_between_retries') |
| 187 | + if timeout != DEFAULT_TIMEOUT: |
| 188 | + conflicting.append('timeout') |
| 189 | + if headers is not None: |
| 190 | + conflicting.append('headers') |
| 191 | + if conflicting: |
| 192 | + raise ValueError( |
| 193 | + f'Cannot pass {", ".join(conflicting)} together with http_client. When using a custom ' |
| 194 | + 'HTTP client, configure these options directly on the client instance.' |
| 195 | + ) |
| 196 | + |
154 | 197 | if headers and not http_client: |
155 | 198 | self._check_custom_headers(headers) |
156 | 199 |
|
@@ -427,6 +470,29 @@ async def main() -> None: |
427 | 470 |
|
428 | 471 | _OVERRIDABLE_DEFAULT_HEADERS: ClassVar[set[str]] = {'Accept', 'Authorization', 'Accept-Encoding', 'User-Agent'} |
429 | 472 |
|
| 473 | + @overload |
| 474 | + def __init__( |
| 475 | + self, |
| 476 | + token: str | None = None, |
| 477 | + *, |
| 478 | + api_url: str = DEFAULT_API_URL, |
| 479 | + api_public_url: str | None = DEFAULT_API_URL, |
| 480 | + max_retries: int = DEFAULT_MAX_RETRIES, |
| 481 | + min_delay_between_retries: timedelta = DEFAULT_MIN_DELAY_BETWEEN_RETRIES, |
| 482 | + timeout: timedelta = DEFAULT_TIMEOUT, |
| 483 | + headers: dict[str, str] | None = None, |
| 484 | + ) -> None: ... |
| 485 | + |
| 486 | + @overload |
| 487 | + def __init__( |
| 488 | + self, |
| 489 | + token: str | None = None, |
| 490 | + *, |
| 491 | + api_url: str = DEFAULT_API_URL, |
| 492 | + api_public_url: str | None = DEFAULT_API_URL, |
| 493 | + http_client: HttpClientAsync, |
| 494 | + ) -> None: ... |
| 495 | + |
430 | 496 | def __init__( |
431 | 497 | self, |
432 | 498 | token: str | None = None, |
@@ -458,13 +524,33 @@ def __init__( |
458 | 524 | headers: Additional HTTP headers to include in all API requests. Only used when `http_client` is not |
459 | 525 | provided. |
460 | 526 | http_client: A custom HTTP client instance extending `HttpClientAsync`. When provided, the `max_retries`, |
461 | | - `min_delay_between_retries`, `timeout`, and `headers` parameters are ignored, as the custom |
| 527 | + `min_delay_between_retries`, `timeout`, and `headers` parameters must not be set, as the custom |
462 | 528 | client is responsible for its own configuration. |
| 529 | +
|
| 530 | + Raises: |
| 531 | + ValueError: If `http_client` is provided together with `max_retries`, `min_delay_between_retries`, |
| 532 | + `timeout`, or `headers`. |
463 | 533 | """ |
464 | 534 | # We need to do this because of mocking in tests and default mutable arguments. |
465 | 535 | api_url = DEFAULT_API_URL if api_url is None else api_url |
466 | 536 | api_public_url = DEFAULT_API_URL if api_public_url is None else api_public_url |
467 | 537 |
|
| 538 | + if http_client is not None: |
| 539 | + conflicting = [] |
| 540 | + if max_retries != DEFAULT_MAX_RETRIES: |
| 541 | + conflicting.append('max_retries') |
| 542 | + if min_delay_between_retries != DEFAULT_MIN_DELAY_BETWEEN_RETRIES: |
| 543 | + conflicting.append('min_delay_between_retries') |
| 544 | + if timeout != DEFAULT_TIMEOUT: |
| 545 | + conflicting.append('timeout') |
| 546 | + if headers is not None: |
| 547 | + conflicting.append('headers') |
| 548 | + if conflicting: |
| 549 | + raise ValueError( |
| 550 | + f'Cannot pass {", ".join(conflicting)} together with http_client. When using a custom ' |
| 551 | + 'HTTP client, configure these options directly on the client instance.' |
| 552 | + ) |
| 553 | + |
468 | 554 | if headers and not http_client: |
469 | 555 | self._check_custom_headers(headers) |
470 | 556 |
|
|
0 commit comments