Skip to content

Commit

Permalink
Feat/api notes and shares (#42)
Browse files Browse the repository at this point in the history
* added notes endpoint

* added shares endpoint

---------

Co-authored-by: Moser Marco Julian <marco.moser@smaxtec.com>
  • Loading branch information
Mopsgeschwindigkeit and Moser Marco Julian authored Nov 1, 2023
1 parent 3883487 commit 21a3ff9
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/sxapi/publicV2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from sxapi.publicV2.users import Users
from sxapi.publicV2.events import Events
from sxapi.publicV2.animalgroups import AnimalGroups
from sxapi.publicV2.notes import Notes
from sxapi.publicV2.shares import Shares

PUBLIC_API_V2_BASE_URL = "https://api.smaxtec.com/api/v2"

Expand All @@ -28,6 +30,8 @@ def __init__(self, base_url=None, email=None, password=None, api_token=None):
self.feedrations = Feedrations(api=self)
self.events = Events(api=self)
self.animalgroups = AnimalGroups(api=self)
self.notes = Notes(api=self)
self.shares = Shares(api=self)

super().__init__(
base_url,
Expand Down
95 changes: 95 additions & 0 deletions src/sxapi/publicV2/notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class Notes:
"""
Class for interacting with the notes endpoint of the public V2 API
https://api.smaxtec.com/api/v2/
"""

def __init__(self, api=None):
self.api = api
self.path_suffix = "/notes"

def post(self, source, reference_type, reference_id, category, note_event, **kwargs):
"""Create a new note.
Args:
source (str): Source of the note
reference_type (str): Type of the reference
reference_id (str): ID of the reference
category (str): Category of the note
note_event (str): Event of the note
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/api/v2/
Returns:
dict: Response of API call. Note on success,
error message else.
"""
params = {
"source": source,
"reference_type": reference_type,
"reference_id": reference_id,
"category": category,
"note_event": note_event,
}

for k, v in kwargs.items():
params[k] = v

return self.api.post(self.path_suffix, json=params)

def get(self, note_id, **kwargs):
"""Get one note.
Args:
note_id (str): ID of the desired note
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/api/v2/
Returns:
dict: Response of API call. Note on success,
error message else.
"""
params = {}

for k, v in kwargs.items():
params[k] = v

url_suffix = self.path_suffix + f"/{note_id}"
return self.api.get(url_suffix, json=kwargs)

def put(self, note_id, source, reference_type, reference_id, category, note_event, **kwargs):
"""Update an existing note.
Args:
note_id (str): ID of the note to be updated
source (str): Source of the note
reference_type (str): Type of the reference
reference_id (str): ID of the reference
category (str): Category of the note
note_event (str): Event of the note
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/api/v2/
Returns:
dict: Response of API call. Note on success,
error message else.
"""
params = {
"source": source,
"reference_type": reference_type,
"reference_id": reference_id,
"category": category,
"note_event": note_event,
}

for k, v in kwargs.items():
params[k] = v

url_suffix = self.path_suffix + f"/{note_id}"
return self.api.put(url_suffix, json=params)
103 changes: 103 additions & 0 deletions src/sxapi/publicV2/shares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
class Shares:
"""
Class for interacting with the notes endpoint of the public V2 API
https://api.smaxtec.com/api/v2/
"""

def __init__(self, api=None):
self.api = api
self.path_suffix = "/shares"

def post(self, email, organisation_id, **kwargs):
"""Create a new share.
Args:
email (str): Email of the user to share
organisation_id (str): ID of the organisation to share
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/api/v2/
Returns:
dict: Response of API call. Share on success,
error message else.
"""
params = {
"email": email,
"organisation_id": organisation_id,
}

for k, v in kwargs.items():
params[k] = v

return self.api.post(self.path_suffix, json=params)

def get(self, share_id, **kwargs):
"""Get one share.
Args:
share_id (str): ID of the desired share
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/api/v2/
Returns:
dict: Response of API call. Share on success,
error message else.
"""
params = {}

for k, v in kwargs.items():
params[k] = v

url_suffix = self.path_suffix + f"/{share_id}"
return self.api.get(url_suffix, json=params)

def put(self, share_id, role, **kwargs):
"""Update one share.
Args:
share_id (str): ID of the desired share
role (str): Role of the share
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/api/v2/
Returns:
dict: Response of API call. Share on success,
error message else.
"""
params = {
"role": role,
}

for k, v in kwargs.items():
params[k] = v

url_suffix = self.path_suffix + f"/{share_id}"
return self.api.put(url_suffix, json=params)

def delete(self, share_id, **kwargs):
"""Delete one share.
Args:
share_id (str): ID of the desired share
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/api/v2/
Returns:
dict: Response of API call. Result on success,
error message else.
"""
params = {}

for k, v in kwargs.items():
params[k] = v

url_suffix = self.path_suffix + f"/{share_id}"
return self.api.delete(url_suffix, json=params)
69 changes: 69 additions & 0 deletions tests/test_publicV2/test_notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import mock

from sxapi.publicV2 import PublicAPIV2

@mock.patch("sxapi.publicV2.PublicAPIV2.post")
def test_post(post_mock):
test_api = PublicAPIV2()
test_api.notes.post(
"test_source",
"test_reference_type",
"test_reference_id",
"test_category",
"test_note_event",
kwarg1="kwarg1"
)

call_args = post_mock.call_args_list[0]

assert post_mock.call_count == 1
assert call_args.args[0] == "/notes"
assert call_args.kwargs["json"] == {
"source": "test_source",
"reference_type": "test_reference_type",
"reference_id": "test_reference_id",
"category": "test_category",
"note_event": "test_note_event",
"kwarg1": "kwarg1",
}


@mock.patch("sxapi.publicV2.PublicAPIV2.get")
def test_get(get_mock):
test_api = PublicAPIV2()
test_api.notes.get("test_note_id", kwarg1="kwarg1")

call_args = get_mock.call_args_list[0]

assert get_mock.call_count == 1
assert call_args.args[0] == "/notes/test_note_id"
assert call_args.kwargs["json"] == {
"kwarg1": "kwarg1",
}


@mock.patch("sxapi.publicV2.PublicAPIV2.put")
def test_put(put_mock):
test_api = PublicAPIV2()
test_api.notes.put(
"test_note_id",
"test_source",
"test_reference_type",
"test_reference_id",
"test_category",
"test_note_event",
kwarg1="kwarg1"
)

call_args = put_mock.call_args_list[0]

assert put_mock.call_count == 1
assert call_args.args[0] == "/notes/test_note_id"
assert call_args.kwargs["json"] == {
"source": "test_source",
"reference_type": "test_reference_type",
"reference_id": "test_reference_id",
"category": "test_category",
"note_event": "test_note_event",
"kwarg1": "kwarg1",
}
69 changes: 69 additions & 0 deletions tests/test_publicV2/test_shares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import mock

from sxapi.publicV2 import PublicAPIV2


@mock.patch("sxapi.publicV2.PublicAPIV2.post")
def test_post(post_mock):
test_api = PublicAPIV2()
test_api.shares.post(
"test_email",
"test_organisation_id",
kwarg1="1",
)

call_args = post_mock.call_args_list[0]

assert post_mock.call_count == 1
assert call_args.args[0] == "/shares"
assert call_args.kwargs["json"] == {
"email": "test_email",
"organisation_id": "test_organisation_id",
"kwarg1": "1",
}


@mock.patch("sxapi.publicV2.PublicAPIV2.get")
def test_get(get_mock):
test_api = PublicAPIV2()
test_api.shares.get("test_share_id", kwarg1="1", kwarg2="2")

call_args = get_mock.call_args_list[0]

assert get_mock.call_count == 1
assert call_args.args[0] == "/shares/test_share_id"
assert call_args.kwargs["json"] == {
"kwarg1": "1",
"kwarg2": "2",
}


@mock.patch("sxapi.publicV2.PublicAPIV2.put")
def test_put(put_mock):
test_api = PublicAPIV2()
test_api.shares.put("test_share_id", "test_role", kwarg1="1", kwarg2="2")

call_args = put_mock.call_args_list[0]

assert put_mock.call_count == 1
assert call_args.args[0] == "/shares/test_share_id"
assert call_args.kwargs["json"] == {
"role": "test_role",
"kwarg1": "1",
"kwarg2": "2",
}


@mock.patch("sxapi.publicV2.PublicAPIV2.delete")
def test_delete(delete_mock):
test_api = PublicAPIV2()
test_api.shares.delete("test_share_id", kwarg1="1", kwarg2="2")

call_args = delete_mock.call_args_list[0]

assert delete_mock.call_count == 1
assert call_args.args[0] == "/shares/test_share_id"
assert call_args.kwargs["json"] == {
"kwarg1": "1",
"kwarg2": "2",
}

0 comments on commit 21a3ff9

Please sign in to comment.