Skip to content

Commit e44a2bb

Browse files
Add Split Profile resource
1 parent 30dcd53 commit e44a2bb

File tree

8 files changed

+343
-0
lines changed

8 files changed

+343
-0
lines changed

starkbank/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,6 @@
107107

108108
from . import splitreceiver
109109
from .splitreceiver.__splitreceiver import SplitReceiver
110+
111+
from . import splitprofile
112+
from .splitprofile.__splitprofile import SplitProfile

starkbank/splitprofile/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .__splitprofile import update, query, get, page
2+
from .log.__log import Log
3+
from . import log
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
from ..utils import rest
2+
from starkcore.utils.resource import Resource
3+
from starkcore.utils.checks import check_datetime, check_date
4+
5+
6+
class SplitProfile(Resource):
7+
"""# SplitProfile object
8+
When you initialize a SplitProfile, the entity will not be automatically
9+
created in the Stark Bank API. The 'create' function sends the objects
10+
to the Stark Bank API and returns the list of created objects.
11+
## Parameters (required):
12+
- interval [string]: frequency of transfer, default "week". Options: "day", "week", "month"
13+
- delay [string]: how long the amount will stay at the workspace in milliseconds
14+
## Attributes (return-only):
15+
- id [string]: unique id returned when the splitProfile is created. ex: "5656565656565656"
16+
- delay []: 604800,
17+
- interval []: "month",
18+
- tags []: [],
19+
- status [string]: current splitProfile status. ex: "created"
20+
- created [datetime.datetime]: creation datetime for the splitProfile. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0)
21+
- updated [datetime.datetime]: latest update datetime for the splitProfile. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0)
22+
"""
23+
24+
def __init__(self, delay, interval, tags=None, id=None, status=None, created=None, updated=None):
25+
Resource.__init__(self, id=id)
26+
27+
self.interval = interval
28+
self.delay = delay
29+
self.tags = tags
30+
self.status = status
31+
self.created = check_datetime(created)
32+
self.updated = check_datetime(updated)
33+
34+
35+
_resource = {"class": SplitProfile, "name": "SplitProfile"}
36+
37+
38+
def update(splitProfile, user=None):
39+
"""# Create SplitProfile
40+
Send a list of SplitProfile objects for creation in the Stark Bank API
41+
## Parameters (required):
42+
- splitProfile [list of SplitProfile objects]: list of SplitProfile objects to be created in the API
43+
## Parameters (optional):
44+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call
45+
## Return:
46+
- list of SplitProfile objects with updated attributes
47+
"""
48+
return rest.put_raw(resource=_resource, entities=splitProfile, user=user)
49+
50+
51+
def get(id, user=None):
52+
"""# Retrieve a specific SplitProfile
53+
Receive a single SplitProfile object previously created in the Stark Bank API by its id
54+
## Parameters (required):
55+
- id [string]: object unique id. ex: "5656565656565656"
56+
## Parameters (optional):
57+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call
58+
## Return:
59+
- SplitProfile object with updated attributes
60+
"""
61+
return rest.get_id(resource=_resource, id=id, user=user)
62+
63+
64+
def query(limit=None, after=None, before=None, transaction_ids=None, status=None, tax_id=None, sort=None, tags=None, ids=None, user=None):
65+
"""# Retrieve SplitProfile
66+
Receive a generator of SplitProfile objects previously created in the Stark Bank API
67+
## Parameters (optional):
68+
- limit [integer, default None]: maximum number of objects to be retrieved. Unlimited if None. ex: 35
69+
- after [datetime.date or string, default None]: date filter for objects created or updated only after specified date. ex: datetime.date(2020, 3, 10)
70+
- before [datetime.date or string, default None]: date filter for objects created or updated only before specified date. ex: datetime.date(2020, 3, 10)
71+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call
72+
## Return:
73+
- generator of SplitProfile objects with updated attributes
74+
"""
75+
return rest.get_stream(
76+
resource=_resource,
77+
limit=limit,
78+
after=check_date(after),
79+
before=check_date(before),
80+
user=user,
81+
)
82+
83+
84+
def page(cursor=None, after=None, before=None, tags=None, ids=None, receiver_ids=None, status=None, limit=None, user=None):
85+
"""# Retrieve paged Split Profiles
86+
Receive a list of up to 100 Split Profiles objects previously created in the Stark Bank API and the cursor to the next page.
87+
Use this function instead of query if you want to manually page your requests.
88+
## Parameters (optional):
89+
- cursor [string, default None]: cursor returned on the previous page function call
90+
- limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50
91+
- after [datetime.date or string, default None] date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10)
92+
- before [datetime.date or string, default None] date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10)
93+
- tags [list of strings, default None]: tags to filter retrieved objects. ex: ["tony", "stark"]
94+
- ids [list of strings, default None]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"]
95+
- receiver_ids [list of strings, default None]: list of receiver ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"]
96+
- status [string, default None]: filter for status of retrieved objects. ex: "success"
97+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call
98+
## Return:
99+
- list of Split objects with updated attributes
100+
- cursor to retrieve the next page of Split objects
101+
"""
102+
return rest.get_page(
103+
resource=_resource,
104+
cursor=cursor,
105+
limit=limit,
106+
after=check_date(after),
107+
before=check_date(before),
108+
tags=tags,
109+
ids=ids,
110+
receiver_ids=receiver_ids,
111+
status=status,
112+
user=user,
113+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .__log import get, query, page

starkbank/splitprofile/log/__log.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from ...utils import rest
2+
from starkcore.utils.api import from_api_json
3+
from starkcore.utils.resource import Resource
4+
from starkcore.utils.checks import check_datetime, check_date
5+
from ..__splitprofile import _resource as _profile_resource
6+
7+
8+
class Log(Resource):
9+
"""# splitprofile.Log object
10+
Every time a SplitProfile entity is modified, a corresponding splitprofile.Log
11+
is generated for the entity. This log is never generated by the
12+
user, but it can be retrieved to check additional information
13+
on the SplitProfile.
14+
## Attributes (return-only):
15+
- id [string]: unique id returned when the log is created. ex: "5656565656565656"
16+
- profile [splitProfile]: SplitProfile entity to which the log refers to.
17+
- errors [list of strings]: list of errors linked to this SplitProfile event.
18+
- type [string]: type of the SplitProfile event which triggered the log creation. ex: "success" or "failed"
19+
- created [datetime.datetime]: creation datetime for the log. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0)
20+
"""
21+
22+
def __init__(self, id, created, type, errors, profile):
23+
Resource.__init__(self, id=id)
24+
25+
self.created = check_datetime(created)
26+
self.type = type
27+
self.errors = errors
28+
self.profile = from_api_json(_profile_resource, profile)
29+
30+
31+
_resource = {"class": Log, "name": "SplitProfileLog"}
32+
33+
34+
def get(id, user=None):
35+
"""# Retrieve a specific splitprofile.Log
36+
Receive a single SplitProfile.Log object previously created by the Stark Bank API by its id
37+
## Parameters (required):
38+
- id [string]: object unique id. ex: "5656565656565656"
39+
## Parameters (optional):
40+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call
41+
## Return:
42+
- splitprofile.Log object with updated attributes
43+
"""
44+
return rest.get_id(resource=_resource, id=id, user=user)
45+
46+
47+
def query(limit=None, after=None, before=None, types=None, profile_ids=None, user=None):
48+
"""# Retrieve splitprofile.Logs
49+
Receive a generator of SplitProfile.Log objects previously created in the Stark Bank API
50+
## Parameters (optional):
51+
- limit [integer, default None]: maximum number of objects to be retrieved. Unlimited if None. ex: 35
52+
- after [datetime.date or string, default None] date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10)
53+
- before [datetime.date or string, default None] date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10)
54+
- types [list of strings, default None]: filter retrieved objects by event types. ex: "created" or "updated"
55+
- profile_ids [list of strings, default None]: list of SplitProfile ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"]
56+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call
57+
## Return:
58+
- generator of splitprofile.Log objects with updated attributes
59+
"""
60+
return rest.get_stream(
61+
resource=_resource,
62+
limit=limit,
63+
after=check_date(after),
64+
before=check_date(before),
65+
types=types,
66+
profile_ids=profile_ids,
67+
user=user,
68+
)
69+
70+
71+
def page(cursor=None, limit=None, after=None, before=None, types=None, profile_ids=None, user=None):
72+
"""# Retrieve paged splitprofile.Logs
73+
Receive a list of up to 100 splitprofile.Log objects previously created in the Stark Bank API and the cursor to the next page.
74+
Use this function instead of query if you want to manually page your requests.
75+
## Parameters (optional):
76+
- cursor [string, default None]: cursor returned on the previous page function call
77+
- limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50
78+
- after [datetime.date or string, default None] date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10)
79+
- before [datetime.date or string, default None] date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10)
80+
- types [list of strings, default None]: filter retrieved objects by event types. ex: "created" or "updated"
81+
- profile_ids [list of strings, default None]: list of SplitProfile ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"]
82+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call
83+
## Return:
84+
- list of splitprofile.Log objects with updated attributes
85+
- cursor to retrieve the next page of splitprofile.Log objects
86+
"""
87+
return rest.get_page(
88+
resource=_resource,
89+
cursor=cursor,
90+
limit=limit,
91+
after=check_date(after),
92+
before=check_date(before),
93+
types=types,
94+
profile_ids=profile_ids,
95+
user=user,
96+
)

starkbank/utils/rest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
get_raw = set_relay(rest.get_raw)
1515
post_raw = set_relay(rest.post_raw)
1616
patch_id = set_relay(rest.patch_id)
17+
put_raw = set_relay(rest.put_raw)

tests/sdk/test_split_profile.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import starkbank
2+
from datetime import timedelta, date
3+
from unittest import TestCase, main
4+
from tests.utils.user import exampleProject, exampleOrganization
5+
6+
7+
starkbank.user = exampleProject
8+
9+
10+
class TestSplitProfileUpdate(TestCase):
11+
12+
def test_success(self):
13+
splitprofiles =[{
14+
"interval": "day",
15+
"delay": 0
16+
}]
17+
response = starkbank.splitprofile.update(splitprofiles)
18+
self.assertEqual(len(splitprofiles), 1)
19+
for splitprofile in response:
20+
self.assertIsNotNone(splitprofile.id)
21+
22+
23+
class TestSplitProfileQuery(TestCase):
24+
25+
def test_success(self):
26+
splitprofiles = list(starkbank.splitprofile.query(limit=2))
27+
for profile in splitprofiles:
28+
print(profile)
29+
assert len(splitprofiles) == 1
30+
31+
def test_success_with_params(self):
32+
splitprofiles = starkbank.splitprofile.query(
33+
limit=2,
34+
after=date.today() - timedelta(days=100),
35+
before=date.today(),
36+
status="created",
37+
tags=["test"],
38+
)
39+
for receiver in splitprofiles:
40+
print(receiver)
41+
self.assertEqual(len(list(splitprofiles)), 0)
42+
43+
44+
class TestSplitProfilePage(TestCase):
45+
46+
def test_success(self):
47+
cursor = None
48+
ids = []
49+
for _ in range(2):
50+
logs, cursor = starkbank.splitprofile.page(limit=2, cursor=cursor)
51+
for log in logs:
52+
print(log)
53+
self.assertFalse(log.id in ids)
54+
ids.append(log.id)
55+
if cursor is None:
56+
break
57+
self.assertTrue(len(ids) == 1)
58+
59+
60+
class TestSplitProfileInfoGet(TestCase):
61+
62+
def test_success(self):
63+
splitprofiles = starkbank.splitprofile.query()
64+
splitprofile_id = next(splitprofiles).id
65+
splitprofile = starkbank.splitprofile.get(splitprofile_id)
66+
self.assertIsNotNone(splitprofile.id)
67+
self.assertEqual(splitprofile.id, splitprofile_id)
68+
69+
def test_success_ids(self):
70+
splitProfiles = starkbank.splitprofile.query(limit=2)
71+
splitProfiles_ids_expected = [t.id for t in splitProfiles]
72+
splitProfiles_ids_result = [t.id for t in starkbank.splitprofile.query(ids=splitProfiles_ids_expected)]
73+
splitProfiles_ids_expected.sort()
74+
splitProfiles_ids_result.sort()
75+
self.assertTrue(splitProfiles_ids_result)
76+
self.assertEqual(splitProfiles_ids_expected, splitProfiles_ids_result)
77+
78+
79+
if __name__ == '__main__':
80+
main()

tests/sdk/test_split_profile_log.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import starkbank
2+
from unittest import TestCase, main
3+
from tests.utils.user import exampleProject
4+
5+
6+
starkbank.user = exampleProject
7+
8+
9+
class TestSplitProfileLogQuery(TestCase):
10+
11+
def test_success(self):
12+
ids = []
13+
logs = list(starkbank.splitprofile.log.query(limit=10))
14+
for log in logs:
15+
self.assertFalse(log.id in ids)
16+
ids.append(log.id)
17+
self.assertTrue(len(ids) == 10)
18+
19+
20+
class TestSplitProfileLogPage(TestCase):
21+
22+
def test_success(self):
23+
cursor = None
24+
ids = []
25+
for _ in range(2):
26+
logs, cursor = starkbank.splitprofile.log.page(limit=2, cursor=cursor)
27+
for log in logs:
28+
print(log)
29+
self.assertFalse(log.id in ids)
30+
ids.append(log.id)
31+
if cursor is None:
32+
break
33+
self.assertTrue(len(ids) == 4)
34+
35+
36+
class TestSplitProfileLogInfoGet(TestCase):
37+
38+
def test_success(self):
39+
logs = starkbank.splitprofile.log.query()
40+
log_id = next(logs).id
41+
log = starkbank.splitprofile.log.get(id=log_id)
42+
print(log)
43+
44+
45+
if __name__ == '__main__':
46+
main()

0 commit comments

Comments
 (0)