|
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 | +""" |
2 | 14 |
|
3 | 15 | import asyncio |
4 | 16 | import logging |
5 | 17 | from typing import Any |
6 | 18 |
|
7 | 19 | import aiohttp |
8 | 20 |
|
9 | | -client_exceptions = ( |
10 | | - aiohttp.ClientError, |
11 | | - asyncio.TimeoutError, |
12 | | -) |
13 | | - |
14 | 21 | HTTP_STATUS = { |
15 | 22 | 400: "invalid request", |
16 | 23 | 401: "unauthorized access", |
@@ -50,17 +57,27 @@ async def async_request( |
50 | 57 | response = await session.get( |
51 | 58 | path, timeout=self.request_timeout, params=params |
52 | 59 | ) |
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" |
55 | 67 | data[MESSAGE_KEY] = message |
56 | 68 | self.logger.error(message) |
57 | 69 | return data |
58 | 70 | if response.status == 200: |
59 | 71 | data[SUCCESS_KEY] = True |
60 | 72 | json_response = await response.json() |
61 | 73 | 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 |
64 | 81 | else: |
65 | 82 | message = f"error {response.status}" |
66 | 83 | if response.status in HTTP_STATUS: |
|
0 commit comments