From cf99dc83c168fa98f56d638acb0f8ea55d046ebb Mon Sep 17 00:00:00 2001 From: Leonardo Kagohara Date: Thu, 27 Jun 2024 14:38:44 -0300 Subject: [PATCH] Fix request param prefix --- CHANGELOG.md | 2 + README.md | 20 ++++---- requirements.txt | 2 +- starkbank/__init__.py | 1 + starkbank/corporatecard/__corporatecard.py | 2 +- starkbank/request/__request.py | 21 ++++++-- tests/sdk/test_request.py | 56 ++++++++++++---------- tests/utils/holder.py | 5 +- 8 files changed, 64 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21438e3..de2782d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ Given a version number MAJOR.MINOR.PATCH, increment: - request methods ### Changed - core version +### Fixed +- request prefix param ## [2.25.1] - 2024-04-01 ### Fixed diff --git a/README.md b/README.md index 87cf228..2869226 100644 --- a/README.md +++ b/README.md @@ -2605,7 +2605,7 @@ import starkbank example_id = "5155165527080960" request = starkbank.request.get( path=f"/invoice/{example_id}" -) +).json() print(request) ``` @@ -2618,7 +2618,7 @@ import starkbank example_id = "5699165527090460" request = starkbank.request.get( path=f"/invoice/log/{example_id}", -) +).json() print(request) ``` @@ -2631,7 +2631,7 @@ import starkbank request = starkbank.request.get( path="/invoice", query={"limit": 10, "status": "paid"}, -) +).json() for item in request["invoices"]: print(item) @@ -2645,7 +2645,7 @@ import starkbank request = starkbank.request.get( path="/invoice/log", query={"limit": 10, "status": "paid"}, -) +).json() for item in request["invoices"]: print(item) @@ -2659,7 +2659,7 @@ import starkbank example_id = "5155165527080960" pdf = starkbank.request.get( path=f"/invoice/{example_id}/pdf", -) +).content with open("request.pdf", "wb") as file: file.write(pdf) ``` @@ -2692,11 +2692,11 @@ data={ request = starkbank.request.post( path="/invoice", body=data, -) +).json() print(request) ``` -## patch +## PATCH You can perform a PATCH request to any StarkBank route. @@ -2708,7 +2708,7 @@ example_id = "5155165527080960" request = starkbank.request.patch( path=f"/invoice/{example_id}", body={"amount": 0}, -) +).json() print(request) ``` @@ -2731,7 +2731,7 @@ data = { request = starkbank.request.put( path="/split-profile", body=data, -) +).json() print(request) ``` ## DELETE @@ -2745,7 +2745,7 @@ import starkbank example_id = "5155165527080960" request = starkbank.request.delete( path=f"/transfer/{example_id}", -) +).json() print(request) ``` # Handling errors diff --git a/requirements.txt b/requirements.txt index fc01935..8ba584e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -starkcore==0.3.2 +starkcore==0.5.0 diff --git a/starkbank/__init__.py b/starkbank/__init__.py index d070873..2567703 100644 --- a/starkbank/__init__.py +++ b/starkbank/__init__.py @@ -3,6 +3,7 @@ user = None language = "en-US" timeout = 15 +request_methods_prefix = "Joker" from starkcore import Organization, Project diff --git a/starkbank/corporatecard/__corporatecard.py b/starkbank/corporatecard/__corporatecard.py index 13e684a..6b5dd12 100644 --- a/starkbank/corporatecard/__corporatecard.py +++ b/starkbank/corporatecard/__corporatecard.py @@ -77,7 +77,7 @@ def create(card, expand=None, user=None): - CorporateCard object with updated attributes """ path = "{endpoint}/{sub_resource}".format(endpoint=endpoint(_resource), sub_resource="token") - json = rest.post_raw(path=path, payload=api_json(card), expand=expand, user=user) + json = rest.post_raw(path=path, payload=api_json(card), query={"expand": expand}, user=user).json() return from_api_json(_resource, json[last_name(_resource)]) diff --git a/starkbank/request/__request.py b/starkbank/request/__request.py index 22fa28f..b3dfb73 100644 --- a/starkbank/request/__request.py +++ b/starkbank/request/__request.py @@ -1,4 +1,5 @@ from ..utils import rest +from starkbank import request_methods_prefix def get(path, query=None, user=None): @@ -16,7 +17,9 @@ def get(path, query=None, user=None): return rest.get_raw( path=path, query=query, - user=user + user=user, + prefix=request_methods_prefix, + raiseException=False ) @@ -37,7 +40,9 @@ def post(path, body=None, query=None, user=None): path=path, payload=body, query=query, - user=user + user=user, + prefix=request_methods_prefix, + raiseException=False ) @@ -56,7 +61,9 @@ def patch(path, body=None, user=None): return rest.patch_raw( path=path, payload=body, - user=user + user=user, + prefix=request_methods_prefix, + raiseException=False ) @@ -76,7 +83,9 @@ def put(path, body=None, user=None): return rest.put_raw( path=path, payload=body, - user=user + user=user, + prefix=request_methods_prefix, + raiseException=False ) @@ -96,5 +105,7 @@ def delete(path, body=None, user=None): return rest.delete_raw( path=path, payload=body, - user=user + user=user, + prefix=request_methods_prefix, + raiseException=False ) diff --git a/tests/sdk/test_request.py b/tests/sdk/test_request.py index 4f712fd..ce55d9d 100644 --- a/tests/sdk/test_request.py +++ b/tests/sdk/test_request.py @@ -12,25 +12,30 @@ class TestJokerGet(TestCase): def test_get(self): - example_id = starkbank.request.get( - path=f'/invoice/', - query={"limit": 1, "status": "paid"}, - )["invoices"][0]["id"] + try: + example_id = starkbank.request.get( + path=f'/invoice/', + query={"limit": 1, "status": "paid"}, + ) - request = starkbank.request.get( - path=f'/invoice/{example_id}', - user=exampleProject - ) - self.assertEqual(request["invoice"]["id"], example_id) + request = starkbank.request.get( + path=f'/invoice/{example_id.json()["invoices"][0]["id"]}', + user=exampleProject + ) + if request.status != 200: + raise Exception(request.content) + self.assertEqual(request.json()["invoice"]["id"], example_id.json()["invoices"][0]["id"]) + except Exception: + raise Exception def test_get_pdf(self): example_id = starkbank.request.get( path=f'/invoice/', query={"limit": 10, "status": "paid"} - )["invoices"][0]["id"] + ).json()["invoices"][0]["id"] pdf = starkbank.request.get( path=f'/invoice/{example_id}/pdf', - ) + ).content self.assertGreater(len(pdf), 1000) @@ -38,23 +43,22 @@ def test_get_qrcode(self): example_id = starkbank.request.get( path=f'/invoice/', query={"limit": 10, "status": "paid"} - )["invoices"][0]["id"] + ).json()["invoices"][0]["id"] qrcode = starkbank.request.get( path=f'/invoice/{example_id}/qrcode', query={"size": 15}, - ) + ).content self.assertGreater(len(qrcode), 1000) def test_get_reversal_receipt(self): example_id = starkbank.request.get( path=f'/deposit/log/', query={"limit": 1, "types": "reversed"} - ) - example_id = example_id["logs"][0]["id"] + ).json()["logs"][0]["id"] reversal_pdf = starkbank.request.get( path=f'/deposit/log/{example_id}/pdf/', - ) + ).content self.assertGreater(len(reversal_pdf), 1000) def test_get_page(self): @@ -68,7 +72,7 @@ def test_get_page(self): "before": before.strftime("%Y-%m-%d"), "status": "paid" } - ) + ).json() 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"])) @@ -89,7 +93,7 @@ def test_get_pagination(self): "status": "paid", "cursor": cursor } - ) + ).json() cursor = request["cursor"] total_items += len(request["invoices"]) for item in request["invoices"]: @@ -114,7 +118,7 @@ def test_post(self): request = starkbank.request.post( path=f'/invoice/', body=data, - ) + ).json() print(request) @@ -124,18 +128,18 @@ def test_patch(self): initial_state = starkbank.request.get( path=f'/invoice/', query={"limit": 1, "status": "paid"} - ) + ).json() example_id = initial_state["invoices"][0]["id"] amount = initial_state["invoices"][0]["amount"] starkbank.request.patch( path=f'/invoice/{example_id}/', body={"amount": amount - amount}, - ) + ).json() final_state = starkbank.request.get( path=f'/invoice/{example_id}', - ) + ).json() self.assertEqual(final_state["invoice"]["amount"],0) @@ -155,7 +159,7 @@ def test_put(self): body=data, ) - result = starkbank.request.get(path=f'/split-profile/') + result = starkbank.request.get(path=f'/split-profile/').json() self.assertEqual(result["profiles"][0]["delay"], 0) self.assertEqual(result["profiles"][0]["interval"], "day") @@ -185,15 +189,15 @@ def test_delete(self): create = starkbank.request.post( path=f'/transfer/', body=data, - ) + ).json() starkbank.request.delete( path=f'/transfer/{create["transfers"][0]["id"]}', - ) + ).json() final_status = starkbank.request.get( path=f'/transfer/{create["transfers"][0]["id"]}', - )["transfer"]["status"] + ).json()["transfer"]["status"] self.assertEqual(final_status, 'canceled') diff --git a/tests/utils/holder.py b/tests/utils/holder.py index e65b7af..3742b3c 100644 --- a/tests/utils/holder.py +++ b/tests/utils/holder.py @@ -3,9 +3,11 @@ from uuid import uuid4 import starkbank.corporateholder from starkbank import CorporateHolder +from tests.utils.user import exampleProject from .names.names import get_full_name from .rule import generateExampleRuleJson +owner_id = os.environ["SANDBOX_ID"] if "SANDBOX_ID" in os.environ else exampleProject.id example_holder = CorporateHolder( name="Iron Bank S.A." + str(uuid4()), @@ -14,13 +16,12 @@ ], permissions=[ starkbank.corporateholder.Permission( - owner_id=os.environ["SANDBOX_ID"], + owner_id=owner_id, owner_type="project" ) ] ) - def generateExampleHoldersJson(n=1): holders = [] for _ in range(n):