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 21, 2024
1 parent 4318460 commit 6f4543c
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 1 deletion.
158 changes: 158 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,163 @@ 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.

## GET

You can perform a GET request to any StarkBank.

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 of the get single log.

```python
import starkbank

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

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

If you need to get a file related to the resource you will also use 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.

It's possible to create a list of items of the same resource.

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

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.

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.

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
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)
52 changes: 52 additions & 0 deletions starkbank/request/__request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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
)
5 changes: 4 additions & 1 deletion starkbank/utils/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Loading

0 comments on commit 6f4543c

Please sign in to comment.