Skip to content

Commit c9eae42

Browse files
committed
http: api: use httpx.Auth to implement authentication
1 parent 1515377 commit c9eae42

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

src/enapter/http/api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from .auth import Auth
12
from .client import Client
23
from .config import Config
34
from .errors import Error, MultiError, check_error
45

56
from . import devices, sites, commands, blueprints # isort: skip
67

78
__all__ = [
9+
"Auth",
810
"Client",
911
"Config",
1012
"devices",

src/enapter/http/api/auth.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Generator
2+
3+
import httpx
4+
5+
6+
class Auth(httpx.Auth):
7+
8+
def __init__(self, token: str) -> None:
9+
self.token = token
10+
11+
def auth_flow(
12+
self, request: httpx.Request
13+
) -> Generator[httpx.Request, httpx.Response, None]:
14+
request.headers["X-Enapter-Auth-Token"] = self.token
15+
yield request

src/enapter/http/api/client.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,51 @@
44

55
from enapter.http.api import blueprints, commands, devices, sites, telemetry
66

7+
from .auth import Auth
78
from .config import Config
89

910

1011
class Client:
1112

1213
def __init__(self, config: Config) -> None:
1314
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()
1520

1621
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"
2122
return httpx.AsyncClient(
22-
headers=headers,
23+
auth=self._auth,
24+
headers=self._headers,
2325
base_url=self._config.base_url,
26+
transport=self._transport,
2427
)
2528

2629
async def __aenter__(self) -> Self:
27-
await self._client.__aenter__()
30+
await self._transport.__aenter__()
2831
return self
2932

3033
async def __aexit__(self, *exc) -> None:
31-
await self._client.__aexit__(*exc)
34+
await self._transport.__aexit__(*exc)
3235

3336
@property
3437
def devices(self) -> devices.Client:
35-
return devices.Client(client=self._client)
38+
return devices.Client(client=self._new_client())
3639

3740
@property
3841
def sites(self) -> sites.Client:
39-
return sites.Client(client=self._client)
42+
return sites.Client(client=self._new_client())
4043

4144
@property
4245
def commands(self) -> commands.Client:
43-
return commands.Client(client=self._client)
46+
return commands.Client(client=self._new_client())
4447

4548
@property
4649
def blueprints(self) -> blueprints.Client:
47-
return blueprints.Client(client=self._client)
50+
return blueprints.Client(client=self._new_client())
4851

4952
@property
5053
def telemetry(self) -> telemetry.Client:
51-
return telemetry.Client(client=self._client)
54+
return telemetry.Client(client=self._new_client())

0 commit comments

Comments
 (0)