branddrive-python-sdk 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- branddrive/__init__.py +33 -0
- branddrive/client.py +134 -0
- branddrive/config.py +15 -0
- branddrive/dataclasses/__init__.py +118 -0
- branddrive/dataclasses/_utils.py +8 -0
- branddrive/dataclasses/customers.py +66 -0
- branddrive/dataclasses/expenses.py +155 -0
- branddrive/dataclasses/invoices.py +131 -0
- branddrive/dataclasses/payment_links.py +134 -0
- branddrive/dataclasses/pos.py +141 -0
- branddrive/dataclasses/products.py +166 -0
- branddrive/dataclasses/purchase_orders.py +141 -0
- branddrive/dataclasses/services.py +76 -0
- branddrive/dataclasses/vendors.py +48 -0
- branddrive/dataclasses/wallets.py +72 -0
- branddrive/exceptions.py +35 -0
- branddrive/protocols/__init__.py +43 -0
- branddrive/protocols/analytics.py +61 -0
- branddrive/protocols/customers.py +42 -0
- branddrive/protocols/expenses.py +69 -0
- branddrive/protocols/invoices.py +81 -0
- branddrive/protocols/payment_links.py +49 -0
- branddrive/protocols/pos.py +88 -0
- branddrive/protocols/products.py +150 -0
- branddrive/protocols/purchase_orders.py +68 -0
- branddrive/protocols/services.py +56 -0
- branddrive/protocols/vendors.py +39 -0
- branddrive/protocols/wallets.py +52 -0
- branddrive/resources/__init__.py +125 -0
- branddrive/resources/_utils.py +10 -0
- branddrive/resources/analytics.py +198 -0
- branddrive/resources/customers.py +194 -0
- branddrive/resources/expenses.py +209 -0
- branddrive/resources/invoices.py +268 -0
- branddrive/resources/payment_links.py +265 -0
- branddrive/resources/pos.py +353 -0
- branddrive/resources/products.py +612 -0
- branddrive/resources/purchase_orders.py +235 -0
- branddrive/resources/services.py +184 -0
- branddrive/resources/vendors.py +152 -0
- branddrive/resources/wallets.py +204 -0
- branddrive_python_sdk-0.1.0.dist-info/METADATA +132 -0
- branddrive_python_sdk-0.1.0.dist-info/RECORD +45 -0
- branddrive_python_sdk-0.1.0.dist-info/WHEEL +5 -0
- branddrive_python_sdk-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any, Optional
|
|
5
|
+
from urllib.parse import quote
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
|
|
9
|
+
from ..exceptions import handle_api_exception
|
|
10
|
+
from ..dataclasses.customers import CustomerData, UpdateCustomerData
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Customer:
|
|
14
|
+
def __init__(self, client: httpx.Client) -> None:
|
|
15
|
+
self.client = client
|
|
16
|
+
|
|
17
|
+
@staticmethod
|
|
18
|
+
def _validate_id(name: str, value: int) -> None:
|
|
19
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
20
|
+
raise TypeError(f"{name} must be an integer")
|
|
21
|
+
if value < 1:
|
|
22
|
+
raise ValueError(f"{name} must be greater than 0")
|
|
23
|
+
|
|
24
|
+
@staticmethod
|
|
25
|
+
def _path_id(name: str, value: int) -> str:
|
|
26
|
+
Customer._validate_id(name, value)
|
|
27
|
+
return quote(str(value), safe="")
|
|
28
|
+
|
|
29
|
+
def _handle_response(self, response: httpx.Response) -> Any:
|
|
30
|
+
if not response.is_success:
|
|
31
|
+
raise handle_api_exception(response)
|
|
32
|
+
return response.json()
|
|
33
|
+
|
|
34
|
+
def _handle_binary_response(self, response: httpx.Response) -> bytes:
|
|
35
|
+
if not response.is_success:
|
|
36
|
+
raise handle_api_exception(response)
|
|
37
|
+
return response.content
|
|
38
|
+
|
|
39
|
+
def create_customer(
|
|
40
|
+
self,
|
|
41
|
+
customer_data: CustomerData,
|
|
42
|
+
) -> dict[str, Any]:
|
|
43
|
+
"""Create a customer."""
|
|
44
|
+
response = self.client.request(
|
|
45
|
+
"POST",
|
|
46
|
+
"/api/v1/customer",
|
|
47
|
+
json=customer_data.to_dict(),
|
|
48
|
+
)
|
|
49
|
+
return self._handle_response(response)
|
|
50
|
+
|
|
51
|
+
def import_csv(
|
|
52
|
+
self,
|
|
53
|
+
file_path: str | Path,
|
|
54
|
+
file_field: str = "file",
|
|
55
|
+
) -> dict[str, Any]:
|
|
56
|
+
"""Upload a CSV file to import customer records."""
|
|
57
|
+
if not isinstance(file_field, str) or not file_field.strip():
|
|
58
|
+
raise ValueError("file_field must be a non-empty string")
|
|
59
|
+
|
|
60
|
+
path = Path(file_path)
|
|
61
|
+
if not path.exists():
|
|
62
|
+
raise FileNotFoundError(f"{file_path} not found.")
|
|
63
|
+
if not path.is_file():
|
|
64
|
+
raise ValueError("Supported path must be a file.")
|
|
65
|
+
if path.suffix.lower() != ".csv":
|
|
66
|
+
raise ValueError("Only CSV file are supported.")
|
|
67
|
+
|
|
68
|
+
with path.open("rb") as csv_file:
|
|
69
|
+
response = self.client.request(
|
|
70
|
+
"POST",
|
|
71
|
+
"/api/v1/customer/import",
|
|
72
|
+
files={
|
|
73
|
+
file_field: (
|
|
74
|
+
path.name,
|
|
75
|
+
csv_file,
|
|
76
|
+
"text/csv",
|
|
77
|
+
)
|
|
78
|
+
},
|
|
79
|
+
)
|
|
80
|
+
return self._handle_response(response)
|
|
81
|
+
|
|
82
|
+
def export(
|
|
83
|
+
self,
|
|
84
|
+
email: Optional[str] = None,
|
|
85
|
+
file_type: Optional[str] = None,
|
|
86
|
+
) -> bytes:
|
|
87
|
+
"""Download customer records in the requested file format."""
|
|
88
|
+
params: dict[str, str] = {}
|
|
89
|
+
if email is not None:
|
|
90
|
+
params["email"] = email
|
|
91
|
+
if file_type is not None:
|
|
92
|
+
params["type"] = file_type
|
|
93
|
+
|
|
94
|
+
response = self.client.request(
|
|
95
|
+
"GET",
|
|
96
|
+
"/api/v1/customer/download",
|
|
97
|
+
params=params,
|
|
98
|
+
)
|
|
99
|
+
return self._handle_binary_response(response)
|
|
100
|
+
|
|
101
|
+
def retrieve_customer(self, customer_id: int) -> dict[str, Any]:
|
|
102
|
+
"""Retrieve one customer record."""
|
|
103
|
+
customer_path_id = self._path_id("customer_id", customer_id)
|
|
104
|
+
response = self.client.request(
|
|
105
|
+
"GET",
|
|
106
|
+
f"/api/v1/customer/{customer_path_id}",
|
|
107
|
+
)
|
|
108
|
+
return self._handle_response(response)
|
|
109
|
+
|
|
110
|
+
def update_customer(
|
|
111
|
+
self,
|
|
112
|
+
customer_id: int,
|
|
113
|
+
customer_data: UpdateCustomerData,
|
|
114
|
+
) -> dict[str, Any]:
|
|
115
|
+
"""Update a customer's record."""
|
|
116
|
+
customer_path_id = self._path_id("customer_id", customer_id)
|
|
117
|
+
response = self.client.request(
|
|
118
|
+
"PATCH",
|
|
119
|
+
f"/api/v1/customer/{customer_path_id}",
|
|
120
|
+
json=customer_data.to_dict(),
|
|
121
|
+
)
|
|
122
|
+
return self._handle_response(response)
|
|
123
|
+
|
|
124
|
+
def toggle_status(self, customer_id: int) -> dict[str, Any]:
|
|
125
|
+
"""Toggle a customer's active status."""
|
|
126
|
+
self._validate_id("customer_id", customer_id)
|
|
127
|
+
response = self.client.request(
|
|
128
|
+
"POST",
|
|
129
|
+
"/api/v1/customer/toggle-status",
|
|
130
|
+
json={"customerId": int(customer_id)},
|
|
131
|
+
)
|
|
132
|
+
return self._handle_response(response)
|
|
133
|
+
|
|
134
|
+
def delete_customer(self, customer_id: int) -> dict[str, Any]:
|
|
135
|
+
"""Delete a customer."""
|
|
136
|
+
customer_path_id = self._path_id("customer_id", customer_id)
|
|
137
|
+
response = self.client.request(
|
|
138
|
+
"DELETE",
|
|
139
|
+
f"/api/v1/customer/{customer_path_id}",
|
|
140
|
+
)
|
|
141
|
+
return self._handle_response(response)
|
|
142
|
+
|
|
143
|
+
def _list_customers(
|
|
144
|
+
self,
|
|
145
|
+
search: Optional[str] = None,
|
|
146
|
+
sort: Optional[str] = None,
|
|
147
|
+
limit: int = 10,
|
|
148
|
+
offset: int = 0,
|
|
149
|
+
) -> dict[str, Any]:
|
|
150
|
+
|
|
151
|
+
for name, value in (("limit", limit), ("offset", offset)):
|
|
152
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
153
|
+
raise TypeError(f"{name} must be an integer")
|
|
154
|
+
if limit < 1:
|
|
155
|
+
raise ValueError("limit must be greater than 0")
|
|
156
|
+
if offset < 0:
|
|
157
|
+
raise ValueError("offset must be greater than or equal to 0")
|
|
158
|
+
if search is not None and not isinstance(search, str):
|
|
159
|
+
raise TypeError("search must be a string")
|
|
160
|
+
if sort is not None and not isinstance(sort, str):
|
|
161
|
+
raise TypeError("sort must be a string")
|
|
162
|
+
if sort is not None and ":" not in sort:
|
|
163
|
+
raise ValueError("sort must use the format field_name:asc or field_name:desc")
|
|
164
|
+
|
|
165
|
+
params: dict[str, int | str] = {
|
|
166
|
+
"limit": limit,
|
|
167
|
+
"offset": offset,
|
|
168
|
+
}
|
|
169
|
+
if search:
|
|
170
|
+
params["search"] = search
|
|
171
|
+
if sort:
|
|
172
|
+
params["sort"] = sort
|
|
173
|
+
|
|
174
|
+
response = self.client.request(
|
|
175
|
+
"GET",
|
|
176
|
+
"/api/v1/customer",
|
|
177
|
+
params=params,
|
|
178
|
+
)
|
|
179
|
+
return self._handle_response(response)
|
|
180
|
+
|
|
181
|
+
def list_cusotmers(
|
|
182
|
+
self,
|
|
183
|
+
search: Optional[str] = None,
|
|
184
|
+
sort: Optional[str] = None,
|
|
185
|
+
limit: int = 10,
|
|
186
|
+
offset: int = 0,
|
|
187
|
+
) -> dict[str, Any]:
|
|
188
|
+
"""List customers with optional search, sorting, and pagination."""
|
|
189
|
+
return self._list_customers(
|
|
190
|
+
search=search,
|
|
191
|
+
sort=sort,
|
|
192
|
+
limit=limit,
|
|
193
|
+
offset=offset,
|
|
194
|
+
)
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
from urllib.parse import quote
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
|
|
8
|
+
from ..exceptions import handle_api_exception
|
|
9
|
+
from ..dataclasses.expenses import (
|
|
10
|
+
ExpenseCreate,
|
|
11
|
+
ExpensePaymentCreate,
|
|
12
|
+
ExpenseProcurementCreate,
|
|
13
|
+
ExpenseProcurementUpdate,
|
|
14
|
+
ExpenseUpdate,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class _ExpenseResource:
|
|
19
|
+
def __init__(self, client: httpx.Client) -> None:
|
|
20
|
+
self.client = client
|
|
21
|
+
|
|
22
|
+
@staticmethod
|
|
23
|
+
def _validate_id(name: str, value: int) -> str:
|
|
24
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
25
|
+
raise TypeError(f"{name} must be an integer")
|
|
26
|
+
if value < 1:
|
|
27
|
+
raise ValueError(f"{name} must be greater than 0")
|
|
28
|
+
return quote(str(value), safe="")
|
|
29
|
+
|
|
30
|
+
def _handle_response(self, response: httpx.Response) -> dict[str, Any]:
|
|
31
|
+
if not response.is_success:
|
|
32
|
+
raise handle_api_exception(response)
|
|
33
|
+
return response.json()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Expense(_ExpenseResource):
|
|
37
|
+
def __init__(self, client: httpx.Client) -> None:
|
|
38
|
+
super().__init__(client)
|
|
39
|
+
self.procurements = ExpenseProcurement(client)
|
|
40
|
+
self.payments = ExpensePayment(client)
|
|
41
|
+
|
|
42
|
+
def create_expense(self, expense_data: ExpenseCreate) -> dict[str, Any]:
|
|
43
|
+
"""Create an expense record."""
|
|
44
|
+
for name, value in (
|
|
45
|
+
("branch_id", expense_data.branch_id),
|
|
46
|
+
("vendor_id", expense_data.vendor_id),
|
|
47
|
+
):
|
|
48
|
+
self._validate_id(name, value)
|
|
49
|
+
response = self.client.request(
|
|
50
|
+
"POST",
|
|
51
|
+
"/api/v1/expense",
|
|
52
|
+
json=expense_data.to_dict(),
|
|
53
|
+
)
|
|
54
|
+
return self._handle_response(response)
|
|
55
|
+
|
|
56
|
+
def list_expenses(
|
|
57
|
+
self,
|
|
58
|
+
limit: int = 10,
|
|
59
|
+
offset: int = 0,
|
|
60
|
+
) -> dict[str, Any]:
|
|
61
|
+
"""List expenses with pagination."""
|
|
62
|
+
for name, value in (("limit", limit), ("offset", offset)):
|
|
63
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
64
|
+
raise TypeError(f"{name} must be an integer")
|
|
65
|
+
if limit < 1:
|
|
66
|
+
raise ValueError("limit must be greater than 0")
|
|
67
|
+
if offset < 0:
|
|
68
|
+
raise ValueError("offset must be greater than or equal to 0")
|
|
69
|
+
|
|
70
|
+
response = self.client.request(
|
|
71
|
+
"GET",
|
|
72
|
+
"/api/v1/expense",
|
|
73
|
+
params={"limit": limit, "offset": offset},
|
|
74
|
+
)
|
|
75
|
+
return self._handle_response(response)
|
|
76
|
+
|
|
77
|
+
def retrieve_expense(self, expense_id: int) -> dict[str, Any]:
|
|
78
|
+
"""Retrieve an expense by ID."""
|
|
79
|
+
expense_path_id = self._validate_id("expense_id", expense_id)
|
|
80
|
+
response = self.client.request(
|
|
81
|
+
"GET",
|
|
82
|
+
f"/api/v1/expense/{expense_path_id}",
|
|
83
|
+
)
|
|
84
|
+
return self._handle_response(response)
|
|
85
|
+
|
|
86
|
+
def get_expense_categories(self) -> dict[str, Any]:
|
|
87
|
+
"""Get the expense categories available to the business."""
|
|
88
|
+
response = self.client.request(
|
|
89
|
+
"GET",
|
|
90
|
+
"/api/v1/expense/categories",
|
|
91
|
+
)
|
|
92
|
+
return self._handle_response(response)
|
|
93
|
+
|
|
94
|
+
def update_expense(
|
|
95
|
+
self,
|
|
96
|
+
expense_id: int,
|
|
97
|
+
expense_data: ExpenseUpdate,
|
|
98
|
+
) -> dict[str, Any]:
|
|
99
|
+
"""Update an expense record."""
|
|
100
|
+
expense_path_id = self._validate_id("expense_id", expense_id)
|
|
101
|
+
self._validate_id("branch_id", expense_data.branch_id)
|
|
102
|
+
response = self.client.request(
|
|
103
|
+
"PATCH",
|
|
104
|
+
f"/api/v1/expense/{expense_path_id}",
|
|
105
|
+
json=expense_data.to_dict(),
|
|
106
|
+
)
|
|
107
|
+
return self._handle_response(response)
|
|
108
|
+
|
|
109
|
+
def delete_expense(self, expense_id: int) -> dict[str, Any]:
|
|
110
|
+
"""Delete an expense record."""
|
|
111
|
+
expense_path_id = self._validate_id("expense_id", expense_id)
|
|
112
|
+
response = self.client.request(
|
|
113
|
+
"DELETE",
|
|
114
|
+
f"/api/v1/expense/{expense_path_id}",
|
|
115
|
+
)
|
|
116
|
+
return self._handle_response(response)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class ExpenseProcurement(_ExpenseResource):
|
|
120
|
+
def add_procurement(
|
|
121
|
+
self,
|
|
122
|
+
procurement_data: ExpenseProcurementCreate,
|
|
123
|
+
) -> dict[str, Any]:
|
|
124
|
+
"""Add a procurement line to an expense."""
|
|
125
|
+
self._validate_id("expense_id", procurement_data.expense_id)
|
|
126
|
+
self._validate_id("category_id", procurement_data.category_id)
|
|
127
|
+
response = self.client.request(
|
|
128
|
+
"POST",
|
|
129
|
+
"/api/v1/expense/procurement",
|
|
130
|
+
json=procurement_data.to_dict(),
|
|
131
|
+
)
|
|
132
|
+
return self._handle_response(response)
|
|
133
|
+
|
|
134
|
+
def update_procurement(
|
|
135
|
+
self,
|
|
136
|
+
expense_id: int,
|
|
137
|
+
procurement_id: int,
|
|
138
|
+
procurement_data: ExpenseProcurementUpdate,
|
|
139
|
+
) -> dict[str, Any]:
|
|
140
|
+
"""Update a procurement line on an expense."""
|
|
141
|
+
expense_path_id = self._validate_id("expense_id", expense_id)
|
|
142
|
+
procurement_path_id = self._validate_id(
|
|
143
|
+
"procurement_id",
|
|
144
|
+
procurement_id,
|
|
145
|
+
)
|
|
146
|
+
if not procurement_data.to_dict():
|
|
147
|
+
raise ValueError("At least one procurement field must be provided")
|
|
148
|
+
if procurement_data.category_id is not None:
|
|
149
|
+
self._validate_id("category_id", procurement_data.category_id)
|
|
150
|
+
|
|
151
|
+
response = self.client.request(
|
|
152
|
+
"PATCH",
|
|
153
|
+
f"/api/v1/expense/{expense_path_id}/procurement/{procurement_path_id}",
|
|
154
|
+
json=procurement_data.to_dict(),
|
|
155
|
+
)
|
|
156
|
+
return self._handle_response(response)
|
|
157
|
+
|
|
158
|
+
def delete_procurement(
|
|
159
|
+
self,
|
|
160
|
+
expense_id: int,
|
|
161
|
+
procurement_id: int,
|
|
162
|
+
) -> dict[str, Any]:
|
|
163
|
+
"""Delete a procurement line from an expense."""
|
|
164
|
+
expense_path_id = self._validate_id("expense_id", expense_id)
|
|
165
|
+
procurement_path_id = self._validate_id(
|
|
166
|
+
"procurement_id",
|
|
167
|
+
procurement_id,
|
|
168
|
+
)
|
|
169
|
+
response = self.client.request(
|
|
170
|
+
"DELETE",
|
|
171
|
+
f"/api/v1/expense/{expense_path_id}/procurement/{procurement_path_id}",
|
|
172
|
+
)
|
|
173
|
+
return self._handle_response(response)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class ExpensePayment(_ExpenseResource):
|
|
177
|
+
def record_payment(
|
|
178
|
+
self,
|
|
179
|
+
payment_data: ExpensePaymentCreate,
|
|
180
|
+
) -> dict[str, Any]:
|
|
181
|
+
"""Record a payment against an expense."""
|
|
182
|
+
self._validate_id("expense_id", payment_data.expense_id)
|
|
183
|
+
response = self.client.request(
|
|
184
|
+
"POST",
|
|
185
|
+
"/api/v1/expense/recordPayment",
|
|
186
|
+
json=payment_data.to_dict(),
|
|
187
|
+
)
|
|
188
|
+
return self._handle_response(response)
|
|
189
|
+
|
|
190
|
+
def add_payment(
|
|
191
|
+
self,
|
|
192
|
+
payment_data: ExpensePaymentCreate,
|
|
193
|
+
) -> dict[str, Any]:
|
|
194
|
+
"""Alias for record_payment."""
|
|
195
|
+
return self.record_payment(payment_data)
|
|
196
|
+
|
|
197
|
+
def remove_payment(
|
|
198
|
+
self,
|
|
199
|
+
expense_id: int,
|
|
200
|
+
payment_id: int,
|
|
201
|
+
) -> dict[str, Any]:
|
|
202
|
+
"""Remove a payment from an expense."""
|
|
203
|
+
expense_path_id = self._validate_id("expense_id", expense_id)
|
|
204
|
+
payment_path_id = self._validate_id("payment_id", payment_id)
|
|
205
|
+
response = self.client.request(
|
|
206
|
+
"DELETE",
|
|
207
|
+
f"/api/v1/expense/{expense_path_id}/payment/{payment_path_id}",
|
|
208
|
+
)
|
|
209
|
+
return self._handle_response(response)
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from datetime import date
|
|
4
|
+
from typing import Any
|
|
5
|
+
from urllib.parse import quote
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
|
|
9
|
+
from ..exceptions import handle_api_exception
|
|
10
|
+
from ..dataclasses.invoices import (
|
|
11
|
+
InvoiceCreate,
|
|
12
|
+
InvoiceCreateSend,
|
|
13
|
+
InvoiceUpdate,
|
|
14
|
+
RecurringScheduleCreate,
|
|
15
|
+
RecurringScheduleUpdate,
|
|
16
|
+
)
|
|
17
|
+
from ..dataclasses.products import ProcurementData, UpdateProcurementData
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Invoice:
|
|
21
|
+
def __init__(self, client: httpx.Client) -> None:
|
|
22
|
+
self.client = client
|
|
23
|
+
|
|
24
|
+
@staticmethod
|
|
25
|
+
def _validate_id(name: str, value: int) -> None:
|
|
26
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
27
|
+
raise TypeError(f"{name} must be an integer")
|
|
28
|
+
if value < 1:
|
|
29
|
+
raise ValueError(f"{name} must be greater than 0")
|
|
30
|
+
|
|
31
|
+
@staticmethod
|
|
32
|
+
def _validate_date(name: str, value: date) -> None:
|
|
33
|
+
""" Ensure that every date obj is in the future"""
|
|
34
|
+
if value < date.today():
|
|
35
|
+
raise ValueError(f"{name} cannot be less than now")
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def _path_id(name: str, value: int) -> str:
|
|
39
|
+
Invoice._validate_id(name, value)
|
|
40
|
+
return quote(str(value), safe="")
|
|
41
|
+
|
|
42
|
+
def _handle_response(self, response: httpx.Response) -> Any:
|
|
43
|
+
if not response.is_success:
|
|
44
|
+
raise handle_api_exception(response)
|
|
45
|
+
return response.json()
|
|
46
|
+
|
|
47
|
+
def _handle_binary_response(self, response: httpx.Response) -> bytes:
|
|
48
|
+
if not response.is_success:
|
|
49
|
+
raise handle_api_exception(response)
|
|
50
|
+
return response.content
|
|
51
|
+
|
|
52
|
+
def create_invoice(self, invoice_data: InvoiceCreate) -> dict[str, Any]:
|
|
53
|
+
"""Create a new invoice."""
|
|
54
|
+
response = self.client.request(
|
|
55
|
+
"POST",
|
|
56
|
+
"/api/v1/invoice",
|
|
57
|
+
json=invoice_data.to_dict(),
|
|
58
|
+
)
|
|
59
|
+
return self._handle_response(response)
|
|
60
|
+
|
|
61
|
+
def create_send_invoice(
|
|
62
|
+
self,
|
|
63
|
+
invoice_data: InvoiceCreateSend,
|
|
64
|
+
) -> dict[str, Any]:
|
|
65
|
+
"""Create an invoice and optionally send it to the customer's email."""
|
|
66
|
+
if invoice_data.send and invoice_data.customer_id is None:
|
|
67
|
+
raise ValueError("customer_id is required when send is True")
|
|
68
|
+
|
|
69
|
+
response = self.client.request(
|
|
70
|
+
"POST",
|
|
71
|
+
"/api/v1/invoice",
|
|
72
|
+
json=invoice_data.to_dict(),
|
|
73
|
+
)
|
|
74
|
+
return self._handle_response(response)
|
|
75
|
+
|
|
76
|
+
def send_invoice(self, invoice_id: int) -> dict[str, Any]:
|
|
77
|
+
"""Send an invoice PDF to the associated customer."""
|
|
78
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
79
|
+
response = self.client.request(
|
|
80
|
+
"POST",
|
|
81
|
+
f"/api/v1/invoice/pdf/{invoice_path_id}",
|
|
82
|
+
)
|
|
83
|
+
return self._handle_response(response)
|
|
84
|
+
|
|
85
|
+
def list_invoices(
|
|
86
|
+
self,
|
|
87
|
+
currency_id: int = 0,
|
|
88
|
+
limit: int = 10,
|
|
89
|
+
offset: int = 0,
|
|
90
|
+
) -> dict[str, Any]:
|
|
91
|
+
"""List invoices with optional currency filtering and pagination."""
|
|
92
|
+
for name, value in (
|
|
93
|
+
("currency_id", currency_id),
|
|
94
|
+
("limit", limit),
|
|
95
|
+
("offset", offset),
|
|
96
|
+
):
|
|
97
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
98
|
+
raise TypeError(f"{name} must be an integer")
|
|
99
|
+
|
|
100
|
+
if currency_id < 0:
|
|
101
|
+
raise ValueError("currency_id must be greater than or equal to 0")
|
|
102
|
+
if limit < 1:
|
|
103
|
+
raise ValueError("limit must be greater than 0")
|
|
104
|
+
if offset < 0:
|
|
105
|
+
raise ValueError("offset must be greater than or equal to 0")
|
|
106
|
+
|
|
107
|
+
response = self.client.request(
|
|
108
|
+
"GET",
|
|
109
|
+
"/api/v1/invoice",
|
|
110
|
+
params={
|
|
111
|
+
"currencyId": currency_id,
|
|
112
|
+
"limit": limit,
|
|
113
|
+
"offset": offset,
|
|
114
|
+
},
|
|
115
|
+
)
|
|
116
|
+
return self._handle_response(response)
|
|
117
|
+
|
|
118
|
+
def retrieve_invoice(self, invoice_id: int) -> dict[str, Any]:
|
|
119
|
+
"""Retrieve a single invoice."""
|
|
120
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
121
|
+
response = self.client.request(
|
|
122
|
+
"GET",
|
|
123
|
+
f"/api/v1/invoice/{invoice_path_id}",
|
|
124
|
+
)
|
|
125
|
+
return self._handle_response(response)
|
|
126
|
+
|
|
127
|
+
def preview_invoice(self, invoice_id: int) -> bytes:
|
|
128
|
+
"""Download the PDF representation of an invoice."""
|
|
129
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
130
|
+
response = self.client.request(
|
|
131
|
+
"GET",
|
|
132
|
+
f"/api/v1/invoice/{invoice_path_id}/pdf",
|
|
133
|
+
)
|
|
134
|
+
return self._handle_binary_response(response)
|
|
135
|
+
|
|
136
|
+
def update_invoice(
|
|
137
|
+
self,
|
|
138
|
+
invoice_id: int,
|
|
139
|
+
invoice_data: InvoiceUpdate,
|
|
140
|
+
) -> dict[str, Any]:
|
|
141
|
+
"""Update an invoice."""
|
|
142
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
143
|
+
response = self.client.request(
|
|
144
|
+
"PATCH",
|
|
145
|
+
f"/api/v1/invoice/{invoice_path_id}",
|
|
146
|
+
json=invoice_data.to_dict(),
|
|
147
|
+
)
|
|
148
|
+
return self._handle_response(response)
|
|
149
|
+
|
|
150
|
+
def update_send_invoice(
|
|
151
|
+
self,
|
|
152
|
+
invoice_id: int,
|
|
153
|
+
invoice_data: InvoiceUpdate,
|
|
154
|
+
) -> dict[str, Any]:
|
|
155
|
+
"""Update an invoice and send the PDF to the customer."""
|
|
156
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
157
|
+
payload = invoice_data.to_dict()
|
|
158
|
+
payload["send"] = True
|
|
159
|
+
response = self.client.request(
|
|
160
|
+
"PATCH",
|
|
161
|
+
f"/api/v1/invoice/{invoice_path_id}",
|
|
162
|
+
json=payload,
|
|
163
|
+
)
|
|
164
|
+
return self._handle_response(response)
|
|
165
|
+
|
|
166
|
+
def list_procurements(self, invoice_id: int) -> dict[str, Any]:
|
|
167
|
+
"""Get the product procurements associated with an invoice."""
|
|
168
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
169
|
+
response = self.client.request(
|
|
170
|
+
"GET",
|
|
171
|
+
f"/api/v1/invoice/{invoice_path_id}/procurement",
|
|
172
|
+
)
|
|
173
|
+
return self._handle_response(response)
|
|
174
|
+
|
|
175
|
+
def add_procurement(
|
|
176
|
+
self,
|
|
177
|
+
invoice_id: int,
|
|
178
|
+
procurement: ProcurementData,
|
|
179
|
+
) -> dict[str, Any]:
|
|
180
|
+
"""Add a product procurement to an invoice."""
|
|
181
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
182
|
+
response = self.client.request(
|
|
183
|
+
"POST",
|
|
184
|
+
f"/api/v1/invoice/{invoice_path_id}/procurement",
|
|
185
|
+
json=procurement.to_dict(),
|
|
186
|
+
)
|
|
187
|
+
return self._handle_response(response)
|
|
188
|
+
|
|
189
|
+
def update_procurement(
|
|
190
|
+
self,
|
|
191
|
+
invoice_id: int,
|
|
192
|
+
procurement_id: int,
|
|
193
|
+
procurement_data: UpdateProcurementData,
|
|
194
|
+
) -> dict[str, Any]:
|
|
195
|
+
"""Update a product procurement on an invoice."""
|
|
196
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
197
|
+
procurement_path_id = self._path_id("procurement_id", procurement_id)
|
|
198
|
+
response = self.client.request(
|
|
199
|
+
"PATCH",
|
|
200
|
+
f"/api/v1/invoice/{invoice_path_id}/procurement/{procurement_path_id}",
|
|
201
|
+
json=procurement_data.to_dict(),
|
|
202
|
+
)
|
|
203
|
+
return self._handle_response(response)
|
|
204
|
+
|
|
205
|
+
def delete_procurement(
|
|
206
|
+
self,
|
|
207
|
+
invoice_id: int,
|
|
208
|
+
procurement_id: int,
|
|
209
|
+
) -> dict[str, Any]:
|
|
210
|
+
"""Remove a product procurement from an invoice."""
|
|
211
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
212
|
+
procurement_path_id = self._path_id("procurement_id", procurement_id)
|
|
213
|
+
response = self.client.request(
|
|
214
|
+
"DELETE",
|
|
215
|
+
f"/api/v1/invoice/{invoice_path_id}/procurement/{procurement_path_id}",
|
|
216
|
+
)
|
|
217
|
+
return self._handle_response(response)
|
|
218
|
+
|
|
219
|
+
def delete_invoice(self, invoice_id: int) -> dict[str, Any]:
|
|
220
|
+
"""Delete an invoice."""
|
|
221
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
222
|
+
response = self.client.request(
|
|
223
|
+
"DELETE",
|
|
224
|
+
f"/api/v1/invoice/{invoice_path_id}",
|
|
225
|
+
)
|
|
226
|
+
return self._handle_response(response)
|
|
227
|
+
|
|
228
|
+
def create_recurring_schedule(
|
|
229
|
+
self,
|
|
230
|
+
invoice_id: int,
|
|
231
|
+
schedule_data: RecurringScheduleCreate,
|
|
232
|
+
) -> dict[str, Any]:
|
|
233
|
+
"""Create a recurring schedule for an invoice."""
|
|
234
|
+
|
|
235
|
+
Invoice._validate_date("start_date", schedule_data.start_date)
|
|
236
|
+
Invoice._validate_date("end_date", schedule_data.end_date)
|
|
237
|
+
|
|
238
|
+
if schedule_data.specifically_on:
|
|
239
|
+
Invoice._validate_date("specifically_on", schedule_data.specifically_on)
|
|
240
|
+
|
|
241
|
+
invoice_path_id = self._path_id("invoice_id", invoice_id)
|
|
242
|
+
response = self.client.request(
|
|
243
|
+
"POST",
|
|
244
|
+
f"/api/v1/invoice/{invoice_path_id}/schedule",
|
|
245
|
+
json=schedule_data.to_dict(),
|
|
246
|
+
)
|
|
247
|
+
return self._handle_response(response)
|
|
248
|
+
|
|
249
|
+
def update_recurring_schedule(
|
|
250
|
+
self,
|
|
251
|
+
invoice_schedule_id: int,
|
|
252
|
+
schedule_data: RecurringScheduleUpdate,
|
|
253
|
+
) -> dict[str, Any]:
|
|
254
|
+
"""Update an invoice recurring schedule."""
|
|
255
|
+
|
|
256
|
+
Invoice._validate_date("start_date", schedule_data.start_date)
|
|
257
|
+
Invoice._validate_date("end_date", schedule_data.end_date)
|
|
258
|
+
Invoice._validate_date("specifically_on", schedule_data.specifically_on)
|
|
259
|
+
schedule_path_id = self._path_id(
|
|
260
|
+
"invoice_schedule_id",
|
|
261
|
+
invoice_schedule_id,
|
|
262
|
+
)
|
|
263
|
+
response = self.client.request(
|
|
264
|
+
"PATCH",
|
|
265
|
+
f"/api/v1/invoice/{schedule_path_id}/schedule",
|
|
266
|
+
json=schedule_data.to_dict(),
|
|
267
|
+
)
|
|
268
|
+
return self._handle_response(response)
|