From 30d04ec8a9112d2fd0ab08e3d4037815ce674b18 Mon Sep 17 00:00:00 2001 From: Leonardo Kagohara Date: Fri, 1 Mar 2024 13:54:56 -0300 Subject: [PATCH] Add Split Profile resource --- starkbank/__init__.py | 3 + starkbank/splitprofile/__init__.py | 3 + starkbank/splitprofile/__splitprofile.py | 113 +++++++++++++++++++++++ starkbank/splitprofile/log/__init__.py | 1 + starkbank/splitprofile/log/__log.py | 96 +++++++++++++++++++ starkbank/utils/rest.py | 1 + tests/sdk/test_split_profile.py | 79 ++++++++++++++++ tests/sdk/test_split_profile_log.py | 45 +++++++++ 8 files changed, 341 insertions(+) create mode 100644 starkbank/splitprofile/__init__.py create mode 100644 starkbank/splitprofile/__splitprofile.py create mode 100644 starkbank/splitprofile/log/__init__.py create mode 100644 starkbank/splitprofile/log/__log.py create mode 100644 tests/sdk/test_split_profile.py create mode 100644 tests/sdk/test_split_profile_log.py diff --git a/starkbank/__init__.py b/starkbank/__init__.py index 7268a5ad..8314faf6 100644 --- a/starkbank/__init__.py +++ b/starkbank/__init__.py @@ -107,3 +107,6 @@ from . import splitreceiver from .splitreceiver.__splitreceiver import SplitReceiver + +from . import splitprofile +from .splitprofile.__splitprofile import SplitProfile diff --git a/starkbank/splitprofile/__init__.py b/starkbank/splitprofile/__init__.py new file mode 100644 index 00000000..e2bb42a3 --- /dev/null +++ b/starkbank/splitprofile/__init__.py @@ -0,0 +1,3 @@ +from .__splitprofile import update, query, get, page +from .log.__log import Log +from . import log diff --git a/starkbank/splitprofile/__splitprofile.py b/starkbank/splitprofile/__splitprofile.py new file mode 100644 index 00000000..b7688bf2 --- /dev/null +++ b/starkbank/splitprofile/__splitprofile.py @@ -0,0 +1,113 @@ +from ..utils import rest +from starkcore.utils.resource import Resource +from starkcore.utils.checks import check_datetime, check_date + + +class SplitProfile(Resource): + """# SplitProfile object + When you initialize a SplitProfile, the entity will not be automatically + created in the Stark Bank API. The 'create' function sends the objects + to the Stark Bank API and returns the list of created objects. + ## Parameters (required): + - interval [string]: frequency of transfer, default "week". Options: "day", "week", "month" + - delay [string]: how long the amount will stay at the workspace in milliseconds + ## Attributes (return-only): + - id [string]: unique id returned when the splitProfile is created. ex: "5656565656565656" + - delay []: 604800, + - interval []: "month", + - tags []: [], + - status [string]: current splitProfile status. ex: "created" + - created [datetime.datetime]: creation datetime for the splitProfile. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + - updated [datetime.datetime]: latest update datetime for the splitProfile. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + """ + + def __init__(self, delay, interval, tags=None, id=None, status=None, created=None, updated=None): + Resource.__init__(self, id=id) + + self.interval = interval + self.delay = delay + self.tags = tags + self.status = status + self.created = check_datetime(created) + self.updated = check_datetime(updated) + + +_resource = {"class": SplitProfile, "name": "SplitProfile"} + + +def update(splitProfile, user=None): + """# Create SplitProfile + Send a list of SplitProfile objects for creation in the Stark Bank API + ## Parameters (required): + - splitProfile [list of SplitProfile objects]: list of SplitProfile objects to be created in the API + ## Parameters (optional): + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call + ## Return: + - list of SplitProfile objects with updated attributes + """ + return rest.put_raw(resource=_resource, entities=splitProfile, user=user) + + +def get(id, user=None): + """# Retrieve a specific SplitProfile + Receive a single SplitProfile object previously created in the Stark Bank API by its id + ## Parameters (required): + - id [string]: object unique id. ex: "5656565656565656" + ## Parameters (optional): + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call + ## Return: + - SplitProfile object with updated attributes + """ + return rest.get_id(resource=_resource, id=id, user=user) + + +def query(limit=None, after=None, before=None, transaction_ids=None, status=None, tax_id=None, sort=None, tags=None, ids=None, user=None): + """# Retrieve SplitProfile + Receive a generator of SplitProfile objects previously created in the Stark Bank API + ## Parameters (optional): + - limit [integer, default None]: maximum number of objects to be retrieved. Unlimited if None. ex: 35 + - after [datetime.date or string, default None]: date filter for objects created or updated only after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None]: date filter for objects created or updated only before specified date. ex: datetime.date(2020, 3, 10) + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call + ## Return: + - generator of SplitProfile objects with updated attributes + """ + return rest.get_stream( + resource=_resource, + limit=limit, + after=check_date(after), + before=check_date(before), + user=user, + ) + + +def page(cursor=None, after=None, before=None, tags=None, ids=None, receiver_ids=None, status=None, limit=None, user=None): + """# Retrieve paged Split Profiles + Receive a list of up to 100 Split Profiles objects previously created in the Stark Bank API and the cursor to the next page. + Use this function instead of query if you want to manually page your requests. + ## Parameters (optional): + - cursor [string, default None]: cursor returned on the previous page function call + - limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + - after [datetime.date or string, default None] date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None] date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10) + - tags [list of strings, default None]: tags to filter retrieved objects. ex: ["tony", "stark"] + - ids [list of strings, default None]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + - receiver_ids [list of strings, default None]: list of receiver ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + - status [string, default None]: filter for status of retrieved objects. ex: "success" + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call + ## Return: + - list of Split objects with updated attributes + - cursor to retrieve the next page of Split objects + """ + return rest.get_page( + resource=_resource, + cursor=cursor, + limit=limit, + after=check_date(after), + before=check_date(before), + tags=tags, + ids=ids, + receiver_ids=receiver_ids, + status=status, + user=user, + ) diff --git a/starkbank/splitprofile/log/__init__.py b/starkbank/splitprofile/log/__init__.py new file mode 100644 index 00000000..329366b4 --- /dev/null +++ b/starkbank/splitprofile/log/__init__.py @@ -0,0 +1 @@ +from .__log import get, query, page diff --git a/starkbank/splitprofile/log/__log.py b/starkbank/splitprofile/log/__log.py new file mode 100644 index 00000000..b1b421fe --- /dev/null +++ b/starkbank/splitprofile/log/__log.py @@ -0,0 +1,96 @@ +from ...utils import rest +from starkcore.utils.api import from_api_json +from starkcore.utils.resource import Resource +from starkcore.utils.checks import check_datetime, check_date +from ..__splitprofile import _resource as _profile_resource + + +class Log(Resource): + """# splitprofile.Log object + Every time a splitprofile entity is modified, a corresponding splitprofile.Log + is generated for the entity. This log is never generated by the + user, but it can be retrieved to check additional information + on the splitprofile. + ## Attributes (return-only): + - id [string]: unique id returned when the log is created. ex: "5656565656565656" + - profile [splitprofile]: splitprofile entity to which the log refers to. + - errors [list of strings]: list of errors linked to this splitprofile event. + - type [string]: type of the splitprofile event which triggered the log creation. ex: "success" or "failed" + - created [datetime.datetime]: creation datetime for the log. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + """ + + def __init__(self, id, created, type, errors, profile): + Resource.__init__(self, id=id) + + self.created = check_datetime(created) + self.type = type + self.errors = errors + self.profile = from_api_json(_profile_resource, profile) + + +_resource = {"class": Log, "name": "SplitProfileLog"} + + +def get(id, user=None): + """# Retrieve a specific splitprofile.Log + Receive a single splitprofile.Log object previously created by the Stark Bank API by its id + ## Parameters (required): + - id [string]: object unique id. ex: "5656565656565656" + ## Parameters (optional): + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call + ## Return: + - splitprofile.Log object with updated attributes + """ + return rest.get_id(resource=_resource, id=id, user=user) + + +def query(limit=None, after=None, before=None, types=None, profile_ids=None, user=None): + """# Retrieve splitprofile.Logs + Receive a generator of splitprofile.Log objects previously created in the Stark Bank API + ## Parameters (optional): + - limit [integer, default None]: maximum number of objects to be retrieved. Unlimited if None. ex: 35 + - after [datetime.date or string, default None] date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None] date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10) + - types [list of strings, default None]: filter retrieved objects by event types. ex: "processing" or "success" + - profile_ids [list of strings, default None]: list of splitprofile ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call + ## Return: + - generator of splitprofile.Log objects with updated attributes + """ + return rest.get_stream( + resource=_resource, + limit=limit, + after=check_date(after), + before=check_date(before), + types=types, + profile_ids=profile_ids, + user=user, + ) + + +def page(cursor=None, limit=None, after=None, before=None, types=None, profile_ids=None, user=None): + """# Retrieve paged splitprofile.Logs + Receive a list of up to 100 splitprofile.Log objects previously created in the Stark Bank API and the cursor to the next page. + Use this function instead of query if you want to manually page your requests. + ## Parameters (optional): + - cursor [string, default None]: cursor returned on the previous page function call + - limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + - after [datetime.date or string, default None] date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None] date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10) + - types [list of strings, default None]: filter retrieved objects by event types. ex: "processing" or "success" + - profile_ids [list of strings, default None]: list of splitprofile ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call + ## Return: + - list of splitprofile.Log objects with updated attributes + - cursor to retrieve the next page of splitprofile.Log objects + """ + return rest.get_page( + resource=_resource, + cursor=cursor, + limit=limit, + after=check_date(after), + before=check_date(before), + types=types, + profile_ids=profile_ids, + user=user, + ) diff --git a/starkbank/utils/rest.py b/starkbank/utils/rest.py index 7027f57a..1428199e 100644 --- a/starkbank/utils/rest.py +++ b/starkbank/utils/rest.py @@ -14,3 +14,4 @@ get_raw = set_relay(rest.get_raw) post_raw = set_relay(rest.post_raw) patch_id = set_relay(rest.patch_id) +put_raw = set_relay(rest.put_raw) diff --git a/tests/sdk/test_split_profile.py b/tests/sdk/test_split_profile.py new file mode 100644 index 00000000..437dfd5b --- /dev/null +++ b/tests/sdk/test_split_profile.py @@ -0,0 +1,79 @@ +import starkbank +from datetime import timedelta, date +from unittest import TestCase, main +from tests.utils.user import exampleProject, exampleOrganization + + +starkbank.user = exampleProject + +class TestSplitProfileUpdate(TestCase): + + def test_success(self): + splitprofiles =[{ + "interval": "day", + "delay": 0 + }] + response = starkbank.splitprofile.update(splitprofiles) + self.assertEqual(len(splitprofiles), 1) + for splitprofile in response: + self.assertIsNotNone(splitprofile.id) + + +class TestSplitProfileQuery(TestCase): + + def test_success(self): + splitprofiles = list(starkbank.splitprofile.query(limit=2)) + for profile in splitprofiles: + print(profile) + assert len(splitprofiles) == 1 + + def test_success_with_params(self): + splitprofiles = starkbank.splitprofile.query( + limit=2, + after=date.today() - timedelta(days=100), + before=date.today(), + status="created", + tags=["test"], + ) + for receiver in splitprofiles: + print(receiver) + self.assertEqual(len(list(splitprofiles)), 0) + + +class TestSplitProfilePage(TestCase): + + def test_success(self): + cursor = None + ids = [] + for _ in range(2): + logs, cursor = starkbank.splitprofile.page(limit=2, cursor=cursor) + for log in logs: + print(log) + self.assertFalse(log.id in ids) + ids.append(log.id) + if cursor is None: + break + self.assertTrue(len(ids) == 1) + + +class TestSplitProfileInfoGet(TestCase): + + def test_success(self): + splitprofiles = starkbank.splitprofile.query() + splitprofile_id = next(splitprofiles).id + splitprofile = starkbank.splitprofile.get(splitprofile_id) + self.assertIsNotNone(splitprofile.id) + self.assertEqual(splitprofile.id, splitprofile_id) + + def test_success_ids(self): + splitProfiles = starkbank.splitprofile.query(limit=2) + splitProfiles_ids_expected = [t.id for t in splitProfiles] + splitProfiles_ids_result = [t.id for t in starkbank.splitprofile.query(ids=splitProfiles_ids_expected)] + splitProfiles_ids_expected.sort() + splitProfiles_ids_result.sort() + self.assertTrue(splitProfiles_ids_result) + self.assertEqual(splitProfiles_ids_expected, splitProfiles_ids_result) + + +if __name__ == '__main__': + main() diff --git a/tests/sdk/test_split_profile_log.py b/tests/sdk/test_split_profile_log.py new file mode 100644 index 00000000..99154401 --- /dev/null +++ b/tests/sdk/test_split_profile_log.py @@ -0,0 +1,45 @@ +import starkbank +from unittest import TestCase, main +from tests.utils.user import exampleProject + + +starkbank.user = exampleProject + + +class TestSplitProfileLogQuery(TestCase): + + def test_success(self): + ids = [] + logs = list(starkbank.splitprofile.log.query(limit=10)) + for log in logs: + self.assertFalse(log.id in ids) + ids.append(log.id) + self.assertTrue(len(ids) == 10) + + +class TestSplitProfileLogPage(TestCase): + + def test_success(self): + cursor = None + ids = [] + for _ in range(2): + logs, cursor = starkbank.splitprofile.log.page(limit=2, cursor=cursor) + for log in logs: + print(log) + self.assertFalse(log.id in ids) + ids.append(log.id) + if cursor is None: + break + self.assertTrue(len(ids) == 4) + + +class TestSplitProfileLogInfoGet(TestCase): + + def test_success(self): + logs = starkbank.splitprofile.log.query() + log_id = next(logs).id + log = starkbank.splitprofile.log.get(id=log_id) + print(log) + +if __name__ == '__main__': + main()