|
| 1 | +from os import getenv |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from attrs import define, evolve, field |
| 5 | + |
| 6 | +from code_to_optimize.code_directories.circular_deps.constants import DEFAULT_API_URL, DEFAULT_APP_URL |
| 7 | + |
| 8 | + |
| 9 | +@define |
| 10 | +class GalileoApiClient(): |
| 11 | + """A Client which has been authenticated for use on secured endpoints |
| 12 | +
|
| 13 | + The following are accepted as keyword arguments and will be used to construct httpx Clients internally: |
| 14 | +
|
| 15 | + ``base_url``: The base URL for the API, all requests are made to a relative path to this URL |
| 16 | + This can also be set via the GALILEO_CONSOLE_URL environment variable |
| 17 | +
|
| 18 | + ``api_key``: The API key to be sent with every request |
| 19 | + This can also be set via the GALILEO_API_KEY environment variable |
| 20 | +
|
| 21 | + ``cookies``: A dictionary of cookies to be sent with every request |
| 22 | +
|
| 23 | + ``headers``: A dictionary of headers to be sent with every request |
| 24 | +
|
| 25 | + ``timeout``: The maximum amount of a time a request can take. API functions will raise |
| 26 | + httpx.TimeoutException if this is exceeded. |
| 27 | +
|
| 28 | + ``verify_ssl``: Whether or not to verify the SSL certificate of the API server. This should be True in production, |
| 29 | + but can be set to False for testing purposes. |
| 30 | +
|
| 31 | + ``follow_redirects``: Whether or not to follow redirects. Default value is False. |
| 32 | +
|
| 33 | + ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor. |
| 34 | +
|
| 35 | + Attributes: |
| 36 | + raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a |
| 37 | + status code that was not documented in the source OpenAPI document. Can also be provided as a keyword |
| 38 | + argument to the constructor. |
| 39 | + token: The token to use for authentication |
| 40 | + prefix: The prefix to use for the Authorization header |
| 41 | + auth_header_name: The name of the Authorization header |
| 42 | + """ |
| 43 | + |
| 44 | + _base_url: Optional[str] = field(factory=lambda: GalileoApiClient.get_api_url(), kw_only=True, alias="base_url") |
| 45 | + _api_key: Optional[str] = field(factory=lambda: getenv("GALILEO_API_KEY", None), kw_only=True, alias="api_key") |
| 46 | + token: Optional[str] = None |
| 47 | + |
| 48 | + api_key_header_name: str = "Galileo-API-Key" |
| 49 | + client_type_header_name: str = "client-type" |
| 50 | + client_type_header_value: str = "sdk-python" |
| 51 | + |
| 52 | + @staticmethod |
| 53 | + def get_console_url() -> str: |
| 54 | + console_url = getenv("GALILEO_CONSOLE_URL", DEFAULT_API_URL) |
| 55 | + if DEFAULT_API_URL == console_url: |
| 56 | + return DEFAULT_APP_URL |
| 57 | + |
| 58 | + return console_url |
| 59 | + |
| 60 | + def with_api_key(self, api_key: str) -> "GalileoApiClient": |
| 61 | + """Get a new client matching this one with a new API key""" |
| 62 | + if self._client is not None: |
| 63 | + self._client.headers.update({self.api_key_header_name: api_key}) |
| 64 | + if self._async_client is not None: |
| 65 | + self._async_client.headers.update({self.api_key_header_name: api_key}) |
| 66 | + return evolve(self, api_key=api_key) |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def get_api_url(base_url: Optional[str] = None) -> str: |
| 70 | + api_url = base_url or getenv("GALILEO_CONSOLE_URL", DEFAULT_API_URL) |
| 71 | + if api_url is None: |
| 72 | + raise ValueError("base_url or GALILEO_CONSOLE_URL must be set") |
| 73 | + if any(map(api_url.__contains__, ["localhost", "127.0.0.1"])): |
| 74 | + api_url = "http://localhost:8088" |
| 75 | + else: |
| 76 | + api_url = api_url.replace("app.galileo.ai", "api.galileo.ai").replace("console", "api") |
| 77 | + return api_url |
0 commit comments