cirro_api_client.v1.client

  1import ssl
  2from typing import Any, Dict, Optional, Union
  3
  4import httpx
  5from attrs import define, evolve, field
  6
  7
  8@define
  9class Client:
 10    """A class for keeping track of data related to the API
 11
 12    The following are accepted as keyword arguments and will be used to construct httpx Clients internally:
 13
 14        ``base_url``: The base URL for the API, all requests are made to a relative path to this URL
 15
 16        ``cookies``: A dictionary of cookies to be sent with every request
 17
 18        ``headers``: A dictionary of headers to be sent with every request
 19
 20        ``timeout``: The maximum amount of a time a request can take. API functions will raise
 21        httpx.TimeoutException if this is exceeded.
 22
 23        ``verify_ssl``: Whether or not to verify the SSL certificate of the API server. This should be True in production,
 24        but can be set to False for testing purposes.
 25
 26        ``follow_redirects``: Whether or not to follow redirects. Default value is False.
 27
 28        ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
 29
 30
 31    Attributes:
 32        raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
 33            status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
 34            argument to the constructor.
 35
 36        auth_method: Optionally provide a method used to authenticate API requests.
 37    """
 38
 39    raise_on_unexpected_status: bool = field(default=False, kw_only=True)
 40    auth_method: httpx.Auth = field(default=None, kw_only=True)
 41    _base_url: str
 42    _cookies: Dict[str, str] = field(factory=dict, kw_only=True)
 43    _headers: Dict[str, str] = field(factory=dict, kw_only=True)
 44    _timeout: Optional[httpx.Timeout] = field(default=None, kw_only=True)
 45    _verify_ssl: Union[str, bool, ssl.SSLContext] = field(default=True, kw_only=True)
 46    _follow_redirects: bool = field(default=False, kw_only=True)
 47    _httpx_args: Dict[str, Any] = field(factory=dict, kw_only=True)
 48    _client: Optional[httpx.Client] = field(default=None, init=False)
 49    _async_client: Optional[httpx.AsyncClient] = field(default=None, init=False)
 50
 51    def get_auth(self):
 52        return self.auth_method
 53
 54    def with_headers(self, headers: Dict[str, str]) -> "Client":
 55        """Get a new client matching this one with additional headers"""
 56        if self._client is not None:
 57            self._client.headers.update(headers)
 58        if self._async_client is not None:
 59            self._async_client.headers.update(headers)
 60        return evolve(self, headers={**self._headers, **headers})
 61
 62    def with_cookies(self, cookies: Dict[str, str]) -> "Client":
 63        """Get a new client matching this one with additional cookies"""
 64        if self._client is not None:
 65            self._client.cookies.update(cookies)
 66        if self._async_client is not None:
 67            self._async_client.cookies.update(cookies)
 68        return evolve(self, cookies={**self._cookies, **cookies})
 69
 70    def with_timeout(self, timeout: httpx.Timeout) -> "Client":
 71        """Get a new client matching this one with a new timeout (in seconds)"""
 72        if self._client is not None:
 73            self._client.timeout = timeout
 74        if self._async_client is not None:
 75            self._async_client.timeout = timeout
 76        return evolve(self, timeout=timeout)
 77
 78    def set_httpx_client(self, client: httpx.Client) -> "Client":
 79        """Manually the underlying httpx.Client
 80
 81        **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
 82        """
 83        self._client = client
 84        return self
 85
 86    def get_httpx_client(self) -> httpx.Client:
 87        """Get the underlying httpx.Client, constructing a new one if not previously set"""
 88        if self._client is None:
 89            self._client = httpx.Client(
 90                base_url=self._base_url,
 91                cookies=self._cookies,
 92                headers=self._headers,
 93                timeout=self._timeout,
 94                verify=self._verify_ssl,
 95                follow_redirects=self._follow_redirects,
 96                **self._httpx_args,
 97            )
 98        return self._client
 99
100    def __enter__(self) -> "Client":
101        """Enter a context manager for self.client—you cannot enter twice (see httpx docs)"""
102        self.get_httpx_client().__enter__()
103        return self
104
105    def __exit__(self, *args: Any, **kwargs: Any) -> None:
106        """Exit a context manager for internal httpx.Client (see httpx docs)"""
107        self.get_httpx_client().__exit__(*args, **kwargs)
108
109    def set_async_httpx_client(self, async_client: httpx.AsyncClient) -> "Client":
110        """Manually the underlying httpx.AsyncClient
111
112        **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
113        """
114        self._async_client = async_client
115        return self
116
117    def get_async_httpx_client(self) -> httpx.AsyncClient:
118        """Get the underlying httpx.AsyncClient, constructing a new one if not previously set"""
119        if self._async_client is None:
120            self._async_client = httpx.AsyncClient(
121                base_url=self._base_url,
122                cookies=self._cookies,
123                headers=self._headers,
124                timeout=self._timeout,
125                verify=self._verify_ssl,
126                follow_redirects=self._follow_redirects,
127                **self._httpx_args,
128            )
129        return self._async_client
130
131    async def __aenter__(self) -> "Client":
132        """Enter a context manager for underlying httpx.AsyncClient—you cannot enter twice (see httpx docs)"""
133        await self.get_async_httpx_client().__aenter__()
134        return self
135
136    async def __aexit__(self, *args: Any, **kwargs: Any) -> None:
137        """Exit a context manager for underlying httpx.AsyncClient (see httpx docs)"""
138        await self.get_async_httpx_client().__aexit__(*args, **kwargs)
@define
class Client:
  9@define
 10class Client:
 11    """A class for keeping track of data related to the API
 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
 17        ``cookies``: A dictionary of cookies to be sent with every request
 18
 19        ``headers``: A dictionary of headers to be sent with every request
 20
 21        ``timeout``: The maximum amount of a time a request can take. API functions will raise
 22        httpx.TimeoutException if this is exceeded.
 23
 24        ``verify_ssl``: Whether or not to verify the SSL certificate of the API server. This should be True in production,
 25        but can be set to False for testing purposes.
 26
 27        ``follow_redirects``: Whether or not to follow redirects. Default value is False.
 28
 29        ``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
 30
 31
 32    Attributes:
 33        raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
 34            status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
 35            argument to the constructor.
 36
 37        auth_method: Optionally provide a method used to authenticate API requests.
 38    """
 39
 40    raise_on_unexpected_status: bool = field(default=False, kw_only=True)
 41    auth_method: httpx.Auth = field(default=None, kw_only=True)
 42    _base_url: str
 43    _cookies: Dict[str, str] = field(factory=dict, kw_only=True)
 44    _headers: Dict[str, str] = field(factory=dict, kw_only=True)
 45    _timeout: Optional[httpx.Timeout] = field(default=None, kw_only=True)
 46    _verify_ssl: Union[str, bool, ssl.SSLContext] = field(default=True, kw_only=True)
 47    _follow_redirects: bool = field(default=False, kw_only=True)
 48    _httpx_args: Dict[str, Any] = field(factory=dict, kw_only=True)
 49    _client: Optional[httpx.Client] = field(default=None, init=False)
 50    _async_client: Optional[httpx.AsyncClient] = field(default=None, init=False)
 51
 52    def get_auth(self):
 53        return self.auth_method
 54
 55    def with_headers(self, headers: Dict[str, str]) -> "Client":
 56        """Get a new client matching this one with additional headers"""
 57        if self._client is not None:
 58            self._client.headers.update(headers)
 59        if self._async_client is not None:
 60            self._async_client.headers.update(headers)
 61        return evolve(self, headers={**self._headers, **headers})
 62
 63    def with_cookies(self, cookies: Dict[str, str]) -> "Client":
 64        """Get a new client matching this one with additional cookies"""
 65        if self._client is not None:
 66            self._client.cookies.update(cookies)
 67        if self._async_client is not None:
 68            self._async_client.cookies.update(cookies)
 69        return evolve(self, cookies={**self._cookies, **cookies})
 70
 71    def with_timeout(self, timeout: httpx.Timeout) -> "Client":
 72        """Get a new client matching this one with a new timeout (in seconds)"""
 73        if self._client is not None:
 74            self._client.timeout = timeout
 75        if self._async_client is not None:
 76            self._async_client.timeout = timeout
 77        return evolve(self, timeout=timeout)
 78
 79    def set_httpx_client(self, client: httpx.Client) -> "Client":
 80        """Manually the underlying httpx.Client
 81
 82        **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
 83        """
 84        self._client = client
 85        return self
 86
 87    def get_httpx_client(self) -> httpx.Client:
 88        """Get the underlying httpx.Client, constructing a new one if not previously set"""
 89        if self._client is None:
 90            self._client = httpx.Client(
 91                base_url=self._base_url,
 92                cookies=self._cookies,
 93                headers=self._headers,
 94                timeout=self._timeout,
 95                verify=self._verify_ssl,
 96                follow_redirects=self._follow_redirects,
 97                **self._httpx_args,
 98            )
 99        return self._client
100
101    def __enter__(self) -> "Client":
102        """Enter a context manager for self.client—you cannot enter twice (see httpx docs)"""
103        self.get_httpx_client().__enter__()
104        return self
105
106    def __exit__(self, *args: Any, **kwargs: Any) -> None:
107        """Exit a context manager for internal httpx.Client (see httpx docs)"""
108        self.get_httpx_client().__exit__(*args, **kwargs)
109
110    def set_async_httpx_client(self, async_client: httpx.AsyncClient) -> "Client":
111        """Manually the underlying httpx.AsyncClient
112
113        **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
114        """
115        self._async_client = async_client
116        return self
117
118    def get_async_httpx_client(self) -> httpx.AsyncClient:
119        """Get the underlying httpx.AsyncClient, constructing a new one if not previously set"""
120        if self._async_client is None:
121            self._async_client = httpx.AsyncClient(
122                base_url=self._base_url,
123                cookies=self._cookies,
124                headers=self._headers,
125                timeout=self._timeout,
126                verify=self._verify_ssl,
127                follow_redirects=self._follow_redirects,
128                **self._httpx_args,
129            )
130        return self._async_client
131
132    async def __aenter__(self) -> "Client":
133        """Enter a context manager for underlying httpx.AsyncClient—you cannot enter twice (see httpx docs)"""
134        await self.get_async_httpx_client().__aenter__()
135        return self
136
137    async def __aexit__(self, *args: Any, **kwargs: Any) -> None:
138        """Exit a context manager for underlying httpx.AsyncClient (see httpx docs)"""
139        await self.get_async_httpx_client().__aexit__(*args, **kwargs)

A class for keeping track of data related to the API

The following are accepted as keyword arguments and will be used to construct httpx Clients internally:

base_url: The base URL for the API, all requests are made to a relative path to this URL

cookies: A dictionary of cookies to be sent with every request

headers: A dictionary of headers to be sent with every request

timeout: The maximum amount of a time a request can take. API functions will raise httpx.TimeoutException if this is exceeded.

verify_ssl: Whether or not to verify the SSL certificate of the API server. This should be True in production, but can be set to False for testing purposes.

follow_redirects: Whether or not to follow redirects. Default value is False.

httpx_args: A dictionary of additional arguments to be passed to the httpx.Client and httpx.AsyncClient constructor.

Attributes:
  • raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a status code that was not documented in the source OpenAPI document. Can also be provided as a keyword argument to the constructor.
  • auth_method: Optionally provide a method used to authenticate API requests.
Client( base_url: str, *, raise_on_unexpected_status: bool = False, auth_method: httpx.Auth = None, cookies: Dict[str, str] = NOTHING, headers: Dict[str, str] = NOTHING, timeout: Optional[httpx.Timeout] = None, verify_ssl: Union[str, bool, ssl.SSLContext] = True, follow_redirects: bool = False, httpx_args: Dict[str, Any] = NOTHING)
 2def __init__(self, base_url, *, raise_on_unexpected_status=attr_dict['raise_on_unexpected_status'].default, auth_method=attr_dict['auth_method'].default, cookies=NOTHING, headers=NOTHING, timeout=attr_dict['_timeout'].default, verify_ssl=attr_dict['_verify_ssl'].default, follow_redirects=attr_dict['_follow_redirects'].default, httpx_args=NOTHING):
 3    self.raise_on_unexpected_status = raise_on_unexpected_status
 4    self.auth_method = auth_method
 5    self._base_url = base_url
 6    if cookies is not NOTHING:
 7        self._cookies = cookies
 8    else:
 9        self._cookies = __attr_factory__cookies()
10    if headers is not NOTHING:
11        self._headers = headers
12    else:
13        self._headers = __attr_factory__headers()
14    self._timeout = timeout
15    self._verify_ssl = verify_ssl
16    self._follow_redirects = follow_redirects
17    if httpx_args is not NOTHING:
18        self._httpx_args = httpx_args
19    else:
20        self._httpx_args = __attr_factory__httpx_args()
21    self._client = attr_dict['_client'].default
22    self._async_client = attr_dict['_async_client'].default

Method generated by attrs for class Client.

raise_on_unexpected_status: bool
auth_method: httpx.Auth
def get_auth(self):
52    def get_auth(self):
53        return self.auth_method
def with_headers(self, headers: Dict[str, str]) -> Client:
55    def with_headers(self, headers: Dict[str, str]) -> "Client":
56        """Get a new client matching this one with additional headers"""
57        if self._client is not None:
58            self._client.headers.update(headers)
59        if self._async_client is not None:
60            self._async_client.headers.update(headers)
61        return evolve(self, headers={**self._headers, **headers})

Get a new client matching this one with additional headers

def with_cookies(self, cookies: Dict[str, str]) -> Client:
63    def with_cookies(self, cookies: Dict[str, str]) -> "Client":
64        """Get a new client matching this one with additional cookies"""
65        if self._client is not None:
66            self._client.cookies.update(cookies)
67        if self._async_client is not None:
68            self._async_client.cookies.update(cookies)
69        return evolve(self, cookies={**self._cookies, **cookies})

Get a new client matching this one with additional cookies

def with_timeout(self, timeout: httpx.Timeout) -> Client:
71    def with_timeout(self, timeout: httpx.Timeout) -> "Client":
72        """Get a new client matching this one with a new timeout (in seconds)"""
73        if self._client is not None:
74            self._client.timeout = timeout
75        if self._async_client is not None:
76            self._async_client.timeout = timeout
77        return evolve(self, timeout=timeout)

Get a new client matching this one with a new timeout (in seconds)

def set_httpx_client(self, client: httpx.Client) -> Client:
79    def set_httpx_client(self, client: httpx.Client) -> "Client":
80        """Manually the underlying httpx.Client
81
82        **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
83        """
84        self._client = client
85        return self

Manually the underlying httpx.Client

NOTE: This will override any other settings on the client, including cookies, headers, and timeout.

def get_httpx_client(self) -> httpx.Client:
87    def get_httpx_client(self) -> httpx.Client:
88        """Get the underlying httpx.Client, constructing a new one if not previously set"""
89        if self._client is None:
90            self._client = httpx.Client(
91                base_url=self._base_url,
92                cookies=self._cookies,
93                headers=self._headers,
94                timeout=self._timeout,
95                verify=self._verify_ssl,
96                follow_redirects=self._follow_redirects,
97                **self._httpx_args,
98            )
99        return self._client

Get the underlying httpx.Client, constructing a new one if not previously set

def set_async_httpx_client( self, async_client: httpx.AsyncClient) -> Client:
110    def set_async_httpx_client(self, async_client: httpx.AsyncClient) -> "Client":
111        """Manually the underlying httpx.AsyncClient
112
113        **NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
114        """
115        self._async_client = async_client
116        return self

Manually the underlying httpx.AsyncClient

NOTE: This will override any other settings on the client, including cookies, headers, and timeout.

def get_async_httpx_client(self) -> httpx.AsyncClient:
118    def get_async_httpx_client(self) -> httpx.AsyncClient:
119        """Get the underlying httpx.AsyncClient, constructing a new one if not previously set"""
120        if self._async_client is None:
121            self._async_client = httpx.AsyncClient(
122                base_url=self._base_url,
123                cookies=self._cookies,
124                headers=self._headers,
125                timeout=self._timeout,
126                verify=self._verify_ssl,
127                follow_redirects=self._follow_redirects,
128                **self._httpx_args,
129            )
130        return self._async_client

Get the underlying httpx.AsyncClient, constructing a new one if not previously set