Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 13 additions & 13 deletions pyemoncms/emoncms_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -30,8 +32,6 @@
MESSAGE_KEY = "message"
SUCCESS_KEY = "success"

logging.basicConfig()

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


Expand All @@ -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
Expand All @@ -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}"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -178,23 +178,23 @@ 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:
"""Close open client session."""
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

Expand Down
Empty file added pyemoncms/py.typed
Empty file.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"Operating System :: OS Independent",
],
packages=setuptools.find_packages(),
package_data={"pyemoncms": ["py.typed"]},
install_requires=[
'aiohttp'
],
Expand Down
Loading