| | from __future__ import annotations |
| |
|
| | from typing import TYPE_CHECKING, Any, cast |
| |
|
| | from .._extras import google_auth |
| |
|
| | if TYPE_CHECKING: |
| | from google.auth.credentials import Credentials |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| |
|
| | def load_auth(*, project_id: str | None) -> tuple[Credentials, str]: |
| | try: |
| | from google.auth.transport.requests import Request |
| | except ModuleNotFoundError as err: |
| | raise RuntimeError( |
| | f"Could not import google.auth, you need to install the SDK with `pip install anthropic[vertex]`" |
| | ) from err |
| |
|
| | credentials, loaded_project_id = google_auth.default( |
| | scopes=["https://www.googleapis.com/auth/cloud-platform"], |
| | ) |
| | credentials = cast(Any, credentials) |
| | credentials.refresh(Request()) |
| |
|
| | if not project_id: |
| | project_id = loaded_project_id |
| |
|
| | if not project_id: |
| | raise ValueError("Could not resolve project_id") |
| |
|
| | return credentials, project_id |
| |
|
| |
|
| | def refresh_auth(credentials: Credentials) -> None: |
| | from google.auth.transport.requests import Request |
| |
|
| | credentials.refresh(Request()) |
| |
|