|
4 | 4 |
|
5 | 5 | from enapter.http.api import blueprints, commands, devices, sites, telemetry |
6 | 6 |
|
| 7 | +from .auth import Auth |
7 | 8 | from .config import Config |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class Client: |
11 | 12 |
|
12 | 13 | def __init__(self, config: Config) -> None: |
13 | 14 | self._config = config |
14 | | - self._client = self._new_client() |
| 15 | + self._auth = Auth(token=self._config.token) |
| 16 | + self._headers = {} |
| 17 | + if self._config.allow_http: |
| 18 | + self._headers["X-Enapter-Allow-HTTP"] = "true" |
| 19 | + self._transport = httpx.AsyncHTTPTransport() |
15 | 20 |
|
16 | 21 | def _new_client(self) -> httpx.AsyncClient: |
17 | | - assert self._config.base_url is not None |
18 | | - headers = {"X-Enapter-Auth-Token": self._config.token} |
19 | | - if self._config.allow_http: |
20 | | - headers["X-Enapter-Allow-HTTP"] = "true" |
21 | 22 | return httpx.AsyncClient( |
22 | | - headers=headers, |
| 23 | + auth=self._auth, |
| 24 | + headers=self._headers, |
23 | 25 | base_url=self._config.base_url, |
| 26 | + transport=self._transport, |
24 | 27 | ) |
25 | 28 |
|
26 | 29 | async def __aenter__(self) -> Self: |
27 | | - await self._client.__aenter__() |
| 30 | + await self._transport.__aenter__() |
28 | 31 | return self |
29 | 32 |
|
30 | 33 | async def __aexit__(self, *exc) -> None: |
31 | | - await self._client.__aexit__(*exc) |
| 34 | + await self._transport.__aexit__(*exc) |
32 | 35 |
|
33 | 36 | @property |
34 | 37 | def devices(self) -> devices.Client: |
35 | | - return devices.Client(client=self._client) |
| 38 | + return devices.Client(client=self._new_client()) |
36 | 39 |
|
37 | 40 | @property |
38 | 41 | def sites(self) -> sites.Client: |
39 | | - return sites.Client(client=self._client) |
| 42 | + return sites.Client(client=self._new_client()) |
40 | 43 |
|
41 | 44 | @property |
42 | 45 | def commands(self) -> commands.Client: |
43 | | - return commands.Client(client=self._client) |
| 46 | + return commands.Client(client=self._new_client()) |
44 | 47 |
|
45 | 48 | @property |
46 | 49 | def blueprints(self) -> blueprints.Client: |
47 | | - return blueprints.Client(client=self._client) |
| 50 | + return blueprints.Client(client=self._new_client()) |
48 | 51 |
|
49 | 52 | @property |
50 | 53 | def telemetry(self) -> telemetry.Client: |
51 | | - return telemetry.Client(client=self._client) |
| 54 | + return telemetry.Client(client=self._new_client()) |
0 commit comments