|
2 | 2 | from http import HTTPStatus |
3 | 3 |
|
4 | 4 | import httpx |
| 5 | +from httpx import Timeout |
5 | 6 |
|
6 | 7 | from adminui.constants import BRIDGE_SOCKET |
7 | 8 |
|
| 9 | +# increasing httpx default (5s) as processing on host can be slow on older devices |
| 10 | +DEFAULT_TIMEOUT = Timeout(timeout=20.0) |
| 11 | + |
8 | 12 |
|
9 | 13 | @dataclass |
10 | 14 | class BridgeResponse: |
@@ -46,9 +50,11 @@ def __init__(self): |
46 | 50 | # custom transport over UDS |
47 | 51 | self.transport = httpx.HTTPTransport(uds=str(BRIDGE_SOCKET)) |
48 | 52 |
|
49 | | - def do_request(self, path: str) -> BridgeResponse: |
| 53 | + def do_request( |
| 54 | + self, path: str, timeout: Timeout = DEFAULT_TIMEOUT |
| 55 | + ) -> BridgeResponse: |
50 | 56 | try: |
51 | | - with httpx.Client(transport=self.transport) as client: |
| 57 | + with httpx.Client(transport=self.transport, timeout=timeout) as client: |
52 | 58 | response = client.get(f"{self.api_url}{path}") |
53 | 59 |
|
54 | 60 | if response.status_code != 200: |
@@ -82,7 +88,10 @@ def request_restart(self, after_seconds: int) -> BridgeResponse: |
82 | 88 | def request_service_toggle(self, name: str, enable: bool) -> BridgeResponse: |
83 | 89 | """enable (not start) systemd service""" |
84 | 90 | action = "enable" if enable else "disable" |
85 | | - return self.do_request(path=f"/toggle-service/{action}/{name}") |
| 91 | + return self.do_request( |
| 92 | + path=f"/toggle-service/{action}/{name}", |
| 93 | + timeout=Timeout(timeout=20.0, read=60.0), |
| 94 | + ) |
86 | 95 |
|
87 | 96 | def request_service_enabled(self, name: str) -> BridgeResponse: |
88 | 97 | return self.do_request(path=f"/service-is-enabled/{name}") |
|
0 commit comments