Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,19 @@ daily_stats = stats.get_stats(
)
```

### 2. emon_api

The `emon_api` module is Emoncms python api module, used to interract with Emoncms server instance.

#### Features

- Data Reading: Efficiently read data from Emoncms.
- Data Writing: Set Inputs, feeds or values

### Usage Examples:

...

## Running Tests

To ensure everything is functioning correctly, run the test suite:
Expand Down
2 changes: 1 addition & 1 deletion emon_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""emon-tools package"""
__version__ = "0.1.1"
__version__ = "0.1.2"
117 changes: 117 additions & 0 deletions emon_tools/api_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""
Api common utilities.
"""
import re
from typing import Any

HTTP_STATUS = {
400: "invalid request",
401: "unauthorized access",
404: "Not found",
406: "URI not acceptable",
}

MESSAGE_KEY = "message"
SUCCESS_KEY = "success"


class Utils:
"""Emoncms data Helper"""

@staticmethod
def is_str(text: str, not_empty=False) -> bool:
"""
Test if text is a string.

:Example :
>>> Utils.is_str(text='hello')
>>> True
:param text: str: Value to test.
:return: bool: True if value is valid string object.
"""
result = isinstance(text, str)

Check warning on line 32 in emon_tools/api_common.py

View check run for this annotation

Codecov / codecov/patch

emon_tools/api_common.py#L32

Added line #L32 was not covered by tests
if not_empty:
result = result and len(text) > 0
return result

Check warning on line 35 in emon_tools/api_common.py

View check run for this annotation

Codecov / codecov/patch

emon_tools/api_common.py#L34-L35

Added lines #L34 - L35 were not covered by tests

@staticmethod
def is_list(data: Any, not_empty: bool = False) -> bool:
"""
Test if data is a list.

:Example :
>>> Utils.is_list(data=['hello'])
>>> True
:param data: Any: Value to test.
:return: bool: True if value is valid list object.
"""
result = isinstance(data, list)

Check warning on line 48 in emon_tools/api_common.py

View check run for this annotation

Codecov / codecov/patch

emon_tools/api_common.py#L48

Added line #L48 was not covered by tests
if not_empty:
result = result and len(data) > 0
return result

Check warning on line 51 in emon_tools/api_common.py

View check run for this annotation

Codecov / codecov/patch

emon_tools/api_common.py#L50-L51

Added lines #L50 - L51 were not covered by tests

@staticmethod
def is_dict(data: Any, not_empty: bool = False) -> bool:
"""
Test if data is a dict.

:Example :
>>> Utils.is_dict(data=['hello'])
>>> True
:param data: Any: Value to test.
:return: bool: True if value is valid dict object.
"""
result = isinstance(data, dict)
if not_empty:
result = result and len(data) > 0

Check warning on line 66 in emon_tools/api_common.py

View check run for this annotation

Codecov / codecov/patch

emon_tools/api_common.py#L66

Added line #L66 was not covered by tests
return result

@staticmethod
def is_valid_node(text) -> bool:
"""
Test if text is valid node or name values.

[Original regex from emoncms](https://github.com/emoncms/emoncms/blob/master/Modules/feed/feed_model.php#L99)

:Example :
>>> Utils.is_valid_node(text="Node1")
>>> True

:param text: str: Node value.
:return: bool: True if text is valid node.
"""
result = False

Check warning on line 83 in emon_tools/api_common.py

View check run for this annotation

Codecov / codecov/patch

emon_tools/api_common.py#L83

Added line #L83 was not covered by tests
if Utils.is_str(text):
matches = re.findall(r'[\w\s\-:]', text, flags=re.UNICODE)
result = Utils.is_list(matches, not_empty=True)
return result

Check warning on line 87 in emon_tools/api_common.py

View check run for this annotation

Codecov / codecov/patch

emon_tools/api_common.py#L85-L87

Added lines #L85 - L87 were not covered by tests

@staticmethod
def is_request_success(result) -> bool:
"""
Test if request to emoncms is success.

:Example :
>>> Utils.is_request_success(result={"success": "true"})
>>> True
:param result: dict: The request json response.
:return: bool: True if the request return success.
"""
return Utils.is_dict(result)\

Check warning on line 100 in emon_tools/api_common.py

View check run for this annotation

Codecov / codecov/patch

emon_tools/api_common.py#L100

Added line #L100 was not covered by tests
and result.get('success') == "true"

@staticmethod
def compute_response(result: dict) -> bool:
"""
Compute the response from emoncms.
"""
if Utils.is_dict(result):
success = result.get(SUCCESS_KEY) is True
message = result.get(MESSAGE_KEY)
elif Utils.is_str(result):
success = True
message = result

Check warning on line 113 in emon_tools/api_common.py

View check run for this annotation

Codecov / codecov/patch

emon_tools/api_common.py#L112-L113

Added lines #L112 - L113 were not covered by tests
else:
success = False
message = "Invalid response"

Check warning on line 116 in emon_tools/api_common.py

View check run for this annotation

Codecov / codecov/patch

emon_tools/api_common.py#L115-L116

Added lines #L115 - L116 were not covered by tests
return success, message
Loading
Loading