Skip to content

Commit

Permalink
remove uuid param from async_list_feeds & drop warning in async_request
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrecuer committed Jun 27, 2024
1 parent c6313d2 commit e99a6d1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ client.logger.setLevel("DEBUG")
async def get_path(path):
print(await client.async_request(path))
async def list_feeds(uuid=False):
print(await client.async_list_feeds(uuid=uuid))
async def list_feeds():
print(await client.async_list_feeds())
async def get_feed_fields(feed_id):
print(await client.async_get_feed_fields(feed_id))
Expand All @@ -31,7 +31,6 @@ loop = asyncio.get_event_loop()
loop.create_task(get_path("feed/list.json"))
loop.create_task(get_path("/user/getuuid.json"))
loop.create_task(list_feeds())
loop.create_task(list_feeds(uuid=True))
loop.create_task(get_feed_fields(1))
try:
loop.run_forever()
Expand Down
21 changes: 7 additions & 14 deletions pyemoncms/emoncms_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ async def async_request(
data[SUCCESS_KEY] = True
json_response = await response.json()
data[MESSAGE_KEY] = json_response
try:
if MESSAGE_KEY in json_response:
data[MESSAGE_KEY] = json_response[MESSAGE_KEY]
except TypeError:
if json_response is False:
data[SUCCESS_KEY] = False
elif SUCCESS_KEY in json_response:
data = json_response
if not data[SUCCESS_KEY]:
message = data[MESSAGE_KEY].replace("ADMIN", "EMONCMS ADMIN")
self.logger.warning(message)
else:
message = f"error {response.status}"
if response.status in HTTP_STATUS:
Expand All @@ -89,21 +91,12 @@ async def async_request(
self.logger.error(message)
return data

async def async_list_feeds(self, uuid: bool = False) -> list[dict[str, Any]] | None:
async def async_list_feeds(self) -> list[dict[str, Any]] | None:
"""Request emoncms feeds list.
return a uuid per feed if available
"""
feed_data = await self.async_request("/feed/list.json")
if uuid:
uuid_data = await self.async_request("/user/getuuid.json")
if not uuid_data[SUCCESS_KEY]:
message = "no uuid available"
message = f"{message} - migrate your emoncms sensor to a newer version"
self.logger.warning(message)
if feed_data[SUCCESS_KEY] and uuid_data[SUCCESS_KEY]:
for feed in feed_data[MESSAGE_KEY]:
feed["uuid"] = f"{uuid_data[MESSAGE_KEY]}_{feed['id']}"
if feed_data[SUCCESS_KEY]:
return feed_data[MESSAGE_KEY]
return None
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name="pyemoncms",
version="0.0.7",
version="0.0.8",
author="Alexandre CUER",
author_email="alexandre.cuer@wanadoo.fr",
description="A python library to interrogate emoncms API",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_async_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def test_timeout():

async def test_client_error():
"""Test when IP exists but client not running."""
client = EmoncmsClient("http://127.0.0.1:8081", API_KEY)
client = EmoncmsClient("http://127.0.0.1:8087", API_KEY)
datas = await client.async_request("/feed/list.json")
assert not datas["success"]
assert "client error" in datas["message"]

0 comments on commit e99a6d1

Please sign in to comment.