Skip to content

Commit c9af4af

Browse files
Merge pull request #20 from LeandroDeJesus-S/feature/improve-models
Feature/improve models
2 parents 7150caa + 8a4afff commit c9af4af

23 files changed

Lines changed: 1056 additions & 193 deletions

File tree

README-pt.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Para usar o SDK, importe-o e inicialize o cliente com sua chave de API:
4545
```python
4646
import abacatepay
4747

48-
client = abacatepay.AbacatePay(api_key="<your-api-key>")
48+
client = abacatepay.AbacatePay("<your-api-key>")
4949
```
5050

5151
---
@@ -57,22 +57,28 @@ client = abacatepay.AbacatePay(api_key="<your-api-key>")
5757
```python
5858
from abacatepay.products import Product
5959

60-
client = abacatepay.AbacatePay(api_key="<your-api-key>")
61-
6260
products = [
6361
Product(
64-
externalId="123",
62+
external_id="123",
6563
name="Test Product",
6664
quantity=1,
6765
price=5000,
6866
description="Example product"
69-
)
67+
),
68+
# ou como um dicionário
69+
{
70+
'external_id': "321",
71+
'name': "Product as dict",
72+
'quantity': 1,
73+
'price': 10_00,
74+
'description': "Example using dict"
75+
}
7076
]
7177

7278
billing = client.billing.create(
7379
products=products,
74-
returnURL="https://yourwebsite.com/return",
75-
completionUrl="https://yourwebsite.com/complete"
80+
return_url="https://yourwebsite.com/return",
81+
completion_url="https://yourwebsite.com/complete"
7682
)
7783

7884
print(billing.data.url)
@@ -84,20 +90,23 @@ print(billing.data.url)
8490
billings = client.billing.list()
8591
for billing in billings:
8692
print(billing.id, billing.status)
93+
94+
print(len(billings))
8795
```
8896

8997
### Gerenciamento de clientes
9098

9199
```python
92-
from abacatepay.customers import Customer
100+
from abacatepay.customers import CustomerMetadata
93101

94-
customer = Customer(
102+
customer = CustomerMetadata( # Também pode ser apenas um dicionário
95103
email="customer@example.com",
96104
name="Customer Name",
97-
cellphone="+1234567890"
105+
cellphone="(12) 3456-7890",
106+
tax_id="123-456-789-10"
98107
)
99108

100-
created_customer = client.customers.create(customer=customer)
109+
created_customer = client.customers.create(customer)
101110
print(created_customer.id)
102111
```
103112

README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ To use the SDK, import it and initialize the client with your API key:
4646
```python
4747
import abacatepay
4848

49-
client = abacatepay.AbacatePay(api_key="<your-api-key>")
49+
client = abacatepay.AbacatePay("<your-api-key>")
5050
```
5151

5252
---
@@ -58,22 +58,29 @@ client = abacatepay.AbacatePay(api_key="<your-api-key>")
5858
```python
5959
from abacatepay.products import Product
6060

61-
client = abacatepay.AbacatePay(api_key="<your-api-key>")
6261

6362
products = [
6463
Product(
65-
externalId="123",
64+
external_id="123",
6665
name="Test Product",
6766
quantity=1,
68-
price=5000,
67+
price=50_00,
6968
description="Example product"
70-
)
69+
),
70+
# or as dict
71+
{
72+
'external_id': "321",
73+
'name': "Product as dict",
74+
'quantity': 1,
75+
'price': 10_00,
76+
'description': "Example using dict"
77+
}
7178
]
7279

7380
billing = client.billing.create(
7481
products=products,
75-
returnURL="https://yourwebsite.com/return",
76-
completionUrl="https://yourwebsite.com/complete"
82+
return_url="https://yourwebsite.com/return",
83+
completion_url="https://yourwebsite.com/complete"
7784
)
7885

7986
print(billing.data.url)
@@ -85,20 +92,23 @@ print(billing.data.url)
8592
billings = client.billing.list()
8693
for billing in billings:
8794
print(billing.id, billing.status)
95+
96+
print(len(billings))
8897
```
8998

9099
### Customer Management
91100

92101
```python
93-
from abacatepay.customers import Customer
102+
from abacatepay.customers import CustomerMetadata
94103

95-
customer = Customer(
104+
customer = CustomerMetadata( # Its can also be only a dict
96105
email="customer@example.com",
97106
name="Customer Name",
98-
cellphone="+1234567890"
107+
cellphone="(12) 3456-7890",
108+
tax_id="123-456-789-10"
99109
)
100110

101-
created_customer = client.customers.create(customer=customer)
111+
created_customer = client.customers.create(customer)
102112
print(created_customer.id)
103113
```
104114

abacatepay/billings/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
from .client import BillingClient
2-
from .models import BillingResponse
2+
from .models import (
3+
Billing,
4+
BillingList,
5+
BillingIn,
6+
BillingMetadata,
7+
)
38

4-
__all__ = ["BillingClient", "BillingResponse"]
9+
__all__ = [
10+
"BillingClient",
11+
"Billing",
12+
"BillingList",
13+
"BillingIn",
14+
"BillingMetadata",
15+
]

abacatepay/billings/client.py

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,51 @@
1-
from ..constants import (
2-
BASE_URL,
3-
BILLING_KINDS,
4-
BILLING_METHODS,
5-
)
6-
from ..products import Product
7-
from .models import (
8-
BillingResponse,
9-
Customer,
10-
)
11-
from ..base.client import BaseClient
121
from logging import getLogger
2+
from typing import Optional
3+
from ..base.client import BaseClient
4+
from ..constants import BASE_URL
5+
from .models import Billing, BillingIn, BillingList
6+
from ..utils.helpers import prepare_data
137

148
logger = getLogger(__name__)
159

1610

1711
class BillingClient(BaseClient):
18-
def create(
19-
self,
20-
products: list[Product],
21-
returnURL: str,
22-
completionUrl: str,
23-
methods: list[BILLING_METHODS] = ["PIX"],
24-
frequency: BILLING_KINDS = "ONE_TIME",
25-
customerId: str | None = None,
26-
customer: Customer | None = None
27-
) -> BillingResponse:
28-
29-
12+
def create(self, data: Optional[BillingIn | dict] = None, **kwargs) -> Billing:
3013
"""
3114
Create a new billing.
3215
3316
Args:
34-
products: List of products to be billed.
35-
returnURL: The URL the user will be redirected to after the billing is completed.
36-
completionUrl: The URL the API will make a POST request after the billing is completed.
37-
methods: The payment methods to be accepted. Defaults to ["PIX"].
38-
frequency: The frequency of the billing. Defaults to "ONE_TIME".
39-
customerId: The ID of the customer. If provided, the customer information won't be required.
40-
customer: The customer information. If customerId is provided, this parameter is ignored.
17+
data (BillingIn): an instance of `abacatepay.billings.models.BillingIn` a dict \
18+
or the named params following the model schema.
19+
20+
Keyword args:
21+
products (List[Product]): List of products to be billed.
22+
returnURL (str): The URL the user will be redirected to after the billing is completed.
23+
completionUrl (str): The URL the API will make a POST request after the billing is completed.
24+
methods (List[BILLING_METHODS]): The payment methods to be accepted. Defaults to ["PIX"].
25+
frequency (BILLING_KINDS): The frequency of the billing. Defaults to "ONE_TIME".
26+
customerId (str): The ID of the customer. If provided, the customer information won't be required.
27+
customer (CustomerMetadata): The customer information. If customerId is provided, this parameter is ignored.
4128
4229
Returns:
43-
BillingResponse: The response with the billing data.
30+
Billing: The response with the billing data.
4431
"""
32+
json_data = prepare_data(data or kwargs, BillingIn)
33+
logger.debug('creating billing: %s', json_data)
34+
4535
response = self._request(
4636
f"{BASE_URL}/billing/create",
4737
method="POST",
48-
json={
49-
"products": [product.model_dump() for product in products],
50-
"returnUrl": returnURL,
51-
"completionUrl": completionUrl,
52-
"methods": methods,
53-
"frequency": frequency,
54-
"customerId": customerId,
55-
**({"customer": customer.model_dump()} if customer is not None else {}),
56-
}
38+
json=json_data,
5739
)
40+
return Billing(**response.json()["data"])
5841

59-
billing_data = BillingResponse(data=response.json()["data"])
60-
return billing_data
61-
62-
def list(self) -> list[BillingResponse]:
42+
def list(self) -> BillingList:
6343
"""
6444
List all bills.
6545
6646
Returns:
67-
list[BillingResponse]: A list of billing responses.
47+
BillingList: A list of billing objects.
6848
"""
6949
logger.debug(f"Listing bills with URL: {BASE_URL}/billing/list")
7050
response = self._request(f"{BASE_URL}/billing/list", method="GET")
71-
return [BillingResponse(data=bill) for bill in response.json()["data"]]
51+
return BillingList.model_validate({'data': response.json()["data"]})

0 commit comments

Comments
 (0)