|
| 1 | +from http import HTTPStatus |
| 2 | +from typing import Any, Dict, Optional, Union |
| 3 | + |
| 4 | +import httpx |
| 5 | + |
| 6 | +from ... import errors |
| 7 | +from ...client import Client |
| 8 | +from ...models.error import Error |
| 9 | +from ...models.instance_configuration import InstanceConfiguration |
| 10 | +from ...types import Response |
| 11 | + |
| 12 | + |
| 13 | +def _get_kwargs( |
| 14 | + *, |
| 15 | + client: Client, |
| 16 | +) -> Dict[str, Any]: |
| 17 | + url = "{}/config".format(client.base_url) |
| 18 | + |
| 19 | + headers: Dict[str, str] = client.get_headers() |
| 20 | + cookies: Dict[str, Any] = client.get_cookies() |
| 21 | + |
| 22 | + # Set the proxies if the client has proxies set. |
| 23 | + proxies = None |
| 24 | + if hasattr(client, "proxies") and client.proxies is not None: |
| 25 | + https_proxy = client.proxies.get("https") |
| 26 | + if https_proxy: |
| 27 | + proxies = https_proxy |
| 28 | + else: |
| 29 | + http_proxy = client.proxies.get("http") |
| 30 | + if http_proxy: |
| 31 | + proxies = http_proxy |
| 32 | + |
| 33 | + return { |
| 34 | + "method": "get", |
| 35 | + "url": url, |
| 36 | + "headers": headers, |
| 37 | + "cookies": cookies, |
| 38 | + "timeout": client.get_timeout(), |
| 39 | + "proxies": proxies, |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[Error, InstanceConfiguration]]: |
| 44 | + if response.status_code == HTTPStatus.OK: |
| 45 | + response_200 = InstanceConfiguration.from_dict(response.json()) |
| 46 | + |
| 47 | + return response_200 |
| 48 | + if response.status_code == HTTPStatus.UNAUTHORIZED: |
| 49 | + response_401 = Error.from_dict(response.json()) |
| 50 | + |
| 51 | + return response_401 |
| 52 | + if response.status_code == HTTPStatus.FORBIDDEN: |
| 53 | + response_403 = Error.from_dict(response.json()) |
| 54 | + |
| 55 | + return response_403 |
| 56 | + if response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR: |
| 57 | + response_500 = Error.from_dict(response.json()) |
| 58 | + |
| 59 | + return response_500 |
| 60 | + if client.raise_on_unexpected_status: |
| 61 | + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") |
| 62 | + else: |
| 63 | + return None |
| 64 | + |
| 65 | + |
| 66 | +def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[Error, InstanceConfiguration]]: |
| 67 | + return Response( |
| 68 | + status_code=HTTPStatus(response.status_code), |
| 69 | + content=response.content, |
| 70 | + headers=response.headers, |
| 71 | + parsed=_parse_response(client=client, response=response), |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def sync_detailed( |
| 76 | + *, |
| 77 | + client: Client, |
| 78 | +) -> Response[Union[Error, InstanceConfiguration]]: |
| 79 | + """get information about the instance's configuration (requires administrative privileges) |
| 80 | +
|
| 81 | + Raises: |
| 82 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 83 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + Response[Union[Error, InstanceConfiguration]] |
| 87 | + """ |
| 88 | + |
| 89 | + kwargs = _get_kwargs( |
| 90 | + client=client, |
| 91 | + ) |
| 92 | + |
| 93 | + response = httpx.request( |
| 94 | + verify=client.verify_ssl, |
| 95 | + **kwargs, |
| 96 | + ) |
| 97 | + |
| 98 | + return _build_response(client=client, response=response) |
| 99 | + |
| 100 | + |
| 101 | +def sync( |
| 102 | + *, |
| 103 | + client: Client, |
| 104 | +) -> Optional[Union[Error, InstanceConfiguration]]: |
| 105 | + """get information about the instance's configuration (requires administrative privileges) |
| 106 | +
|
| 107 | + Raises: |
| 108 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 109 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 110 | +
|
| 111 | + Returns: |
| 112 | + Response[Union[Error, InstanceConfiguration]] |
| 113 | + """ |
| 114 | + |
| 115 | + return sync_detailed( |
| 116 | + client=client, |
| 117 | + ).parsed |
| 118 | + |
| 119 | + |
| 120 | +async def asyncio_detailed( |
| 121 | + *, |
| 122 | + client: Client, |
| 123 | +) -> Response[Union[Error, InstanceConfiguration]]: |
| 124 | + """get information about the instance's configuration (requires administrative privileges) |
| 125 | +
|
| 126 | + Raises: |
| 127 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 128 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 129 | +
|
| 130 | + Returns: |
| 131 | + Response[Union[Error, InstanceConfiguration]] |
| 132 | + """ |
| 133 | + |
| 134 | + kwargs = _get_kwargs( |
| 135 | + client=client, |
| 136 | + ) |
| 137 | + |
| 138 | + async with httpx.AsyncClient(verify=client.verify_ssl) as _client: |
| 139 | + response = await _client.request(**kwargs) |
| 140 | + |
| 141 | + return _build_response(client=client, response=response) |
| 142 | + |
| 143 | + |
| 144 | +async def asyncio( |
| 145 | + *, |
| 146 | + client: Client, |
| 147 | +) -> Optional[Union[Error, InstanceConfiguration]]: |
| 148 | + """get information about the instance's configuration (requires administrative privileges) |
| 149 | +
|
| 150 | + Raises: |
| 151 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 152 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 153 | +
|
| 154 | + Returns: |
| 155 | + Response[Union[Error, InstanceConfiguration]] |
| 156 | + """ |
| 157 | + |
| 158 | + return ( |
| 159 | + await asyncio_detailed( |
| 160 | + client=client, |
| 161 | + ) |
| 162 | + ).parsed |
0 commit comments