pyemoncms 0.0.4__tar.gz → 0.0.6__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyemoncms
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Summary: A python library to interrogate emoncms API
5
5
  Home-page: https://github.com/Open-Building-Management/pyemoncms
6
6
  Author: Alexandre CUER
@@ -9,7 +9,7 @@ Project-URL: Bug Tracker, https://github.com/Open-Building-Management/pyemoncms/
9
9
  Classifier: Programming Language :: Python :: 3
10
10
  Classifier: License :: OSI Approved :: MIT License
11
11
  Classifier: Operating System :: OS Independent
12
- Requires-Python: >=3.10
12
+ Requires-Python: >=3.9
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE
15
15
  Requires-Dist: aiohttp
@@ -36,8 +36,8 @@ client.logger.setLevel("DEBUG")
36
36
  async def get_path(path):
37
37
  print(await client.async_request(path))
38
38
 
39
- async def list_feeds():
40
- print(await client.async_list_feeds())
39
+ async def list_feeds(uuid=False):
40
+ print(await client.async_list_feeds(uuid=uuid))
41
41
 
42
42
  async def get_feed_fields(feed_id):
43
43
  print(await client.async_get_feed_fields(feed_id))
@@ -47,6 +47,7 @@ loop = asyncio.get_event_loop()
47
47
  loop.create_task(get_path("/feed/list.json"))
48
48
  loop.create_task(get_path("/user/getuuid.json"))
49
49
  loop.create_task(list_feeds())
50
+ loop.create_task(list_feeds(uuid=True))
50
51
  loop.create_task(get_feed_fields(1))
51
52
  loop.run_forever()
52
53
  ```
@@ -20,8 +20,8 @@ client.logger.setLevel("DEBUG")
20
20
  async def get_path(path):
21
21
  print(await client.async_request(path))
22
22
 
23
- async def list_feeds():
24
- print(await client.async_list_feeds())
23
+ async def list_feeds(uuid=False):
24
+ print(await client.async_list_feeds(uuid=uuid))
25
25
 
26
26
  async def get_feed_fields(feed_id):
27
27
  print(await client.async_get_feed_fields(feed_id))
@@ -31,6 +31,7 @@ loop = asyncio.get_event_loop()
31
31
  loop.create_task(get_path("/feed/list.json"))
32
32
  loop.create_task(get_path("/user/getuuid.json"))
33
33
  loop.create_task(list_feeds())
34
+ loop.create_task(list_feeds(uuid=True))
34
35
  loop.create_task(get_feed_fields(1))
35
36
  loop.run_forever()
36
37
  ```
@@ -74,9 +74,7 @@ class EmoncmsClient:
74
74
  try:
75
75
  if MESSAGE_KEY in json_response:
76
76
  data[MESSAGE_KEY] = json_response[MESSAGE_KEY]
77
- except TypeError as er:
78
- message = f"type error : {er}"
79
- self.logger.error(message)
77
+ except TypeError:
80
78
  data[SUCCESS_KEY] = False
81
79
  else:
82
80
  message = f"error {response.status}"
@@ -86,16 +84,21 @@ class EmoncmsClient:
86
84
  self.logger.error(message)
87
85
  return data
88
86
 
89
- async def async_list_feeds(self) -> list[dict[str, Any]] | None:
87
+ async def async_list_feeds(self, uuid: bool = False) -> list[dict[str, Any]] | None:
90
88
  """Request emoncms feeds list.
91
89
 
92
90
  return a uuid per feed if available
93
91
  """
94
- uuid_data = await self.async_request("/user/getuuid.json")
95
92
  feed_data = await self.async_request("/feed/list.json")
96
- if feed_data[SUCCESS_KEY] and uuid_data[SUCCESS_KEY]:
97
- for feed in feed_data[MESSAGE_KEY]:
98
- feed["uuid"] = f"{uuid_data[MESSAGE_KEY]}_{feed['id']}"
93
+ if uuid:
94
+ uuid_data = await self.async_request("/user/getuuid.json")
95
+ if not uuid_data[SUCCESS_KEY]:
96
+ message = "no uuid available"
97
+ message = f"{message} - migrate your emoncms sensor to a newer version"
98
+ self.logger.warning(message)
99
+ if feed_data[SUCCESS_KEY] and uuid_data[SUCCESS_KEY]:
100
+ for feed in feed_data[MESSAGE_KEY]:
101
+ feed["uuid"] = f"{uuid_data[MESSAGE_KEY]}_{feed['id']}"
99
102
  if feed_data[SUCCESS_KEY]:
100
103
  return feed_data[MESSAGE_KEY]
101
104
  return None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyemoncms
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Summary: A python library to interrogate emoncms API
5
5
  Home-page: https://github.com/Open-Building-Management/pyemoncms
6
6
  Author: Alexandre CUER
@@ -9,7 +9,7 @@ Project-URL: Bug Tracker, https://github.com/Open-Building-Management/pyemoncms/
9
9
  Classifier: Programming Language :: Python :: 3
10
10
  Classifier: License :: OSI Approved :: MIT License
11
11
  Classifier: Operating System :: OS Independent
12
- Requires-Python: >=3.10
12
+ Requires-Python: >=3.9
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE
15
15
  Requires-Dist: aiohttp
@@ -36,8 +36,8 @@ client.logger.setLevel("DEBUG")
36
36
  async def get_path(path):
37
37
  print(await client.async_request(path))
38
38
 
39
- async def list_feeds():
40
- print(await client.async_list_feeds())
39
+ async def list_feeds(uuid=False):
40
+ print(await client.async_list_feeds(uuid=uuid))
41
41
 
42
42
  async def get_feed_fields(feed_id):
43
43
  print(await client.async_get_feed_fields(feed_id))
@@ -47,6 +47,7 @@ loop = asyncio.get_event_loop()
47
47
  loop.create_task(get_path("/feed/list.json"))
48
48
  loop.create_task(get_path("/user/getuuid.json"))
49
49
  loop.create_task(list_feeds())
50
+ loop.create_task(list_feeds(uuid=True))
50
51
  loop.create_task(get_feed_fields(1))
51
52
  loop.run_forever()
52
53
  ```
@@ -8,4 +8,5 @@ pyemoncms.egg-info/PKG-INFO
8
8
  pyemoncms.egg-info/SOURCES.txt
9
9
  pyemoncms.egg-info/dependency_links.txt
10
10
  pyemoncms.egg-info/requires.txt
11
- pyemoncms.egg-info/top_level.txt
11
+ pyemoncms.egg-info/top_level.txt
12
+ tests/test_async_request.py
@@ -4,3 +4,6 @@ requires = [
4
4
  "wheel"
5
5
  ]
6
6
  build-backend = "setuptools.build_meta"
7
+
8
+ [tool.pytest.ini_options]
9
+ asyncio_mode = "auto"
@@ -6,7 +6,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
6
6
 
7
7
  setuptools.setup(
8
8
  name="pyemoncms",
9
- version="0.0.4",
9
+ version="0.0.6",
10
10
  author="Alexandre CUER",
11
11
  author_email="alexandre.cuer@wanadoo.fr",
12
12
  description="A python library to interrogate emoncms API",
@@ -25,5 +25,5 @@ setuptools.setup(
25
25
  install_requires=[
26
26
  'aiohttp'
27
27
  ],
28
- python_requires=">=3.10",
28
+ python_requires=">=3.9",
29
29
  )
@@ -0,0 +1,73 @@
1
+ """Tests for async_request method."""
2
+
3
+ import os
4
+ import sys
5
+
6
+ from aiohttp import web
7
+
8
+ root_path = os.getcwd()
9
+ sys.path.insert(0, root_path)
10
+ from pyemoncms.emoncms_client import EmoncmsClient # pylint: disable=E0401,C0413
11
+
12
+ API_KEY = "123"
13
+
14
+ FEED = {
15
+ "id":"1",
16
+ "userid":"1",
17
+ "name":"Cellule_Tcircuit",
18
+ "tag":" sofrel_circuit_Cellule",
19
+ "public":"0",
20
+ "size":"35811340",
21
+ "engine":"5",
22
+ "processList":"",
23
+ "unit":"",
24
+ "time":1665509570,
25
+ "value":17.690000534058
26
+ }
27
+
28
+ FEEDS = [ FEED ]
29
+
30
+
31
+ async def feed_list_route(request):
32
+ """Emoncms feed list route."""
33
+ if "apikey" not in request.query:
34
+ return web.json_response(False)
35
+ if request.query["apikey"] != API_KEY:
36
+ raise web.HTTPUnauthorized(text="Invalid API key")
37
+ return web.json_response(FEEDS)
38
+
39
+
40
+ def create_feed_api():
41
+ """Emoncms server."""
42
+ app = web.Application()
43
+ app.router.add_route("GET", "/feed/list.json", feed_list_route)
44
+ return app
45
+
46
+ async def test_response_ok(aiohttp_server):
47
+ """Test correct response."""
48
+ await aiohttp_server(create_feed_api(), port=8080)
49
+ client = EmoncmsClient("http://127.0.0.1:8080", API_KEY)
50
+ datas = await client.async_request("/feed/list.json")
51
+ assert FEEDS == datas["message"]
52
+
53
+ async def test_no_api_auth(aiohttp_server):
54
+ """Test incorrect API key."""
55
+ await aiohttp_server(create_feed_api(), port=8080)
56
+ client = EmoncmsClient("http://127.0.0.1:8080", "666")
57
+ datas = await client.async_request("/feed/list.json")
58
+ assert not datas["success"]
59
+ assert "401" in datas["message"]
60
+
61
+ async def test_timeout():
62
+ """Test when IP does not exist."""
63
+ client = EmoncmsClient("http://192.16.1.32:8080", API_KEY, request_timeout=1)
64
+ datas = await client.async_request("/feed/list.json")
65
+ assert not datas["success"]
66
+ assert "time out" in datas["message"]
67
+
68
+ async def test_client_error():
69
+ """Test when IP exists but client not running."""
70
+ client = EmoncmsClient("http://127.0.0.1:8081", API_KEY)
71
+ datas = await client.async_request("/feed/list.json")
72
+ assert not datas["success"]
73
+ assert "client error" in datas["message"]
File without changes
File without changes