Skip to content

Commit a2e0086

Browse files
Increase timeout for HTTPX to accomodate slower requests
1 parent 8f075b0 commit a2e0086

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/service_ml_forecast/clients/openremote/openremote_client.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class OpenRemoteClient:
5858
keycloak_url: The URL of the Keycloak API.
5959
service_user: The service user for the OpenRemote API.
6060
service_user_secret: The service user secret for the OpenRemote API.
61+
timeout: Timeout in seconds for HTTP requests. Defaults to 30 seconds.
6162
6263
Raises:
6364
Exception: If the authentication fails
@@ -71,13 +72,15 @@ def __init__(
7172
keycloak_url: str,
7273
service_user: str,
7374
service_user_secret: str,
75+
timeout: float = 60.0,
7476
):
7577
self.openremote_url: str = openremote_url
7678
self.keycloak_url: str = keycloak_url
7779
self.service_user: str = service_user
7880
self.service_user_secret: str = service_user_secret
7981
self.oauth_token: OAuthTokenResponse | None = None
8082
self.token_expiration_timestamp: float | None = None
83+
self.timeout: float = timeout
8184

8285
self.__authenticate()
8386

@@ -98,7 +101,7 @@ def __get_token(self) -> OAuthTokenResponse | None:
98101
client_secret=self.service_user_secret,
99102
)
100103

101-
with httpx.Client() as client:
104+
with httpx.Client(timeout=self.timeout) as client:
102105
try:
103106
response = client.post(url, data=data.model_dump())
104107
response.raise_for_status()
@@ -136,7 +139,7 @@ def health_check(self) -> bool:
136139
url = f"{self.openremote_url}/api/master/health"
137140

138141
request = self.__build_request("GET", url)
139-
with httpx.Client() as client:
142+
with httpx.Client(timeout=self.timeout) as client:
140143
try:
141144
response = client.send(request)
142145
response.raise_for_status()
@@ -161,7 +164,7 @@ def retrieve_asset_datapoint_period(self, asset_id: str, attribute_name: str) ->
161164

162165
request = self.__build_request("GET", url)
163166

164-
with httpx.Client() as client:
167+
with httpx.Client(timeout=self.timeout) as client:
165168
try:
166169
response = client.send(request)
167170
response.raise_for_status()
@@ -199,7 +202,7 @@ def retrieve_historical_datapoints(
199202

200203
request = self.__build_request("POST", url, data=request_body.model_dump())
201204

202-
with httpx.Client() as client:
205+
with httpx.Client(timeout=self.timeout) as client:
203206
try:
204207
response = client.send(request)
205208
response.raise_for_status()
@@ -228,7 +231,7 @@ def write_predicted_datapoints(self, asset_id: str, attribute_name: str, datapoi
228231

229232
request = self.__build_request("PUT", url, data=datapoints_json)
230233

231-
with httpx.Client() as client:
234+
with httpx.Client(timeout=self.timeout) as client:
232235
try:
233236
response = client.send(request)
234237
response.raise_for_status()
@@ -266,7 +269,7 @@ def retrieve_predicted_datapoints(
266269

267270
request = self.__build_request("POST", url, data=request_body.model_dump())
268271

269-
with httpx.Client() as client:
272+
with httpx.Client(timeout=self.timeout) as client:
270273
try:
271274
response = client.send(request)
272275
response.raise_for_status()
@@ -317,7 +320,7 @@ def retrieve_assets_with_historical_datapoints(self, realm: str) -> list[Asset]
317320

318321
request = self.__build_request("POST", url, data=asset_query)
319322

320-
with httpx.Client() as client:
323+
with httpx.Client(timeout=self.timeout) as client:
321324
try:
322325
response = client.send(request)
323326
response.raise_for_status()
@@ -355,7 +358,7 @@ def retrieve_assets_by_ids(self, asset_ids: list[str], realm: str) -> list[Asset
355358

356359
request = self.__build_request("POST", url, data=asset_query)
357360

358-
with httpx.Client() as client:
361+
with httpx.Client(timeout=self.timeout) as client:
359362
try:
360363
response = client.send(request)
361364
response.raise_for_status()
@@ -374,7 +377,7 @@ def retrieve_manager_config(self) -> ManagerConfig | None:
374377
url = f"{self.openremote_url}/api/master/configuration/manager"
375378
request = self.__build_request("GET", url)
376379

377-
with httpx.Client() as client:
380+
with httpx.Client(timeout=self.timeout) as client:
378381
try:
379382
request.headers.pop("Authorization")
380383
response = client.send(request)
@@ -395,7 +398,7 @@ def retrieve_accessible_realms(self, realm: str) -> list[BasicRealm] | None:
395398
url = f"{self.openremote_url}/api/{realm}/realm/accessible"
396399
request = self.__build_request("GET", url)
397400

398-
with httpx.Client() as client:
401+
with httpx.Client(timeout=self.timeout) as client:
399402
try:
400403
response = client.send(request)
401404
response.raise_for_status()
@@ -415,7 +418,7 @@ def retrieve_all_realms(self) -> list[Realm] | None:
415418
url = f"{self.openremote_url}/api/master/realm"
416419
request = self.__build_request("GET", url)
417420

418-
with httpx.Client() as client:
421+
with httpx.Client(timeout=self.timeout) as client:
419422
try:
420423
response = client.send(request)
421424
response.raise_for_status()

src/service_ml_forecast/middlewares/keycloak_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async def _get_jwks(issuer: str, kid: str) -> dict[str, Any]:
134134
jwks_url = f"{issuer}/protocol/openid-connect/certs"
135135

136136
try:
137-
async with httpx.AsyncClient(verify=ENV.ML_VERIFY_SSL) as client:
137+
async with httpx.AsyncClient(verify=ENV.ML_VERIFY_SSL, timeout=10.0) as client:
138138
response = await client.get(jwks_url)
139139
response.raise_for_status()
140140
return cast(dict[str, Any], response.json())

0 commit comments

Comments
 (0)