|
| 1 | +# |
| 2 | +# Copyright (c) 2025, Neptune Labs Sp. z o.o. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from http import HTTPStatus |
| 17 | +from typing import ( |
| 18 | + Any, |
| 19 | + Dict, |
| 20 | + Optional, |
| 21 | + Union, |
| 22 | + cast, |
| 23 | +) |
| 24 | + |
| 25 | +import httpx |
| 26 | + |
| 27 | +from ... import errors |
| 28 | +from ...client import ( |
| 29 | + AuthenticatedClient, |
| 30 | + Client, |
| 31 | +) |
| 32 | +from ...models.error import Error |
| 33 | +from ...models.neptune_oauth_token import NeptuneOauthToken |
| 34 | +from ...types import Response |
| 35 | + |
| 36 | + |
| 37 | +def _get_kwargs( |
| 38 | + *, |
| 39 | + x_neptune_api_token: str, |
| 40 | +) -> Dict[str, Any]: |
| 41 | + headers: Dict[str, Any] = {} |
| 42 | + headers["X-Neptune-Api-Token"] = x_neptune_api_token |
| 43 | + |
| 44 | + _kwargs: Dict[str, Any] = { |
| 45 | + "method": "get", |
| 46 | + "url": "/api/backend/v1/authorization/oauth-token", |
| 47 | + } |
| 48 | + |
| 49 | + _kwargs["headers"] = headers |
| 50 | + return _kwargs |
| 51 | + |
| 52 | + |
| 53 | +def _parse_response( |
| 54 | + *, client: Union[AuthenticatedClient, Client], response: httpx.Response |
| 55 | +) -> Optional[Union[Any, Error, NeptuneOauthToken]]: |
| 56 | + try: |
| 57 | + if response.status_code == HTTPStatus.OK: |
| 58 | + response_200 = NeptuneOauthToken.from_dict(response.json()) |
| 59 | + |
| 60 | + return response_200 |
| 61 | + if response.status_code == HTTPStatus.BAD_REQUEST: |
| 62 | + response_400 = Error.from_dict(response.json()) |
| 63 | + |
| 64 | + return response_400 |
| 65 | + if response.status_code == HTTPStatus.UNAUTHORIZED: |
| 66 | + response_401 = cast(Any, None) |
| 67 | + return response_401 |
| 68 | + if response.status_code == HTTPStatus.FORBIDDEN: |
| 69 | + response_403 = cast(Any, None) |
| 70 | + return response_403 |
| 71 | + if response.status_code == HTTPStatus.NOT_FOUND: |
| 72 | + response_404 = cast(Any, None) |
| 73 | + return response_404 |
| 74 | + if response.status_code == HTTPStatus.REQUEST_TIMEOUT: |
| 75 | + response_408 = cast(Any, None) |
| 76 | + return response_408 |
| 77 | + if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: |
| 78 | + response_422 = cast(Any, None) |
| 79 | + return response_422 |
| 80 | + except Exception as e: |
| 81 | + raise errors.UnableToParseResponse(e, response) from e |
| 82 | + |
| 83 | + if client.raise_on_unexpected_status: |
| 84 | + raise errors.UnexpectedStatus(response.status_code, response.content) |
| 85 | + else: |
| 86 | + return None |
| 87 | + |
| 88 | + |
| 89 | +def _build_response( |
| 90 | + *, client: Union[AuthenticatedClient, Client], response: httpx.Response |
| 91 | +) -> Response[Union[Any, Error, NeptuneOauthToken]]: |
| 92 | + return Response( |
| 93 | + status_code=HTTPStatus(response.status_code), |
| 94 | + content=response.content, |
| 95 | + headers=response.headers, |
| 96 | + parsed=_parse_response(client=client, response=response), |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def sync_detailed( |
| 101 | + *, |
| 102 | + client: AuthenticatedClient, |
| 103 | + x_neptune_api_token: str, |
| 104 | +) -> Response[Union[Any, Error, NeptuneOauthToken]]: |
| 105 | + """ |
| 106 | + Args: |
| 107 | + x_neptune_api_token (str): |
| 108 | +
|
| 109 | + Raises: |
| 110 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 111 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 112 | +
|
| 113 | + Returns: |
| 114 | + Response[Union[Any, Error, NeptuneOauthToken]] |
| 115 | + """ |
| 116 | + |
| 117 | + kwargs = _get_kwargs( |
| 118 | + x_neptune_api_token=x_neptune_api_token, |
| 119 | + ) |
| 120 | + |
| 121 | + response = client.get_httpx_client().request( |
| 122 | + **kwargs, |
| 123 | + ) |
| 124 | + |
| 125 | + return _build_response(client=client, response=response) |
| 126 | + |
| 127 | + |
| 128 | +def sync( |
| 129 | + *, |
| 130 | + client: AuthenticatedClient, |
| 131 | + x_neptune_api_token: str, |
| 132 | +) -> Optional[Union[Any, Error, NeptuneOauthToken]]: |
| 133 | + """ |
| 134 | + Args: |
| 135 | + x_neptune_api_token (str): |
| 136 | +
|
| 137 | + Raises: |
| 138 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 139 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 140 | +
|
| 141 | + Returns: |
| 142 | + Union[Any, Error, NeptuneOauthToken] |
| 143 | + """ |
| 144 | + |
| 145 | + return sync_detailed( |
| 146 | + client=client, |
| 147 | + x_neptune_api_token=x_neptune_api_token, |
| 148 | + ).parsed |
| 149 | + |
| 150 | + |
| 151 | +async def asyncio_detailed( |
| 152 | + *, |
| 153 | + client: AuthenticatedClient, |
| 154 | + x_neptune_api_token: str, |
| 155 | +) -> Response[Union[Any, Error, NeptuneOauthToken]]: |
| 156 | + """ |
| 157 | + Args: |
| 158 | + x_neptune_api_token (str): |
| 159 | +
|
| 160 | + Raises: |
| 161 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 162 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 163 | +
|
| 164 | + Returns: |
| 165 | + Response[Union[Any, Error, NeptuneOauthToken]] |
| 166 | + """ |
| 167 | + |
| 168 | + kwargs = _get_kwargs( |
| 169 | + x_neptune_api_token=x_neptune_api_token, |
| 170 | + ) |
| 171 | + |
| 172 | + response = await client.get_async_httpx_client().request(**kwargs) |
| 173 | + |
| 174 | + return _build_response(client=client, response=response) |
| 175 | + |
| 176 | + |
| 177 | +async def asyncio( |
| 178 | + *, |
| 179 | + client: AuthenticatedClient, |
| 180 | + x_neptune_api_token: str, |
| 181 | +) -> Optional[Union[Any, Error, NeptuneOauthToken]]: |
| 182 | + """ |
| 183 | + Args: |
| 184 | + x_neptune_api_token (str): |
| 185 | +
|
| 186 | + Raises: |
| 187 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 188 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 189 | +
|
| 190 | + Returns: |
| 191 | + Union[Any, Error, NeptuneOauthToken] |
| 192 | + """ |
| 193 | + |
| 194 | + return ( |
| 195 | + await asyncio_detailed( |
| 196 | + client=client, |
| 197 | + x_neptune_api_token=x_neptune_api_token, |
| 198 | + ) |
| 199 | + ).parsed |
0 commit comments