Skip to content

Commit

Permalink
add timeout to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
atimin committed Jun 5, 2024
1 parent e733ca6 commit 36b3503
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 10 deletions.
4 changes: 3 additions & 1 deletion examples/get_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

def main():
# Init
drift_client = DriftClient("drift-test-rig.local", os.getenv("DRIFT_PASSWORD"))
drift_client = DriftClient(
"192.168.1.25", os.getenv("DRIFT_PASSWORD"), timeout=100.0
)
metrics = drift_client.get_metrics(
"energy-distr-1",
start=datetime.datetime.now() - datetime.timedelta(minutes=15),
Expand Down
4 changes: 4 additions & 0 deletions pkg/drift_client/drift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, host: str, password: str, **kwargs):
influx_port (int): InfluxDB port. Default: 8086,
mqtt_port (int): MQTT port. Default: 1883
loop: asyncio loop for integration into async code
timeout (float): Timeout for requests. Default: 30 seconds
"""
if password is None or password == "":
raise ValueError("Password is required")
Expand All @@ -68,6 +69,7 @@ def __init__(self, host: str, password: str, **kwargs):
)
mqtt_port = kwargs["mqtt_port"] if "mqtt_port" in kwargs else 1883
loop = kwargs["loop"] if "loop" in kwargs else None
timeout = kwargs["timeout"] if "timeout" in kwargs else 30

self._mqtt_client = MQTTClient(
f"mqtt://{host}:{mqtt_port}",
Expand All @@ -79,12 +81,14 @@ def __init__(self, host: str, password: str, **kwargs):
org,
password,
False,
timeout,
) # TBD!!! --> SSL handling!

try:
self._blob_storage = ReductStoreClient(
f"{('https://' if secure else 'http://')}{host}:{reduct_storage_port}",
password,
timeout,
loop,
)
return
Expand Down
1 change: 1 addition & 0 deletions pkg/drift_client/drift_data_package.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Wrapper around DriftPackage"""

from typing import Optional, Dict

from drift_bytes import Variant, InputBuffer
Expand Down
9 changes: 2 additions & 7 deletions pkg/drift_client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
class InfluxDBClient:
"""Wrapper around `InfluxDBClient`"""

def __init__(
self,
uri: str,
org: str,
token: str,
secure: bool,
):
def __init__(self, uri: str, org: str, token: str, secure: bool, timeout: float):
"""Create Client for InfluxDB access
:param uri: URI, format: <protocol>://<host>:<port>
Expand All @@ -34,6 +28,7 @@ def __init__(
org=org,
token=token,
verify_ssl=secure,
timeout=int(timeout * 1000),
)
self.__query_api = self.__client.query_api()
self.__bucket = "data"
Expand Down
5 changes: 3 additions & 2 deletions pkg/drift_client/reduct_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Reduct Storage client"""

import asyncio
from asyncio import new_event_loop
from typing import Tuple, List, Optional, Dict, Iterator
Expand All @@ -11,14 +12,14 @@
class ReductStoreClient:
"""Wrapper around ReductStore client"""

def __init__(self, url: str, token: str, loop=None):
def __init__(self, url: str, token: str, timeout: float, loop=None):
"""
Args:
url: ReductStore URL
token: ReductStore API token
loop: asyncio event loop
"""
self._client = Client(url, api_token=token)
self._client = Client(url, api_token=token, timeout=timeout)
self._bucket = "data"
self._loop = loop if loop else new_event_loop()
_ = self._run(self._client.info()) # check connection for fallback to Minio
Expand Down
1 change: 1 addition & 0 deletions tests/drift_client_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for DriftClient"""

from datetime import datetime
from typing import Optional, List, Any

Expand Down
1 change: 1 addition & 0 deletions tests/influxdb_client_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""InfluxDB Client"""

from datetime import datetime

import pytest
Expand Down
1 change: 1 addition & 0 deletions tests/minio_client_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Minio Client"""

import pytest

from minio.error import S3Error
Expand Down
1 change: 1 addition & 0 deletions tests/package_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for Package"""

from typing import Dict

# pylint: disable=no-member
Expand Down
1 change: 1 addition & 0 deletions tests/reduct_client_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Reduct Storage Client"""

from typing import Optional, List, Any

import pytest
Expand Down

0 comments on commit 36b3503

Please sign in to comment.