Skip to content

Commit d6b8ee0

Browse files
committed
improving error catching + example in README
1 parent 04800c2 commit d6b8ee0

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
11
# pyemoncms
22
python library for emoncms API
3+
4+
rely on [aiohttp](https://github.com/aio-libs/aiohttp)
5+
6+
to install : `pip3 install pyemoncms`
7+
8+
how to use :
9+
10+
```
11+
import logging
12+
import asyncio
13+
from pyemoncms import EmoncmsClient
14+
15+
key = "your32bitsAPIkey"
16+
url = "http://url:8081"
17+
client = EmoncmsClient(url, key, request_timeout=1)
18+
client.logger.setLevel("DEBUG")
19+
20+
async def get_path(path):
21+
print(await client.async_request(path))
22+
23+
async def list_feeds():
24+
print(await client.async_list_feeds())
25+
26+
async def get_feed_fields(feed_id):
27+
print(await client.async_get_feed_fields(feed_id))
28+
29+
30+
loop = asyncio.get_event_loop()
31+
loop.create_task(get_path("/feed/list.json"))
32+
loop.create_task(get_path("/user/getuuid.json"))
33+
loop.create_task(list_feeds())
34+
loop.create_task(get_feed_fields(1))
35+
loop.run_forever()
36+
```

pyemoncms/emoncms_client.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
"""Emoncms client."""
1+
"""Emoncms client.
2+
3+
emoncms feed module :
4+
an inexisting json route responds by {success: false, message : "Feed does not exist"}
5+
examples of inexisting json routes :
6+
- feed/aget.json?id=200 if there is no feed number 200
7+
- feed/basket.json
8+
the feed/list.json always returns an array of json objects, which can be empty if there is no feed
9+
10+
emoncms user module
11+
an inexisting json route responds by false which is not a json object
12+
so there is a type error if you search for a key in the response
13+
"""
214

315
import asyncio
416
import logging
517
from typing import Any
618

719
import aiohttp
820

9-
client_exceptions = (
10-
aiohttp.ClientError,
11-
asyncio.TimeoutError,
12-
)
13-
1421
HTTP_STATUS = {
1522
400: "invalid request",
1623
401: "unauthorized access",
@@ -50,17 +57,27 @@ async def async_request(
5057
response = await session.get(
5158
path, timeout=self.request_timeout, params=params
5259
)
53-
except client_exceptions as e:
54-
message = f"exception {e}"
60+
except aiohttp.ClientError as er:
61+
message = f"client error : {er}"
62+
data[MESSAGE_KEY] = message
63+
self.logger.error(message)
64+
return data
65+
except asyncio.TimeoutError:
66+
message = "time out error"
5567
data[MESSAGE_KEY] = message
5668
self.logger.error(message)
5769
return data
5870
if response.status == 200:
5971
data[SUCCESS_KEY] = True
6072
json_response = await response.json()
6173
data[MESSAGE_KEY] = json_response
62-
if MESSAGE_KEY in json_response:
63-
data[MESSAGE_KEY] = json_response[MESSAGE_KEY]
74+
try:
75+
if MESSAGE_KEY in json_response:
76+
data[MESSAGE_KEY] = json_response[MESSAGE_KEY]
77+
except TypeError as er:
78+
message = f"type error : {er}"
79+
self.logger.error(message)
80+
data[SUCCESS_KEY] = False
6481
else:
6582
message = f"error {response.status}"
6683
if response.status in HTTP_STATUS:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setuptools.setup(
88
name="pyemoncms",
9-
version="0.0.3",
9+
version="0.0.4",
1010
author="Alexandre CUER",
1111
author_email="alexandre.cuer@wanadoo.fr",
1212
description="A python library to interrogate emoncms API",

0 commit comments

Comments
 (0)