diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index 15e93dd..73b7dda 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -32,6 +32,9 @@ jobs: pip install build - name: Build package run: python -m build + - name: Verify py.typed + run: | + unzip -l dist/*.whl | grep 'pyemoncms/py.typed' - name: Publish package uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 with: diff --git a/README.md b/README.md index f1c68b2..a02e266 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ import logging import asyncio from pyemoncms import EmoncmsClient +logging.basicConfig() key = "your32bitsAPIkey" url = "http://url:8081" client = EmoncmsClient(url, key, request_timeout=1) diff --git a/pyemoncms/emoncms_client.py b/pyemoncms/emoncms_client.py index f6abe68..9d91e70 100644 --- a/pyemoncms/emoncms_client.py +++ b/pyemoncms/emoncms_client.py @@ -12,13 +12,15 @@ so there is a type error if you search for a key in the response """ +from __future__ import annotations + import asyncio from dataclasses import dataclass import json import logging -from typing import Any, Dict, TypeVar, Optional +from typing import Any, Dict, TypeVar, Optional, cast -from aiohttp import ClientError, ClientSession +from aiohttp import ClientError, ClientSession, ClientTimeout HTTP_STATUS = { 400: "invalid request", @@ -30,8 +32,6 @@ MESSAGE_KEY = "message" SUCCESS_KEY = "success" -logging.basicConfig() - Self = TypeVar("Self", bound="EmoncmsClient") @@ -51,8 +51,8 @@ class EmoncmsConnectionError(EmoncmsError): class EmoncmsClient: """Emoncms client.""" - url: str = None - api_key: str = None + url: str = "" + api_key: str | None = None request_timeout: int = 20 session: ClientSession | None = None _close_session: bool = False @@ -75,7 +75,7 @@ async def async_request( url= f'{self.url}/{path}' try: response = await self.session.get( - url, timeout=self.request_timeout, params=params + url, timeout=ClientTimeout(total=self.request_timeout), params=params ) except ClientError as er: message = f"client error : {er}" @@ -143,7 +143,7 @@ async def async_input_post( try: async with self.session.post( - endpoint, params=params, timeout=self.request_timeout + endpoint, params=params, timeout=ClientTimeout(total=self.request_timeout) ) as resp: text = await resp.text() status = resp.status @@ -168,7 +168,7 @@ async def async_get_uuid(self) -> str | None: if json_response is False: return None if json_response[SUCCESS_KEY]: - return json_response[MESSAGE_KEY] + return cast(str, json_response[MESSAGE_KEY]) message = json_response[MESSAGE_KEY] message = f"{message} : {self.url}" self.logger.debug(message) @@ -178,15 +178,15 @@ async def async_list_feeds(self) -> list[dict[str, Any]] | None: """Request emoncms feeds list.""" feed_data = await self.async_request("/feed/list.json") if feed_data[SUCCESS_KEY]: - return feed_data[MESSAGE_KEY] + return cast(list[dict[str, Any]], feed_data[MESSAGE_KEY]) return None - async def async_get_feed_fields(self, feed_id: int) -> list[str, Any] | None: + async def async_get_feed_fields(self, feed_id: int) -> list[Any] | None: """Get all fields for a single feed.""" params = {"apikey": self.api_key, "id": feed_id} feed_data = await self.async_request("/feed/aget.json", params=params) if feed_data[SUCCESS_KEY]: - return feed_data[MESSAGE_KEY] + return cast(list[Any], feed_data[MESSAGE_KEY]) return None async def close(self) -> None: @@ -194,7 +194,7 @@ async def close(self) -> None: if self.session and self._close_session: await self.session.close() - async def __aenter__(self) -> Self: + async def __aenter__(self: Self) -> Self: """Async enter for context manager.""" return self diff --git a/pyemoncms/py.typed b/pyemoncms/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py index 1e645e9..33e181e 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,7 @@ "Operating System :: OS Independent", ], packages=setuptools.find_packages(), + package_data={"pyemoncms": ["py.typed"]}, install_requires=[ 'aiohttp' ],