Skip to content

Commit 3a6ff39

Browse files
Merge pull request #127 from starkbank/feature/request-methods
Add request methods
2 parents 4318460 + fe340b1 commit 3a6ff39

13 files changed

+519
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Given a version number MAJOR.MINOR.PATCH, increment:
1313

1414

1515
## [Unreleased]
16+
### Added
17+
- request methods
18+
### Changed
19+
- core version
1620

1721
## [2.25.1] - 2024-04-01
1822
### Fixed

README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ is as easy as sending a text message to your client!
5555
- [WebhookEvents](#process-webhook-events): Manage webhook events
5656
- [WebhookEventAttempts](#query-failed-webhook-event-delivery-attempts-information): Query failed webhook event deliveries
5757
- [Workspaces](#create-a-new-workspace): Manage your accounts
58+
- [Request](#request): Send a custom request to Stark Bank. This can be used to access features that haven't been mapped yet.
5859
- [Handling errors](#handling-errors)
5960
- [Help and Feedback](#help-and-feedback)
6061

@@ -2588,6 +2589,165 @@ print(workspace)
25882589

25892590
**Note**: the Organization user can only update a workspace with the Workspace ID set.
25902591

2592+
# request
2593+
2594+
This resource allows you to send HTTP requests to StarkBank routes.
2595+
2596+
## GET
2597+
2598+
You can perform a GET request to any StarkBank route.
2599+
2600+
It's possible to get a single resource using its id in the path.
2601+
2602+
```python
2603+
import starkbank
2604+
2605+
example_id = "5155165527080960"
2606+
request = starkbank.request.get(
2607+
path=f"/invoice/{example_id}"
2608+
)
2609+
2610+
print(request)
2611+
```
2612+
2613+
You can also get the specific resource log,
2614+
2615+
```python
2616+
import starkbank
2617+
2618+
example_id = "5699165527090460"
2619+
request = starkbank.request.get(
2620+
path=f"/invoice/log/{example_id}",
2621+
)
2622+
2623+
print(request)
2624+
```
2625+
2626+
This same method will be used to list all created items for the requested resource.
2627+
2628+
```python
2629+
import starkbank
2630+
2631+
request = starkbank.request.get(
2632+
path="/invoice",
2633+
query={"limit": 10, "status": "paid"},
2634+
)
2635+
2636+
for item in request["invoices"]:
2637+
print(item)
2638+
```
2639+
2640+
To list logs, you will use the same logic as for getting a single log.
2641+
2642+
```python
2643+
import starkbank
2644+
2645+
request = starkbank.request.get(
2646+
path="/invoice/log",
2647+
query={"limit": 10, "status": "paid"},
2648+
)
2649+
2650+
for item in request["invoices"]:
2651+
print(item)
2652+
```
2653+
2654+
You can get a resource file using this method.
2655+
2656+
```python
2657+
import starkbank
2658+
2659+
example_id = "5155165527080960"
2660+
pdf = starkbank.request.get(
2661+
path=f"/invoice/{example_id}/pdf",
2662+
)
2663+
with open("request.pdf", "wb") as file:
2664+
file.write(pdf)
2665+
```
2666+
2667+
## POST
2668+
2669+
You can perform a POST request to any StarkBank route.
2670+
2671+
This will create an object for each item sent in your request
2672+
2673+
**Note**: It's not possible to create multiple resources simultaneously. You need to send separate requests if you want to create multiple resources, such as invoices and boletos.
2674+
2675+
```python
2676+
import starkbank
2677+
2678+
data={
2679+
"invoices": [
2680+
{
2681+
"amount": 100,
2682+
"name": "Iron Bank S.A.",
2683+
"taxId": "20.018.183/0001-80"
2684+
},
2685+
{
2686+
"amount": 450000,
2687+
"name": "Arya Stark.",
2688+
"taxId": "012.345.678-90"
2689+
}
2690+
]
2691+
}
2692+
request = starkbank.request.post(
2693+
path="/invoice",
2694+
body=data,
2695+
)
2696+
print(request)
2697+
```
2698+
2699+
## patch
2700+
2701+
You can perform a PATCH request to any StarkBank route.
2702+
2703+
It's possible to update a single item of a StarkBank resource.
2704+
```python
2705+
import starkbank
2706+
2707+
example_id = "5155165527080960"
2708+
request = starkbank.request.patch(
2709+
path=f"/invoice/{example_id}",
2710+
body={"amount": 0},
2711+
)
2712+
print(request)
2713+
```
2714+
2715+
## PUT
2716+
2717+
You can perform a PUT request to any StarkBank route.
2718+
2719+
It's possible to put a single item of a StarkBank resource.
2720+
```python
2721+
import starkbank
2722+
2723+
data = {
2724+
"profiles": [
2725+
{
2726+
"interval": "day",
2727+
"delay": 0
2728+
}
2729+
]
2730+
}
2731+
request = starkbank.request.put(
2732+
path="/split-profile",
2733+
body=data,
2734+
)
2735+
print(request)
2736+
```
2737+
## DELETE
2738+
2739+
You can perform a DELETE request to any StarkBank route.
2740+
2741+
It's possible to delete a single item of a StarkBank resource.
2742+
```python
2743+
import starkbank
2744+
2745+
example_id = "5155165527080960"
2746+
request = starkbank.request.delete(
2747+
path=f"/transfer/{example_id}",
2748+
)
2749+
print(request)
2750+
```
25912751
# Handling errors
25922752

25932753
The SDK may raise one of four types of errors: __InputErrors__, __InternalServerError__, __UnknownError__, __InvalidSignatureError__

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
starkcore==0.2.1
1+
starkcore==0.3.2

starkbank/__init__.py

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

111111
from . import splitprofile
112112
from .splitprofile.__splitprofile import SplitProfile
113+
114+
from . import request
115+

starkbank/request/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .__request import (get, post, patch, put, delete)

starkbank/request/__request.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from ..utils import rest
2+
3+
4+
def get(path, query=None, user=None):
5+
"""# Retrieve any StarkBank resource
6+
Receive a json of resources previously created in StarkBank's API
7+
## Parameters (required):
8+
- path [string]: StarkBank resource's route. ex: "/invoice/"
9+
- query [dict, default None]: Query parameters. ex: {"limit": 1, "status": paid}
10+
## Parameters (optional):
11+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user
12+
was set before function call
13+
## Return:
14+
- dict of StarkBank objects with updated attributes
15+
"""
16+
return rest.get_raw(
17+
path=path,
18+
query=query,
19+
user=user
20+
)
21+
22+
23+
def post(path, body=None, query=None, user=None):
24+
"""# Create any StarkBank resource
25+
Send a list of jsons and create any StarkBank resource objects
26+
## Parameters (required):
27+
- path [string]: StarkBank resource's route. ex: "/invoice/"
28+
- body [dict]: request parameters. ex: {"invoices": [{"amount": 100, "name": "Iron Bank S.A.", "taxId": "20.018.183/0001-80"}]}
29+
## Parameters (optional):
30+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user
31+
was set before function call
32+
- query [dict, default None]: Query parameters. ex: {"limit": 1, "status": paid}
33+
## Return:
34+
- list of resources jsons with updated attributes
35+
"""
36+
return rest.post_raw(
37+
path=path,
38+
payload=body,
39+
query=query,
40+
user=user
41+
)
42+
43+
44+
def patch(path, body=None, user=None):
45+
"""# Update any StarkBank resource
46+
Send a json with parameters of a single StarkBank resource object and update it
47+
## Parameters (required):
48+
- path [string]: StarkBank resource's route. ex: "/invoice/5699165527090460"
49+
- body [dict]: request parameters. ex: {"amount": 100}
50+
## Parameters (optional):
51+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user
52+
was set before function call
53+
## Return:
54+
- json of the resource with updated attributes
55+
"""
56+
return rest.patch_raw(
57+
path=path,
58+
payload=body,
59+
user=user
60+
)
61+
62+
63+
def put(path, body=None, user=None):
64+
"""# Put any StarkBank resource
65+
Send a json with parameters of a single StarkBank resource object and create it, if the resource alredy exists,
66+
you will update it.
67+
## Parameters (required):
68+
- path [string]: StarkBank resource's route. ex: "/invoice"
69+
- body [dict]: request parameters. ex: {"amount": 100}
70+
## Parameters (optional):
71+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user
72+
was set before function call
73+
## Return:
74+
- json of the resource with updated attributes
75+
"""
76+
return rest.put_raw(
77+
path=path,
78+
payload=body,
79+
user=user
80+
)
81+
82+
83+
def delete(path, body=None, user=None):
84+
"""# Delete any StarkBank resource
85+
Send a json with parameters of a single StarkBank resource object and delete it
86+
you will update it.
87+
## Parameters (required):
88+
- path [string]: StarkBank resource's route. ex: "/invoice/5699165527090460"
89+
- body [dict]: request parameters. ex: {"amount": 100}
90+
## Parameters (optional):
91+
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user
92+
was set before function call
93+
## Return:
94+
- json of the resource with updated attributes
95+
"""
96+
return rest.delete_raw(
97+
path=path,
98+
payload=body,
99+
user=user
100+
)

starkbank/utils/rest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@
1515
post_raw = set_relay(rest.post_raw)
1616
patch_id = set_relay(rest.patch_id)
1717
put_multi = set_relay(rest.put_multi)
18+
put_raw = set_relay(rest.put_raw)
19+
patch_raw = set_relay(rest.patch_raw)
20+
delete_raw = set_relay(rest.delete_raw)

tests/sdk/test_corporate_invoice.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class TestCorporateInvoiceQuery(TestCase):
1212

1313
def test_success(self):
1414
invoices = starkbank.corporateinvoice.query(
15+
limit=1,
1516
after=date.today() - timedelta(days=100),
1617
before=date.today(),
1718
)

tests/sdk/test_corporate_withdrawal.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,37 @@
33
from datetime import date, timedelta
44
from starkbank import CorporateWithdrawal
55
from tests.utils.user import exampleProject
6-
from tests.utils.withdrawal import generateExampleWithdrawalJson
6+
from tests.utils.withdrawal import generateExampleWithdrawalJson, payment_script
7+
import time
78

89
starkbank.user = exampleProject
910

1011

12+
class TestCorporateResourcesChecks(TestCase):
13+
14+
def test_success_corporate_balance(self):
15+
balance = starkbank.corporatebalance.get()
16+
initial_balance = balance.amount
17+
18+
if balance.amount < 0:
19+
request = payment_script(balance.amount)
20+
i = 0
21+
while i <= 5:
22+
br_code = starkbank.brcodepayment.get(request[0].id)
23+
if br_code.status == "success" or br_code.status == "failed":
24+
time.sleep(5)
25+
break
26+
time.sleep(10)
27+
i += 1
28+
balance = starkbank.corporatebalance.get()
29+
if balance.amount == initial_balance:
30+
time.sleep(5)
31+
balance = starkbank.corporatebalance.get()
32+
print("initial_balance:" + str(initial_balance))
33+
print("final_balance:" + str(balance.amount))
34+
print("diff:" + str(initial_balance - balance.amount))
35+
36+
1137
class TestCorporateWithdrawalQuery(TestCase):
1238

1339
def test_success(self):
@@ -50,7 +76,7 @@ def test_success(self):
5076
example_withdrawal = generateExampleWithdrawalJson()
5177
withdrawal = starkbank.corporatewithdrawal.create(
5278
withdrawal=CorporateWithdrawal(
53-
amount=example_withdrawal.amount,
79+
amount=100,
5480
external_id=example_withdrawal.external_id
5581
)
5682
)

0 commit comments

Comments
 (0)