Skip to content

Commit f347dec

Browse files
committed
http: api: ensure response content is read in check_error
1 parent cd987a3 commit f347dec

6 files changed

Lines changed: 26 additions & 25 deletions

File tree

src/enapter/http/api/blueprints/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, client: httpx.AsyncClient) -> None:
1717
async def get(self, blueprint_id: str) -> Blueprint:
1818
url = f"v3/blueprints/{blueprint_id}"
1919
response = await self._client.get(url)
20-
api.check_error(response)
20+
await api.check_error(response)
2121
return Blueprint.from_dto(response.json()["blueprint"])
2222

2323
async def upload_file(self, path: pathlib.Path) -> Blueprint:
@@ -32,15 +32,15 @@ async def upload_directory(self, path: pathlib.Path) -> Blueprint:
3232
async def upload(self, data: bytes) -> Blueprint:
3333
url = "v3/blueprints/upload"
3434
response = await self._client.post(url, content=data)
35-
api.check_error(response)
35+
await api.check_error(response)
3636
return Blueprint.from_dto(response.json()["blueprint"])
3737

3838
async def download(
3939
self, blueprint_id: str, view: BlueprintView = BlueprintView.ORIGINAL
4040
) -> bytes:
4141
url = f"v3/blueprints/{blueprint_id}/zip"
4242
response = await self._client.get(url, params={"view": view.value})
43-
api.check_error(response)
43+
await api.check_error(response)
4444
return response.content
4545

4646
async def validate_file(self, path: pathlib.Path) -> None:
@@ -55,7 +55,7 @@ async def validate_directory(self, path: pathlib.Path) -> None:
5555
async def validate(self, data: bytes) -> None:
5656
url = "v3/blueprints/validate"
5757
response = await self._client.post(url, content=data)
58-
api.check_error(response)
58+
await api.check_error(response)
5959
validation_errors = response.json().get("validation_errors", [])
6060
if validation_errors:
6161
raise api.MultiError(

src/enapter/http/api/commands/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def get_execution(
2020
expand = {"log": expand_log}
2121
expand_string = ",".join(k for k, v in expand.items() if v)
2222
response = await self._client.get(url, params={"expand": expand_string})
23-
api.check_error(response)
23+
await api.check_error(response)
2424
return Execution.from_dto(response.json()["execution"])
2525

2626
@async_.generator
@@ -36,7 +36,7 @@ async def list_executions(
3636
response = await self._client.get(
3737
url, params={"order": order.value, "limit": limit, "offset": offset}
3838
)
39-
api.check_error(response)
39+
await api.check_error(response)
4040
payload = response.json()
4141
if not payload["executions"]:
4242
return
@@ -61,7 +61,7 @@ async def execute(
6161
params={"expand": expand_string},
6262
json={"name": name, "arguments": arguments},
6363
)
64-
api.check_error(response)
64+
await api.check_error(response)
6565
return Execution.from_dto(response.json()["execution"])
6666

6767
async def create_execution(
@@ -73,7 +73,7 @@ async def create_execution(
7373
response = await self._client.post(
7474
url, json={"name": name, "arguments": arguments}
7575
)
76-
api.check_error(response)
76+
await api.check_error(response)
7777
return await self.get_execution(
7878
device_id=device_id, execution_id=response.json()["execution_id"]
7979
)

src/enapter/http/api/devices/client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async def create_standalone(
3030
response = await self._client.post(
3131
url, json={"name": name, "site_id": site_id, "slug": slug}
3232
)
33-
api.check_error(response)
33+
await api.check_error(response)
3434
return await self.get(device_id=response.json()["device_id"])
3535

3636
async def create_vucm(
@@ -47,7 +47,7 @@ async def create_vucm(
4747
response = await self._client.post(
4848
url, json={"name": name, "hardware_id": hardware_id, "site_id": site_id}
4949
)
50-
api.check_error(response)
50+
await api.check_error(response)
5151
return await self.get(
5252
device_id=response.json()["device_id"], expand_communication=True
5353
)
@@ -71,7 +71,7 @@ async def create_lua(
7171
"slug": slug,
7272
},
7373
)
74-
api.check_error(response)
74+
await api.check_error(response)
7575
return await self.get(device_id=response.json()["device_id"])
7676

7777
async def get(
@@ -91,7 +91,7 @@ async def get(
9191
}
9292
expand_string = ",".join(k for k, v in expand.items() if v)
9393
response = await self._client.get(url, params={"expand": expand_string})
94-
api.check_error(response)
94+
await api.check_error(response)
9595
return Device.from_dto(response.json()["device"])
9696

9797
@async_.generator
@@ -117,7 +117,7 @@ async def list(
117117
response = await self._client.get(
118118
url, params={"expand": expand_string, "limit": limit, "offset": offset}
119119
)
120-
api.check_error(response)
120+
await api.check_error(response)
121121
payload = response.json()
122122
if not payload["devices"]:
123123
return
@@ -132,26 +132,26 @@ async def update(
132132
return await self.get(device_id)
133133
url = f"v3/devices/{device_id}"
134134
response = await self._client.patch(url, json={"name": name, "slug": slug})
135-
api.check_error(response)
135+
await api.check_error(response)
136136
return Device.from_dto(response.json()["device"])
137137

138138
async def delete(self, device_id: str) -> None:
139139
url = f"v3/devices/{device_id}"
140140
response = await self._client.delete(url)
141-
api.check_error(response)
141+
await api.check_error(response)
142142

143143
async def assign_blueprint(self, device_id: str, blueprint_id: str) -> Device:
144144
url = f"v3/devices/{device_id}/assign_blueprint"
145145
response = await self._client.post(url, json={"blueprint_id": blueprint_id})
146-
api.check_error(response)
146+
await api.check_error(response)
147147
return Device.from_dto(response.json()["device"])
148148

149149
async def generate_communication_config(
150150
self, device_id: str, mqtt_protocol: MQTTProtocol
151151
) -> CommunicationConfig:
152152
url = f"v3/devices/{device_id}/generate_config"
153153
response = await self._client.post(url, json={"protocol": mqtt_protocol.value})
154-
api.check_error(response)
154+
await api.check_error(response)
155155
return CommunicationConfig.from_dto(response.json()["config"])
156156

157157

src/enapter/http/api/errors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import httpx
44

55

6-
def check_error(response: httpx.Response) -> None:
6+
async def check_error(response: httpx.Response) -> None:
77
if not response.is_success:
8+
await response.aread()
89
try:
910
dto = response.json()
1011
except Exception:

src/enapter/http/api/sites/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ async def create(
2626
"location": location.to_dto() if location is not None else None,
2727
},
2828
)
29-
api.check_error(response)
29+
await api.check_error(response)
3030
return Site.from_dto(response.json()["site"])
3131

3232
async def get(self, site_id: str | None) -> Site:
3333
url = f"v3/sites/{site_id}" if site_id is not None else "v3/site"
3434
response = await self._client.get(url)
35-
api.check_error(response)
35+
await api.check_error(response)
3636
return Site.from_dto(response.json()["site"])
3737

3838
@async_.generator
@@ -44,7 +44,7 @@ async def list(self) -> AsyncGenerator[Site, None]:
4444
response = await self._client.get(
4545
url, params={"limit": limit, "offset": offset}
4646
)
47-
api.check_error(response)
47+
await api.check_error(response)
4848
payload = response.json()
4949
if not payload["sites"]:
5050
return
@@ -70,10 +70,10 @@ async def update(
7070
"location": location.to_dto() if location is not None else None,
7171
},
7272
)
73-
api.check_error(response)
73+
await api.check_error(response)
7474
return Site.from_dto(response.json()["site"])
7575

7676
async def delete(self, site_id: str) -> None:
7777
url = f"v3/sites/{site_id}"
7878
response = await self._client.delete(url)
79-
api.check_error(response)
79+
await api.check_error(response)

src/enapter/http/api/telemetry/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async def latest(
3434
relevance_interval = int(relevance_interval.total_seconds())
3535
params["relevance_interval"] = str(relevance_interval) + "s"
3636
response = await self._client.get(url, params=params)
37-
api.check_error(response)
37+
await api.check_error(response)
3838
return {
3939
device: {
4040
attribute: (
@@ -96,7 +96,7 @@ async def raw_timeseries(
9696
headers={"Accept": "text/csv"},
9797
json=payload,
9898
) as response:
99-
api.check_error(response)
99+
await api.check_error(response)
100100
data_types = [
101101
DataType(dt.strip().upper())
102102
for dt in response.headers["X-Enapter-Timeseries-Data-Types"].split(",")

0 commit comments

Comments
 (0)