|
17 | 17 |
|
18 | 18 | from ._errors import ( |
19 | 19 | BaseRequestException, |
| 20 | + ConfigurationException, |
20 | 21 | RateLimitExceededException, |
21 | 22 | ServerException, |
22 | 23 | WorkOSConnectionException, |
@@ -401,16 +402,12 @@ def __init__( |
401 | 402 | max_retries: int = MAX_RETRIES, |
402 | 403 | ) -> None: |
403 | 404 | self._api_key = api_key or os.environ.get("WORKOS_API_KEY") |
404 | | - if not self._api_key: |
405 | | - raise ValueError( |
406 | | - "WorkOS API key must be provided when instantiating the client " |
407 | | - "or via the WORKOS_API_KEY environment variable." |
408 | | - ) |
409 | 405 | self.client_id = client_id or os.environ.get("WORKOS_CLIENT_ID") |
410 | | - if not self.client_id: |
| 406 | + if not self._api_key and not self.client_id: |
411 | 407 | raise ValueError( |
412 | | - "WorkOS client ID must be provided when instantiating the client " |
413 | | - "or via the WORKOS_CLIENT_ID environment variable." |
| 408 | + "WorkOS requires either an API key or a client ID. " |
| 409 | + "Provide api_key / WORKOS_API_KEY for authenticated server-side usage, " |
| 410 | + "or client_id / WORKOS_CLIENT_ID for flows that require a client ID." |
414 | 411 | ) |
415 | 412 | resolved_base_url = base_url or os.environ.get( |
416 | 413 | "WORKOS_BASE_URL", "https://api.workos.com" |
@@ -493,17 +490,34 @@ def _resolve_max_retries(self, request_options: Optional[RequestOptions]) -> int |
493 | 490 | return retries |
494 | 491 | return self._max_retries |
495 | 492 |
|
| 493 | + def _require_api_key(self) -> str: |
| 494 | + if not self._api_key: |
| 495 | + raise ConfigurationException( |
| 496 | + "This operation requires a WorkOS API key. Provide api_key when instantiating the client " |
| 497 | + "or via the WORKOS_API_KEY environment variable." |
| 498 | + ) |
| 499 | + return self._api_key |
| 500 | + |
| 501 | + def _require_client_id(self) -> str: |
| 502 | + if not self.client_id: |
| 503 | + raise ConfigurationException( |
| 504 | + "This operation requires a WorkOS client ID. Provide client_id when instantiating the client " |
| 505 | + "or via the WORKOS_CLIENT_ID environment variable." |
| 506 | + ) |
| 507 | + return self.client_id |
| 508 | + |
496 | 509 | def _build_headers( |
497 | 510 | self, |
498 | 511 | method: str, |
499 | 512 | idempotency_key: Optional[str], |
500 | 513 | request_options: Optional[RequestOptions], |
501 | 514 | ) -> Dict[str, str]: |
502 | 515 | headers: Dict[str, str] = { |
503 | | - "Authorization": f"Bearer {self._api_key}", |
504 | 516 | "Content-Type": "application/json", |
505 | 517 | "User-Agent": f"workos-python/{VERSION} python/{platform.python_version()}", |
506 | 518 | } |
| 519 | + if self._api_key: |
| 520 | + headers["Authorization"] = f"Bearer {self._api_key}" |
507 | 521 | effective_idempotency_key = idempotency_key |
508 | 522 | if effective_idempotency_key is None and request_options: |
509 | 523 | request_option_idempotency_key = request_options.get("idempotency_key") |
@@ -652,7 +666,7 @@ def __init__( |
652 | 666 | max_retries: Maximum number of retries for failed requests. Defaults to 3. |
653 | 667 |
|
654 | 668 | Raises: |
655 | | - ValueError: If api_key is not provided and WORKOS_API_KEY is not set. |
| 669 | + ValueError: If neither api_key nor client_id is provided, directly or via environment variables. |
656 | 670 | """ |
657 | 671 | super().__init__( |
658 | 672 | api_key=api_key, |
@@ -931,7 +945,7 @@ def __init__( |
931 | 945 | max_retries: Maximum number of retries for failed requests. Defaults to 3. |
932 | 946 |
|
933 | 947 | Raises: |
934 | | - ValueError: If api_key is not provided and WORKOS_API_KEY is not set. |
| 948 | + ValueError: If neither api_key nor client_id is provided, directly or via environment variables. |
935 | 949 | """ |
936 | 950 | super().__init__( |
937 | 951 | api_key=api_key, |
|
0 commit comments