|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 |
|
16 | | -from dataclasses import dataclass |
17 | | -from http import HTTPStatus |
18 | 16 | from typing import ( |
19 | 17 | Callable, |
20 | 18 | Dict, |
|
23 | 21 |
|
24 | 22 | import httpx |
25 | 23 |
|
26 | | -from neptune_query.generated.neptune_api import ( |
27 | | - AuthenticatedClient, |
28 | | - Client, |
29 | | -) |
30 | | -from neptune_query.generated.neptune_api.api.backend import get_client_config |
| 24 | +from neptune_query.generated.neptune_api import AuthenticatedClient |
31 | 25 | from neptune_query.generated.neptune_api.auth_helpers import exchange_api_key |
32 | 26 | from neptune_query.generated.neptune_api.credentials import Credentials |
33 | | -from neptune_query.generated.neptune_api.models import ClientConfig |
34 | | -from neptune_query.generated.neptune_api.types import Response |
35 | 27 |
|
36 | | -from ..exceptions import NeptuneFailedToFetchClientConfig |
37 | 28 | from .env import ( |
38 | 29 | NEPTUNE_HTTP_REQUEST_TIMEOUT_SECONDS, |
39 | 30 | NEPTUNE_VERIFY_SSL, |
40 | 31 | ) |
41 | | -from .retrieval.retry import handle_errors_default |
42 | | - |
43 | | - |
44 | | -@dataclass |
45 | | -class TokenRefreshingURLs: |
46 | | - authorization_endpoint: str |
47 | | - token_endpoint: str |
48 | | - |
49 | | - @classmethod |
50 | | - def from_dict(cls, data: dict) -> "TokenRefreshingURLs": |
51 | | - return TokenRefreshingURLs( |
52 | | - authorization_endpoint=data["authorization_endpoint"], token_endpoint=data["token_endpoint"] |
53 | | - ) |
54 | | - |
55 | | - |
56 | | -def _wrap_httpx_json_response(httpx_response: httpx.Response) -> Response: |
57 | | - """Wrap a httpx.Response into an neptune-api Response object that is compatible |
58 | | - with backoff_retry(). Use .json() as parsed content in the result.""" |
59 | | - |
60 | | - return Response( |
61 | | - status_code=HTTPStatus(httpx_response.status_code), |
62 | | - content=httpx_response.content, |
63 | | - headers=httpx_response.headers, |
64 | | - parsed=httpx_response.json(), |
65 | | - url=str(httpx_response.url), |
66 | | - ) |
67 | | - |
68 | | - |
69 | | -def get_config_and_token_urls( |
70 | | - *, credentials: Credentials, proxies: Optional[Dict[str, str]] = None |
71 | | -) -> tuple[ClientConfig, TokenRefreshingURLs]: |
72 | | - timeout = httpx.Timeout(NEPTUNE_HTTP_REQUEST_TIMEOUT_SECONDS.get()) |
73 | | - with Client( |
74 | | - base_url=credentials.base_url, |
75 | | - httpx_args={"mounts": proxies}, |
76 | | - verify_ssl=NEPTUNE_VERIFY_SSL.get(), |
77 | | - timeout=timeout, |
78 | | - headers={"User-Agent": _generate_user_agent()}, |
79 | | - ) as client: |
80 | | - try: |
81 | | - config_response = handle_errors_default(get_client_config.sync_detailed)(client=client) |
82 | | - config = config_response.parsed |
83 | | - if not isinstance(config, ClientConfig): |
84 | | - raise RuntimeError(f"Expected ClientConfig but got {type(config).__name__}") |
85 | | - |
86 | | - urls_response = handle_errors_default( |
87 | | - lambda: _wrap_httpx_json_response(client.get_httpx_client().get(config.security.open_id_discovery)) |
88 | | - )() |
89 | | - token_urls_dict = urls_response.parsed |
90 | | - if not isinstance(token_urls_dict, dict): |
91 | | - raise RuntimeError(f"Expected dict but got {type(token_urls_dict).__name__}") |
92 | | - token_urls = TokenRefreshingURLs.from_dict(token_urls_dict) |
93 | | - |
94 | | - return config, token_urls |
95 | | - except Exception as e: |
96 | | - raise NeptuneFailedToFetchClientConfig(exception=e) from e |
97 | 32 |
|
98 | 33 |
|
99 | 34 | def create_auth_api_client( |
100 | 35 | *, |
101 | 36 | credentials: Credentials, |
102 | | - config: ClientConfig, |
103 | | - token_refreshing_urls: TokenRefreshingURLs, |
104 | 37 | proxies: Optional[Dict[str, str]] = None, |
105 | 38 | ) -> AuthenticatedClient: |
106 | 39 | return AuthenticatedClient( |
107 | 40 | base_url=credentials.base_url, |
108 | 41 | credentials=credentials, |
109 | | - client_id=config.security.client_id, |
110 | | - token_refreshing_endpoint=token_refreshing_urls.token_endpoint, |
111 | 42 | api_key_exchange_callback=exchange_api_key, |
112 | 43 | verify_ssl=NEPTUNE_VERIFY_SSL.get(), |
113 | 44 | httpx_args={"mounts": proxies, "http2": False}, |
|
0 commit comments