-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create Todos class * added test * renamed test file * moved publicApiV2 impelemntation to publicV2 folder * fixed imports after moving publicAPIV2 class * added docstrings * docstring v2 * improved naming
- Loading branch information
1 parent
96623c2
commit e9656db
Showing
12 changed files
with
240 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from sxapi.base import ( | ||
ApiTypes, | ||
BaseAPI, | ||
) | ||
from sxapi.publicV2.todos import Todos | ||
|
||
PUBLIC_API_V2_BASE_URL = "https://api.smaxtec.com/api/v2" | ||
|
||
|
||
class PublicAPIV2(BaseAPI): | ||
def __init__(self, base_url=None, email=None, password=None, api_token=None): | ||
"""Initialize a new public api client instance.""" | ||
base_url = base_url or PUBLIC_API_V2_BASE_URL | ||
api_type = ApiTypes.PUBLIC | ||
|
||
self.todos = Todos(api=self) | ||
|
||
super().__init__( | ||
base_url, | ||
email=email, | ||
password=password, | ||
api_token=api_token, | ||
api_type=api_type, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
class Todos: | ||
""" | ||
This Class represents the /todos endpoint of the PublicAPIV2. | ||
https://api.smaxtec.com/api/v2/ | ||
""" | ||
|
||
def __init__(self, api=None): | ||
self.api = api | ||
self.path_suffix = "/todos" | ||
|
||
def post(self, todo_type, organisation_id, **kwargs): | ||
"""Creates a new todo. | ||
Args: | ||
todo_type (str): Type of the todo to be created | ||
organisation_id (str): Id of organisation the todo should be created for | ||
**kwargs: Optional parameters of the API call. | ||
Find supported parameters under | ||
https://api.smaxtec.com/api/v2/ | ||
Returns: | ||
dict: Response of API call. Created todo on success, error message else. | ||
""" | ||
params = { | ||
"todo_type": todo_type, | ||
"organisation_id": organisation_id, | ||
} | ||
|
||
for k, v in kwargs.items(): | ||
params[k] = v | ||
|
||
self.api.post(self.path_suffix, json=params) | ||
|
||
def put(self, todo_id, **kwargs): | ||
"""Updates an existing todo. | ||
Args: | ||
todo_id (str): Id of the todo which should be updated. | ||
**kwargs: Optional parameters of the API call. | ||
Find supported parameters under | ||
https://api.smaxtec.com/api/v2/ | ||
Returns: | ||
dict: Response of API call. Updated todo on success, error message else. | ||
""" | ||
params = {} | ||
for k, v in kwargs.items(): | ||
params[k] = v | ||
|
||
url_suffix = self.path_suffix + f"/{todo_id}" | ||
return self.api.put(url_suffix, json=params) | ||
|
||
def get(self, todo_id): | ||
"""Get one todo. | ||
Args: | ||
todo_id (str): Id of the desired todo | ||
Returns: | ||
dict: Response of API call. Queried todo on success, error message else. | ||
""" | ||
return self.api.get(self.path_suffix + f"/{todo_id}") | ||
|
||
def post_comment(self, todo_id, content): | ||
"""Create a comment for a todo. | ||
Args: | ||
todo_id (str): Id of the todo a comment is to be created. | ||
content (str): Content of the comment | ||
Returns: | ||
dict: Response of API call. Updated todo on success, error message else. | ||
""" | ||
params = { | ||
"content": content, | ||
} | ||
|
||
url_suffix = self.path_suffix + f"/{todo_id}/comments" | ||
return self.api.post(url_suffix, json=params) | ||
|
||
def delete_comment(self, todo_id, comment_id): | ||
"""Delete a comment from a todo. | ||
Args: | ||
todo_id (str): Id of the todo a comment is to be deleted. | ||
comment_id (str): Id of the comment to delete. | ||
Returns: | ||
dict: Response of API call. Updated todo on success, error message else. | ||
""" | ||
|
||
url_suffix = self.path_suffix + f"/{todo_id}/comments/{comment_id}" | ||
return self.api.delete(url_suffix) | ||
|
||
def put_comment(self, todo_id, comment_id, content): | ||
"""Update a comment from a todo. | ||
Args: | ||
todo_id (str): Id of the todo a comment is to be updated. | ||
comment_id (str): Id of the comment to be updated. | ||
content (str): Updated content of the Id. | ||
Returns: | ||
dict: Response of API call. Updated todo on success, error message else. | ||
""" | ||
params = { | ||
"content": content, | ||
} | ||
|
||
url_suffix = self.path_suffix + f"/{todo_id}/comments/{comment_id}" | ||
return self.api.put(url_suffix, json=params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
tests/test_publicV2.py → tests/test_publicV2/test_get_sensor_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import mock | ||
|
||
from sxapi.publicV2 import PublicAPIV2 | ||
|
||
|
||
@mock.patch("sxapi.publicV2.PublicAPIV2.post") | ||
def test_post_todos(post_mock): | ||
test_api = PublicAPIV2() | ||
test_api.todos.post( | ||
"test_todo_type", "test_orga_id", kwarg1="kwarg1", optional2="optional2" | ||
) | ||
|
||
call_args = post_mock.call_args_list[0] | ||
|
||
assert post_mock.call_count == 1 | ||
assert call_args.args[0] == "/todos" | ||
assert call_args.kwargs["json"] == { | ||
"todo_type": "test_todo_type", | ||
"organisation_id": "test_orga_id", | ||
"kwarg1": "kwarg1", | ||
"optional2": "optional2", | ||
} | ||
|
||
|
||
@mock.patch("sxapi.publicV2.PublicAPIV2.post") | ||
def test_post_todo_comment(post_mock): | ||
test_api = PublicAPIV2() | ||
test_api.todos.post_comment("test_todo_id", content="comment_content") | ||
|
||
call_args = post_mock.call_args_list[0] | ||
|
||
assert post_mock.call_count == 1 | ||
assert call_args.args[0] == "/todos/test_todo_id/comments" | ||
assert call_args.kwargs["json"] == { | ||
"content": "comment_content", | ||
} | ||
|
||
|
||
@mock.patch("sxapi.publicV2.PublicAPIV2.put") | ||
def test_put_todos(put_mock): | ||
test_api = PublicAPIV2() | ||
test_api.todos.put("test_todo_id", kwarg1="1", kwarg2="2", kwarg3=3) | ||
|
||
call_args = put_mock.call_args_list[0] | ||
|
||
assert put_mock.call_count == 1 | ||
assert call_args.args[0] == "/todos/test_todo_id" | ||
assert call_args.kwargs["json"] == { | ||
"kwarg1": "1", | ||
"kwarg2": "2", | ||
"kwarg3": 3, | ||
} | ||
|
||
|
||
@mock.patch("sxapi.publicV2.PublicAPIV2.put") | ||
def test_put_todo_comments(get_mock): | ||
test_api = PublicAPIV2() | ||
test_api.todos.put_comment("test_todo_id", "test_comment_id", "updated_content") | ||
|
||
call_args = get_mock.call_args_list[0] | ||
|
||
assert get_mock.call_count == 1 | ||
assert call_args.args[0] == "/todos/test_todo_id/comments/test_comment_id" | ||
assert call_args.kwargs["json"] == {"content": "updated_content"} | ||
|
||
|
||
@mock.patch("sxapi.publicV2.PublicAPIV2.get") | ||
def test_get_todos(get_mock): | ||
test_api = PublicAPIV2() | ||
test_api.todos.get("test_todo_id") | ||
|
||
call_args = get_mock.call_args_list[0] | ||
|
||
assert get_mock.call_count == 1 | ||
assert call_args.args[0] == "/todos/test_todo_id" | ||
assert call_args.kwargs == {} | ||
|
||
|
||
@mock.patch("sxapi.publicV2.PublicAPIV2.delete") | ||
def test_delete_todo_comments(get_mock): | ||
test_api = PublicAPIV2() | ||
test_api.todos.delete_comment("test_todo_id", "test_comment_id") | ||
|
||
call_args = get_mock.call_args_list[0] | ||
|
||
assert get_mock.call_count == 1 | ||
assert call_args.args[0] == "/todos/test_todo_id/comments/test_comment_id" | ||
assert call_args.kwargs == {} |