Skip to content

Commit

Permalink
Add Request methods
Browse files Browse the repository at this point in the history
  • Loading branch information
leoKagohara-Stark committed May 31, 2024
1 parent 4318460 commit b373194
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Given a version number MAJOR.MINOR.PATCH, increment:


## [Unreleased]
### Added
- request methods
### Changed
- core version

## [2.25.1] - 2024-04-01
### Fixed
Expand Down
160 changes: 160 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ is as easy as sending a text message to your client!
- [WebhookEvents](#process-webhook-events): Manage webhook events
- [WebhookEventAttempts](#query-failed-webhook-event-delivery-attempts-information): Query failed webhook event deliveries
- [Workspaces](#create-a-new-workspace): Manage your accounts
- [Request](#request): Send a custom request to Stark Bank. This can be used to access features that haven't been mapped yet.
- [Handling errors](#handling-errors)
- [Help and Feedback](#help-and-feedback)

Expand Down Expand Up @@ -2588,6 +2589,165 @@ print(workspace)

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

## request

This resource allows you to send HTTP requests to StarkBank routes.

## GET

You can perform a GET request to any StarkBank route.

It's possible to get a single resource using its id in the path.

```python
import starkbank

example_id = "5155165527080960"
request = starkbank.request.get(
path=f"/invoice/{example_id}"
)

print(request)
```

You can also get the specific resource log,

```python
import starkbank

example_id = "5699165527090460"
request = starkbank.request.get(
path=f"/invoice/log/{example_id}",
)

print(request)
```

This same method will be used to list all created items for the requested resource.

```python
import starkbank

request = starkbank.request.get(
path="/invoice",
query={"limit": 10, "status": "paid"},
)

for item in request["invoices"]:
print(item)
```

To list logs, you will use the same logic as for getting a single log.

```python
import starkbank

request = starkbank.request.get(
path="/invoice/log",
query={"limit": 10, "status": "paid"},
)

for item in request["invoices"]:
print(item)
```

You can get a resource file using this method.

```python
import starkbank

example_id = "5155165527080960"
pdf = starkbank.request.get(
path=f"/invoice/{example_id}/pdf",
)
with open("request.pdf", "wb") as file:
file.write(pdf)
```

## POST

You can perform a POST request to any StarkBank route.

This will create an object for each item sent in your request

**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.

```python
import starkbank

data={
"invoices": [
{
"amount": 100,
"name": "Iron Bank S.A.",
"taxId": "20.018.183/0001-80"
},
{
"amount": 450000,
"name": "Arya Stark.",
"taxId": "012.345.678-90"
}
]
}
request = starkbank.request.post(
path="/invoice",
body=data,
)
print(request)
```

## patch

You can perform a PATCH request to any StarkBank route.

It's possible to update a single item of a StarkBank resource.
```python
import starkbank

example_id = "5155165527080960"
request = starkbank.request.patch(
path=f"/invoice/{example_id}",
body={"amount": 0},
)
print(request)
```

## PUT

You can perform a PUT request to any StarkBank route.

It's possible to put a single item of a StarkBank resource.
```python
import starkbank

data = {
"profiles": [
{
"interval": "day",
"delay": 0
}
]
}
request = starkbank.request.put(
path="/split-profile",
body=data,
)
print(request)
```
## DELETE

You can perform a DELETE request to any StarkBank route.

It's possible to delete a single item of a StarkBank resource.
```python
import starkbank

example_id = "5155165527080960"
request = starkbank.request.delete(
path=f"/transfer/{example_id}",
)
print(request)
```
# Handling errors

The SDK may raise one of four types of errors: __InputErrors__, __InternalServerError__, __UnknownError__, __InvalidSignatureError__
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
starkcore==0.2.1
starkcore==0.3.2
3 changes: 3 additions & 0 deletions starkbank/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@

from . import splitprofile
from .splitprofile.__splitprofile import SplitProfile

from . import request

1 change: 1 addition & 0 deletions starkbank/request/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .__request import (get, post, patch, put, delete)
100 changes: 100 additions & 0 deletions starkbank/request/__request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from ..utils import rest


def get(path, query=None, user=None):
"""# Retrieve any StarkBank resource
Receive a json of resources previously created in StarkBank's 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:
- dict of StarkBank objects with updated attributes
"""
return rest.get_raw(
path=path,
query=query,
user=user
)


def post(path, body=None, query=None, user=None):
"""# Create any StarkBank resource
Send a list of jsons and create any StarkBank resource objects
## Parameters (required):
- path [string]: StarkBank resource's route. ex: "/invoice/"
- body [dict]: request parameters. ex: {"invoices": [{"amount": 100, "name": "Iron Bank S.A.", "taxId": "20.018.183/0001-80"}]}
## Parameters (optional):
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user
was set before function call
- query [dict, default None]: Query parameters. ex: {"limit": 1, "status": paid}
## Return:
- list of resources jsons with updated attributes
"""
return rest.post_raw(
path=path,
payload=body,
query=query,
user=user
)


def patch(path, body=None, user=None):
"""# Update any StarkBank resource
Send a json with parameters of a single StarkBank resource object and update it
## Parameters (required):
- path [string]: StarkBank resource's route. ex: "/invoice/5699165527090460"
- body [dict]: request parameters. ex: {"amount": 100}
## Parameters (optional):
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user
was set before function call
## Return:
- json of the resource with updated attributes
"""
return rest.patch_raw(
path=path,
payload=body,
user=user
)


def put(path, body=None, user=None):
"""# Put any StarkBank resource
Send a json with parameters of a single StarkBank resource object and create it, if the resource alredy exists,
you will update it.
## Parameters (required):
- path [string]: StarkBank resource's route. ex: "/invoice"
- body [dict]: request parameters. ex: {"amount": 100}
## Parameters (optional):
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user
was set before function call
## Return:
- json of the resource with updated attributes
"""
return rest.put_raw(
path=path,
payload=body,
user=user
)


def delete(path, body=None, user=None):
"""# Delete any StarkBank resource
Send a json with parameters of a single StarkBank resource object and delete it
you will update it.
## Parameters (required):
- path [string]: StarkBank resource's route. ex: "/invoice/5699165527090460"
- body [dict]: request parameters. ex: {"amount": 100}
## Parameters (optional):
- user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkbank.user
was set before function call
## Return:
- json of the resource with updated attributes
"""
return rest.delete_raw(
path=path,
payload=body,
user=user
)
3 changes: 3 additions & 0 deletions starkbank/utils/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
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)
1 change: 1 addition & 0 deletions tests/sdk/test_corporate_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TestCorporateInvoiceQuery(TestCase):

def test_success(self):
invoices = starkbank.corporateinvoice.query(
limit=1,
after=date.today() - timedelta(days=100),
before=date.today(),
)
Expand Down
39 changes: 38 additions & 1 deletion tests/sdk/test_corporate_withdrawal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,47 @@
from starkbank import CorporateWithdrawal
from tests.utils.user import exampleProject
from tests.utils.withdrawal import generateExampleWithdrawalJson
import time

starkbank.user = exampleProject


class TestCorporateResourcesChecks(TestCase):

def test_success_corporate_balance(self):
def payment_script(b):
data = starkbank.CorporateInvoice(amount=b * -1)
corporate_invoice = starkbank.corporateinvoice.create(data)
print(corporate_invoice)
payload = [starkbank.BrcodePayment(brcode=corporate_invoice.brcode, tax_id="20.018.183/0001-80",
description="paying debts")]
payments = starkbank.brcodepayment.create(payments=payload)
return payments

balance = starkbank.corporatebalance.get()
initial_balance = balance.amount

if balance.amount < 0:
request = payment_script(balance.amount)
i = 0
while i <= 5:
br_code = starkbank.brcodepayment.get(request[0].id)
if br_code.status == "success" or br_code.status == "failed":
time.sleep(5) # Aguardar log de credited
break
time.sleep(10)
i += 1
balance = starkbank.corporatebalance.get()
if balance.amount == initial_balance:
time.sleep(5)
balance = starkbank.corporatebalance.get()
print("initial_balance:" + str(initial_balance))
print("final_balance:" + str(balance.amount))
print("diff:" + str(initial_balance-balance.amount))

def test_success_corporate_billing_invoice(self):
pass

class TestCorporateWithdrawalQuery(TestCase):

def test_success(self):
Expand Down Expand Up @@ -50,7 +87,7 @@ def test_success(self):
example_withdrawal = generateExampleWithdrawalJson()
withdrawal = starkbank.corporatewithdrawal.create(
withdrawal=CorporateWithdrawal(
amount=example_withdrawal.amount,
amount=100,
external_id=example_withdrawal.external_id
)
)
Expand Down
Loading

0 comments on commit b373194

Please sign in to comment.