Skip to content

Commit

Permalink
using dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrecuer committed Jun 18, 2024
1 parent dc05d33 commit 6421b5c
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions pyemoncms/emoncms_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""

import asyncio
from dataclasses import dataclass
import logging
from typing import Any, TypeVar

Expand All @@ -32,20 +33,16 @@

Self = TypeVar("Self", bound="EmoncmsClient")


@dataclass
class EmoncmsClient:
"""Emoncms client."""

logger = logging.getLogger(__name__)
url: str = None
api_key: str = None
request_timeout: int = 20
session: ClientSession | None = None
_close_session: bool = False

def __init__(self, url: str, api_key: str, request_timeout: int = 20) -> None:
"""Initialize the client."""
self.logger.info("Initializing Emoncms client")
self.api_key = api_key
self.url = url
self.request_timeout = request_timeout
logger = logging.getLogger(__name__)

async def async_request(
self, path: str, params: dict[str, Any] | None = None
Expand All @@ -57,11 +54,13 @@ async def async_request(
if not params:
params = {"apikey": self.api_key}
if self.session is None:
self.session = ClientSession(self.url)
self.session = ClientSession()
self._close_session = True
path = path.lstrip('/')
url= f'{self.url}/{path}'
try:
response = await self.session.get(
path, timeout=self.request_timeout, params=params
url, timeout=self.request_timeout, params=params
)
except ClientError as er:
message = f"client error : {er}"
Expand Down

0 comments on commit 6421b5c

Please sign in to comment.