pyemoncms 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyemoncms might be problematic. Click here for more details.

@@ -0,0 +1,19 @@
1
+ Copyright (c) 2021 The Python Packaging Authority
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,18 @@
1
+ Metadata-Version: 2.1
2
+ Name: pyemoncms
3
+ Version: 0.0.1
4
+ Summary: A python library to interrogate emoncms API
5
+ Home-page: https://github.com/Open-Building-Management/pyemoncms
6
+ Author: Alexandre CUER
7
+ Author-email: alexandre.cuer@wanadoo.fr
8
+ Project-URL: Bug Tracker, https://github.com/Open-Building-Management/pyemoncms/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: aiohttp
16
+
17
+ # pyemoncms
18
+ python library for emoncms API
@@ -0,0 +1,7 @@
1
+ src/__init__.py,sha256=YSa-40KVayarj_BzsDW9qpzopdlInqiImQe309CLflU,65
2
+ src/emoncms_client.py,sha256=XDA0dAYND_Mlx_wWDpQWf_18aFVWtaAKYyW2rzKD_UA,2941
3
+ pyemoncms-0.0.1.dist-info/LICENSE,sha256=A69zY8CrMUdZahSWofGPndViF6kILReG3RoK2cfIMgM,1074
4
+ pyemoncms-0.0.1.dist-info/METADATA,sha256=ZnbsxeGecugBExMEqKL7tQ8UeJCNsM467yp4Vx1MR88,620
5
+ pyemoncms-0.0.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ pyemoncms-0.0.1.dist-info/top_level.txt,sha256=74rtVfumQlgAPzR5_2CgYN24MB0XARCg0t-gzk6gTrM,4
7
+ pyemoncms-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.43.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ src
src/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ """pyemoncms module"""
2
+ from .emoncms_client import EmoncmsClient
src/emoncms_client.py ADDED
@@ -0,0 +1,89 @@
1
+ """Emoncms client."""
2
+
3
+ import asyncio
4
+ import logging
5
+ from typing import Any
6
+
7
+ import aiohttp
8
+
9
+ client_exceptions = (
10
+ aiohttp.ClientError,
11
+ asyncio.TimeoutError,
12
+ )
13
+
14
+ HTTP_STATUS = {
15
+ 400: "invalid request",
16
+ 401: "unauthorized access",
17
+ 404: "Not found",
18
+ 406: "URI not acceptable",
19
+ }
20
+
21
+ MESSAGE_KEY = "message"
22
+ SUCCESS_KEY = "success"
23
+
24
+ logging.basicConfig()
25
+
26
+
27
+ class EmoncmsClient:
28
+ """Emoncms client."""
29
+
30
+ logger = logging.getLogger(__name__)
31
+
32
+ def __init__(self, url: str, api_key: str, request_timeout: int = 20) -> None:
33
+ """Initialize the client."""
34
+ self.logger.info("Initializing Emoncms client")
35
+ self.api_key = api_key
36
+ self.url = url
37
+ self.request_timeout = request_timeout
38
+
39
+ async def async_request(
40
+ self, path: str, params: dict[str, Any] | None = None
41
+ ) -> dict[str, Any]:
42
+ """Request emoncms server."""
43
+ message = f"requesting emoncms on {path}"
44
+ self.logger.debug(message)
45
+ data = {SUCCESS_KEY: False, MESSAGE_KEY: None}
46
+ if not params:
47
+ params = {"apikey": self.api_key}
48
+ async with aiohttp.ClientSession(self.url) as session:
49
+ try:
50
+ response = await session.get(
51
+ path, timeout=self.request_timeout, params=params
52
+ )
53
+ except client_exceptions as e:
54
+ message = f"exception {e}"
55
+ data[MESSAGE_KEY] = message
56
+ self.logger.error(message)
57
+ return data
58
+ if response.status == 200:
59
+ data[SUCCESS_KEY] = True
60
+ json_response = await response.json()
61
+ data[MESSAGE_KEY] = json_response
62
+ if MESSAGE_KEY in json_response:
63
+ data[MESSAGE_KEY] = json_response[MESSAGE_KEY]
64
+ else:
65
+ message = f"error {response.status}"
66
+ if response.status in HTTP_STATUS:
67
+ message = f"{message} {HTTP_STATUS[response.status]}"
68
+ data[MESSAGE_KEY] = message
69
+ self.logger.error(message)
70
+ return data
71
+
72
+ async def async_list_feeds(self) -> list[dict[str, Any]] | None:
73
+ """Request emoncms feeds list.
74
+
75
+ return a uuid per feed if available
76
+ """
77
+ uuid_data = await self.async_request("/user/getuuid.json")
78
+ feed_data = await self.async_request("/feed/list.json")
79
+ if feed_data[SUCCESS_KEY] and uuid_data[SUCCESS_KEY]:
80
+ for feed in feed_data[MESSAGE_KEY]:
81
+ feed["uuid"] = f"{uuid_data[MESSAGE_KEY]}_{feed['id']}"
82
+ if feed_data[SUCCESS_KEY]:
83
+ return feed_data[MESSAGE_KEY]
84
+ return None
85
+
86
+ async def async_get_feed_fields(self, feed_id: int) -> list[str, Any]:
87
+ """Get all feed fields."""
88
+ params = {"apikey": self.api_key, "id": feed_id}
89
+ return await self.async_request("/feed/aget.json", params=params)