diff --git a/starkbank/Request/__init__.py b/starkbank/Request/__init__.py new file mode 100644 index 0000000..283c3a0 --- /dev/null +++ b/starkbank/Request/__init__.py @@ -0,0 +1 @@ +from .__request import (get, post, patch, put, delete) diff --git a/starkbank/Request/__request.py b/starkbank/Request/__request.py new file mode 100644 index 0000000..d82301d --- /dev/null +++ b/starkbank/Request/__request.py @@ -0,0 +1,51 @@ +from ..utils import rest + + +def get(path, query=None, user=None): + """# Retrieve any StarkBank resource + Receive a json of resources previously created in the Stark Bank API + ## Parameters (required): + - path [string]: StarkBank resource's route. ex: "/invoice/" + - query [dict, default None]: Query parameters. ex: {"limit": 1, "status": paid} + ## Parameters (optional): + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user was set before function call + ## Return: + - generator of Invoice objects with updated attributes + """ + return rest.get_raw( + path=path, + query=query, + user=user + ) + + +def post(path, body=None, user=None): + return rest.post_raw( + path=path, + payload=body, + user=user + ) + + +def patch(path, body=None, user=None): + return rest.patch_raw( + path=path, + payload=body, + user=user + ) + + +def put(path, body=None, user=None): + return rest.put_raw( + path=path, + payload=body, + user=user + ) + + +def delete(path, body=None, user=None): + return rest.delete_raw( + path=path, + payload=body, + user=user + ) \ No newline at end of file diff --git a/starkbank/__init__.py b/starkbank/__init__.py index 532aa5c..a77e815 100644 --- a/starkbank/__init__.py +++ b/starkbank/__init__.py @@ -110,3 +110,6 @@ from . import splitprofile from .splitprofile.__splitprofile import SplitProfile + +from . import Request + diff --git a/starkbank/utils/rest.py b/starkbank/utils/rest.py index 0f550a7..f21a322 100644 --- a/starkbank/utils/rest.py +++ b/starkbank/utils/rest.py @@ -14,4 +14,7 @@ get_raw = set_relay(rest.get_raw) post_raw = set_relay(rest.post_raw) patch_id = set_relay(rest.patch_id) -put_multi = set_relay(rest.put_multi) +put_raw = set_relay(rest.put_raw) +patch_raw = set_relay(rest.patch_raw) +delete_raw = set_relay(rest.delete_raw) + diff --git a/tests/sdk/test_request.py b/tests/sdk/test_request.py new file mode 100644 index 0000000..1b48339 --- /dev/null +++ b/tests/sdk/test_request.py @@ -0,0 +1,199 @@ +import starkbank +from datetime import datetime, timedelta +import time +from unittest import TestCase, main +from tests.utils.date import randomPastDate +from tests.utils.user import exampleProject + +import json + +starkbank.user = exampleProject + + +class TestJokerGet(TestCase): + + def test_get(self): + example_id = starkbank.Request.get( + path=f'/invoice/', + query={"limit": 1, "status": "paid"}, + )["invoices"][0]["id"] + + request = starkbank.Request.get( + path=f'/invoice/{example_id}', + user=exampleProject + ) + self.assertEqual(request["invoice"]["id"], example_id) + + def test_get_pdf(self): + example_id = starkbank.Request.get( + path=f'/invoice/', + query={"limit": 10, "status": "paid"} + )["invoices"][0]["id"] + pdf = starkbank.Request.get( + path=f'/invoice/{example_id}/pdf', + ) + + self.assertGreater(len(pdf), 1000) + + def test_get_qrcode(self): + example_id = starkbank.Request.get( + path=f'/invoice/', + query={"limit": 10, "status": "paid"} + )["invoices"][0]["id"] + + qrcode = starkbank.Request.get( + path=f'/invoice/{example_id}/qrcode', + query={"size": 15}, + ) + self.assertGreater(len(qrcode), 1000) + + def test_get_devolution_receipt(self): + example_id = starkbank.Request.get( + path=f'/deposit/log/', + query={"limit": 1, "types": "reversed"} + ) + example_id = example_id["logs"][0]["id"] + devolution_pdf = starkbank.Request.get( + path=f'/deposit/log/{example_id}/pdf/', + ) + self.assertGreater(len(devolution_pdf), 1000) + + def test_get_page(self): + after = randomPastDate(days=10) + before = datetime.today() + request = starkbank.Request.get( + path=f'/invoice/', + query={ + "limit": 10, + "after": after.strftime("%Y-%m-%d"), + "before": before.strftime("%Y-%m-%d"), + "status": "paid" + } + ) + for item in request["invoices"]: + self.assertTrue(after.date() <= datetime.strptime(item["created"], "%Y-%m-%dT%H:%M:%S.%f%z").date() <= (before + timedelta(hours=3)).date()) + self.assertEqual(10, len(request["invoices"])) + + def test_get_pagination(self): + after = randomPastDate(days=10) + before = datetime.today() + total_items = 0 + cursor = None + i = 0 + while i <= 2: + request = starkbank.Request.get( + path=f'/invoice/', + query={ + "limit": 10, + "after": after.strftime("%Y-%m-%d"), + "before": before.strftime("%Y-%m-%d"), + "status": "paid", + "cursor": cursor + } + ) + cursor = request["cursor"] + total_items += len(request["invoices"]) + for item in request["invoices"]: + self.assertTrue(after.date() <= datetime.strptime(item["created"], "%Y-%m-%dT%H:%M:%S.%f%z").date() <= (before + timedelta(hours=3)).date()) + if cursor is None: + break + i += 1 + self.assertLessEqual(len(request["invoices"]), 10) + self.assertLessEqual(total_items, 30) + + +class TestJokerPost(TestCase): + + def test_post(self): + data={ + "invoices": [{ + "amount": 100, + "name": "Iron Bank S.A.", + "taxId": "20.018.183/0001-80" + }] + } + request = starkbank.Request.post( + path=f'/invoice/', + body=data, + ) + print(request) + + +class TestJokerPatch(TestCase): + + def test_patch(self): + initial_state = starkbank.Request.get( + path=f'/invoice/', + query={"limit": 1, "status": "paid"} + ) + example_id = initial_state["invoices"][0]["id"] + amount = initial_state["invoices"][0]["amount"] + + request = starkbank.Request.patch( + path=f'/invoice/{example_id}/', + body={"amount": amount-amount}, + ) + + final_state = starkbank.Request.get( + path=f'/invoice/{example_id}', + ) + self.assertEqual(final_state["invoice"]["amount"],0) + + +class TestJokerPut(TestCase): + + def test_put(self): + data = { + "profiles": [ + { + "interval": "day", + "delay": 0 + } + ] + } + request = starkbank.Request.put( + path=f'/split-profile/', + body=data, + ) + + result = starkbank.Request.get(path=f'/split-profile/') + + self.assertEqual(result["profiles"][0]["delay"], 0) + self.assertEqual(result["profiles"][0]["interval"], "day") + + +class TestJokerDelete(TestCase): + + def test_delete(self): + future_date = datetime.now().date() + timedelta(days=10) + + data = { + "transfers": [ + { + "amount": 10000, + "name": "Steve Rogers", + "taxId": "330.731.970-10", + "bankCode": "001", + "branchCode": "1234", + "accountNumber": "123456-0", + "accountType": "checking", + "scheduled": future_date.strftime("%Y-%m-%d"), + "externalId": str(int(time.time() * 1000)), + } + ] + } + + create = starkbank.Request.post( + path=f'/transfer/', + body=data, + ) + + request = starkbank.Request.delete( + path=f'/transfer/{create["transfers"][0]["id"]}', + ) + + self.assertEqual(request['message'], 'Transfer(s) successfully deleted') + + +if __name__ == '__main__': + main()