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.
Files changed (45) hide show
  1. branddrive/__init__.py +33 -0
  2. branddrive/client.py +134 -0
  3. branddrive/config.py +15 -0
  4. branddrive/dataclasses/__init__.py +118 -0
  5. branddrive/dataclasses/_utils.py +8 -0
  6. branddrive/dataclasses/customers.py +66 -0
  7. branddrive/dataclasses/expenses.py +155 -0
  8. branddrive/dataclasses/invoices.py +131 -0
  9. branddrive/dataclasses/payment_links.py +134 -0
  10. branddrive/dataclasses/pos.py +141 -0
  11. branddrive/dataclasses/products.py +166 -0
  12. branddrive/dataclasses/purchase_orders.py +141 -0
  13. branddrive/dataclasses/services.py +76 -0
  14. branddrive/dataclasses/vendors.py +48 -0
  15. branddrive/dataclasses/wallets.py +72 -0
  16. branddrive/exceptions.py +35 -0
  17. branddrive/protocols/__init__.py +43 -0
  18. branddrive/protocols/analytics.py +61 -0
  19. branddrive/protocols/customers.py +42 -0
  20. branddrive/protocols/expenses.py +69 -0
  21. branddrive/protocols/invoices.py +81 -0
  22. branddrive/protocols/payment_links.py +49 -0
  23. branddrive/protocols/pos.py +88 -0
  24. branddrive/protocols/products.py +150 -0
  25. branddrive/protocols/purchase_orders.py +68 -0
  26. branddrive/protocols/services.py +56 -0
  27. branddrive/protocols/vendors.py +39 -0
  28. branddrive/protocols/wallets.py +52 -0
  29. branddrive/resources/__init__.py +125 -0
  30. branddrive/resources/_utils.py +10 -0
  31. branddrive/resources/analytics.py +198 -0
  32. branddrive/resources/customers.py +194 -0
  33. branddrive/resources/expenses.py +209 -0
  34. branddrive/resources/invoices.py +268 -0
  35. branddrive/resources/payment_links.py +265 -0
  36. branddrive/resources/pos.py +353 -0
  37. branddrive/resources/products.py +612 -0
  38. branddrive/resources/purchase_orders.py +235 -0
  39. branddrive/resources/services.py +184 -0
  40. branddrive/resources/vendors.py +152 -0
  41. branddrive/resources/wallets.py +204 -0
  42. branddrive_python_sdk-0.1.0.dist-info/METADATA +132 -0
  43. branddrive_python_sdk-0.1.0.dist-info/RECORD +45 -0
  44. branddrive_python_sdk-0.1.0.dist-info/WHEEL +5 -0
  45. branddrive_python_sdk-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,612 @@
1
+ from __future__ import annotations
2
+
3
+ from ..exceptions import handle_api_exception
4
+ from ..dataclasses.products import (
5
+ ProductDistributionCreate,
6
+ ProductDistributionUpdate,
7
+ SingleProductCreate,
8
+ UpdateProductData,
9
+ ProductVariationCreate,
10
+ ProductCategoryCreate,
11
+ ProductCategoryUpdate,
12
+ )
13
+ from ..protocols.products import (
14
+ _DistributionAPI,
15
+ _GroupedProductAPI,
16
+ _PricingDistributionAPI,
17
+ _SingleProductAPI,
18
+ _VariationAPI,
19
+ _CategoryAPI,
20
+ )
21
+
22
+ from urllib.parse import quote
23
+ from typing import Any, Optional
24
+
25
+ import httpx
26
+
27
+
28
+ def _validate_product_data(product_data: SingleProductCreate) -> None:
29
+ for name, value in (
30
+ ("selling_price", product_data.selling_price),
31
+ ("cost_price", product_data.cost_price),
32
+ ):
33
+ if isinstance(value, bool) or not isinstance(value, int):
34
+ raise TypeError(f"{name} must be an integer")
35
+ if value < 0:
36
+ raise ValueError(f"{name} must be greater than or equal to 0")
37
+ for name, value in (
38
+ ("category_id", product_data.category_id),
39
+ ("branch_id", product_data.branch_id),
40
+ ):
41
+ if isinstance(value, bool) or not isinstance(value, int):
42
+ raise TypeError(f"{name} must be an integer")
43
+ if value < 1:
44
+ raise ValueError(f"{name} must be greater than 0")
45
+
46
+ if not isinstance(product_data.name, str) or not product_data.name.strip():
47
+ raise ValueError("name must be a non-empty string")
48
+
49
+
50
+ class Product:
51
+ def __init__(self, client: httpx.Client) -> None:
52
+ self.client = client
53
+ self.single: _SingleProductAPI = Single(self.client)
54
+ self.grouped: _GroupedProductAPI = Group(self.client)
55
+ self.distribution: _DistributionAPI = Distribution(self.client)
56
+ self.pricing_distribution: _PricingDistributionAPI = PricingDistribution(
57
+ self.client
58
+ )
59
+ self.variation: _VariationAPI = Variation(self.client)
60
+ self.category: _CategoryAPI = Category(self.client)
61
+
62
+ def _handle_response(self, response: httpx.Response) -> dict[str, Any]:
63
+ if not response.is_success:
64
+ raise handle_api_exception(response)
65
+ return response.json()
66
+
67
+ @staticmethod
68
+ def _validate_id(name: str, value: int) -> None:
69
+ if isinstance(value, bool) or not isinstance(value, int):
70
+ raise TypeError(f"{name} must be an integer")
71
+ if value < 1:
72
+ raise ValueError(f"{name} must be greater than 0")
73
+
74
+ def list_products(
75
+ self,
76
+ limit: int = 10,
77
+ offset: int = 0,
78
+ search: Optional[str] = None,
79
+ sort: Optional[str] = None
80
+ ) -> dict[str, Any]:
81
+ """
82
+ Get All Products
83
+ """
84
+ if limit < 1:
85
+ raise ValueError("limit must be greater than 0")
86
+ if offset < 0:
87
+ raise ValueError("offset must be greater than or equal to 0")
88
+
89
+ if search is not None and not isinstance(search, str):
90
+ raise TypeError("search must be a string")
91
+ if sort is not None and not isinstance(sort, str):
92
+ raise TypeError("sort must be a string")
93
+
94
+ params: dict[str, Any] = {
95
+ "limit": limit,
96
+ "offset": offset
97
+ }
98
+ if search: params["search"] = search
99
+ if sort: params["sort"] = sort
100
+ res = self.client.request(
101
+ "GET",
102
+ "/api/v1/product",
103
+ params=params
104
+ )
105
+ return self._handle_response(res)
106
+
107
+ def export_products(
108
+ self,
109
+ export_type: str,
110
+ email: Optional[str] = None,
111
+ branch_id: Optional[int] = None,
112
+ search: Optional[str] = None,
113
+ category_id: Optional[int] = None,
114
+ limit: int = 10,
115
+ offset: int = 0
116
+ ) -> dict[str, Any]:
117
+
118
+ """
119
+ Export Products
120
+ """
121
+ valid_types = ('ALL', 'SINGLE', 'GROUPED', 'VARIANT')
122
+ if export_type.upper() not in valid_types:
123
+ raise ValueError(f"Invalid export types. {valid_types}")
124
+
125
+ if branch_id: self._validate_id("branch_id", branch_id)
126
+ if category_id: self._validate_id("category_id", category_id)
127
+
128
+ if search is not None and not isinstance(search , str):
129
+ raise TypeError("search must be a string")
130
+
131
+ if limit < 1:
132
+ raise ValueError("limit must be greater than 0")
133
+ if offset < 0:
134
+ raise ValueError("offset must be greater than or equal to 0")
135
+
136
+ params: dict[str, Any] = {
137
+ "limit": limit,
138
+ "offset": offset
139
+ }
140
+
141
+ if email: params['email'] = email
142
+ if branch_id: params["branchId"] = branch_id
143
+ if category_id: params['categoryId'] = category_id
144
+ if search: params['search'] = search
145
+
146
+ res = self.client.request(
147
+ "GET",
148
+ f"/api/v1/product/export/{export_type.upper().strip()}",
149
+ params=params
150
+ )
151
+
152
+ return self._handle_response(res)
153
+
154
+
155
+ def delete_products(
156
+ self,
157
+ product_ids: list[int]
158
+
159
+ ) -> dict[str, Any]:
160
+ """
161
+ Bulk Delete Product
162
+ """
163
+ for id in product_ids:
164
+ self._validate_id("product_id", id)
165
+
166
+ res = self.client.request(
167
+ "DELETE",
168
+ "/api/v1/product/bulk",
169
+ json={"bulkProductIds": product_ids}
170
+ )
171
+
172
+ return self._handle_response(res)
173
+
174
+
175
+ class Single:
176
+ def __init__(self, client: httpx.Client) -> None:
177
+ self.client = client
178
+
179
+ def _handle_response(self, response: httpx.Response) -> Any:
180
+ if not response.is_success:
181
+ raise handle_api_exception(response)
182
+ return response.json()
183
+
184
+ @staticmethod
185
+ def _validate_id(name: str, value: int) -> None:
186
+ if isinstance(value, bool) or not isinstance(value, int):
187
+ raise TypeError(f"{name} must be an integer")
188
+ if value < 1:
189
+ raise ValueError(f"{name} must be greater than 0")
190
+
191
+ def create_product(
192
+ self,
193
+ product_data: SingleProductCreate,
194
+ ) -> dict[str, Any]:
195
+ """Create a single product."""
196
+
197
+ _validate_product_data(product_data)
198
+
199
+ response = self.client.request(
200
+ "POST",
201
+ "/api/v1/product",
202
+ json=product_data.to_dict(),
203
+ )
204
+ return self._handle_response(response)
205
+
206
+ def get_variable_product(self, variable_name: str) -> dict[str, Any]:
207
+ """Get a product by its variable name."""
208
+ if not isinstance(variable_name, str) or not variable_name.strip():
209
+ raise ValueError("variable_name must be a non-empty string")
210
+
211
+ normalized_name = variable_name.strip().replace(" / ", " /")
212
+ encoded_name = quote(normalized_name, safe="")
213
+ response = self.client.request(
214
+ "GET",
215
+ f"/api/v1/product/variable/{encoded_name}",
216
+ )
217
+ return self._handle_response(response)
218
+
219
+ def update_product(
220
+ self,
221
+ product_id: int,
222
+ product_data: UpdateProductData,
223
+ ) -> dict[str, Any]:
224
+ """Update a product."""
225
+ self._validate_id("product_id", product_id)
226
+ payload = product_data.to_dict()
227
+ if not payload:
228
+ raise ValueError("At least one product field must be provided")
229
+
230
+ numeric_fields = (
231
+ ("stock", "stock"),
232
+ ("minimum_stock_alert", "minimumStockAlert"),
233
+ ("selling_price", "sellingPrice"),
234
+ ("markup", "markup"),
235
+ )
236
+ for attribute, key in numeric_fields:
237
+ value = getattr(product_data, attribute)
238
+ if value is not None:
239
+ if isinstance(value, bool) or not isinstance(value, int):
240
+ raise TypeError(f"{attribute} must be an integer")
241
+ if value < 0:
242
+ raise ValueError(f"{attribute} must be greater than or equal to 0")
243
+
244
+ response = self.client.request(
245
+ "PATCH",
246
+ f"/api/v1/product/{product_id}",
247
+ json=payload,
248
+ )
249
+ return self._handle_response(response)
250
+
251
+ class Group:
252
+ def __init__(self, client: httpx.Client) -> None:
253
+ self.client = client
254
+
255
+ def _handle_response(self, response: httpx.Response) -> Any:
256
+ if not response.is_success:
257
+ raise handle_api_exception(response)
258
+ return response.json()
259
+
260
+
261
+ def create_grouped_products(self, products: list[SingleProductCreate]) -> dict[str, Any]:
262
+ """
263
+ Create grouped product
264
+ """
265
+ if not products:
266
+ raise ValueError("products field cannot be left empty")
267
+
268
+ for data in products:
269
+ _validate_product_data(data)
270
+
271
+ payload = {
272
+ "products": [product.to_dict() for product in products]
273
+ }
274
+
275
+ res = self.client.request(
276
+ "POST",
277
+ "/api/v1/products",
278
+ json=payload
279
+ )
280
+ return self._handle_response(res)
281
+
282
+ class PricingDistribution:
283
+
284
+ def __init__(self, client: httpx.Client) -> None:
285
+ self.client = client
286
+
287
+ @staticmethod
288
+ def _validate_id(name: str, value: int) -> str:
289
+ if isinstance(value, bool) or not isinstance(value, int):
290
+ raise TypeError(f"{name} must be an integer")
291
+ if value < 1:
292
+ raise ValueError(f"{name} must be greater than 0")
293
+ return quote(str(value), safe="")
294
+
295
+ def _handle_response(self, response: httpx.Response) -> Any:
296
+ if not response.is_success:
297
+ raise handle_api_exception(response)
298
+ return response.json()
299
+
300
+ @staticmethod
301
+ def _validate_price_distributions(
302
+ price_distributions: list[dict[str, Any]],
303
+ ) -> None:
304
+ if not isinstance(price_distributions, list):
305
+ raise TypeError("price_distributions must be a list")
306
+ if any(not isinstance(item, dict) for item in price_distributions):
307
+ raise TypeError("each price distribution must be a dictionary")
308
+
309
+ def add_pricing_distribution(
310
+ self,
311
+ product_id: int,
312
+ price_distributions: list[dict[str, Any]],
313
+ ) -> dict[str, Any]:
314
+ """Add pricing distributions to a product."""
315
+ product_path_id = self._validate_id("product_id", product_id)
316
+ self._validate_price_distributions(price_distributions)
317
+
318
+ response = self.client.request(
319
+ "POST",
320
+ f"/api/v1/product/{product_path_id}/distributions/pricing",
321
+ json={"priceDistributions": price_distributions},
322
+ )
323
+ return self._handle_response(response)
324
+
325
+ def update_pricing_distribution(
326
+ self,
327
+ product_id: int,
328
+ price_distributions: list[dict[str, Any]],
329
+ ) -> dict[str, Any]:
330
+ """Replace a product's pricing distributions."""
331
+ product_path_id = self._validate_id("product_id", product_id)
332
+ self._validate_price_distributions(price_distributions)
333
+
334
+ response = self.client.request(
335
+ "PATCH",
336
+ f"/api/v1/product/{product_path_id}/distributions/pricing",
337
+ json={"priceDistributions": price_distributions},
338
+ )
339
+ return self._handle_response(response)
340
+
341
+ def delete_pricing_distribution(
342
+ self,
343
+ price_distribution_ids: list[int],
344
+ ) -> dict[str, Any]:
345
+ """Delete pricing distributions by their IDs."""
346
+ if not isinstance(price_distribution_ids, list):
347
+ raise TypeError("price_distribution_ids must be a list")
348
+ if not price_distribution_ids:
349
+ raise ValueError("price_distribution_ids cannot be empty")
350
+
351
+ for distribution_id in price_distribution_ids:
352
+ self._validate_id("price_distribution_id", distribution_id)
353
+
354
+ response = self.client.request(
355
+ "DELETE",
356
+ "/api/v1/product/distributions/pricing",
357
+ json={"priceDistributionIds": price_distribution_ids},
358
+ )
359
+ return self._handle_response(response)
360
+
361
+ class Distribution:
362
+
363
+ def __init__(self, client: httpx.Client) -> None:
364
+ self.client = client
365
+
366
+ @staticmethod
367
+ def _validate_id(name: str, value: int) -> str:
368
+ if isinstance(value, bool) or not isinstance(value, int):
369
+ raise TypeError(f"{name} must be an integer")
370
+ if value < 1:
371
+ raise ValueError(f"{name} must be greater than 0")
372
+ return quote(str(value), safe="")
373
+
374
+ def _handle_response(self, response: httpx.Response) -> Any:
375
+ if not response.is_success:
376
+ raise handle_api_exception(response)
377
+ return response.json()
378
+
379
+ @staticmethod
380
+ def _validate_distributions(distributions: dict) -> None:
381
+ for key, value in distributions.values():
382
+ if not isinstance(value, int):
383
+ raise TypeError(f"{key}, must be of type integer")
384
+ if value < 0:
385
+ raise ValueError(f"{key}, cannot be less than 0")
386
+
387
+ def add_distribution(
388
+ self,
389
+ product_id: int,
390
+ distribution: ProductDistributionCreate
391
+ ) -> dict[str, Any]:
392
+ """
393
+ Add product distribution
394
+ """
395
+ product_path_id = self._validate_id("product_id", product_id)
396
+ payload = distribution.to_dict()
397
+ self._validate_distributions(payload)
398
+
399
+ res = self.client.request(
400
+ "POST",
401
+ f"/api/v1/product/{product_path_id}/distributions",
402
+ json=payload
403
+
404
+ )
405
+ return self._handle_response(res)
406
+
407
+ def update_distribution(
408
+ self,
409
+ product_id: int,
410
+ distribution: ProductDistributionUpdate
411
+ ) -> dict[str, Any]:
412
+ """
413
+ Update product distribution
414
+ """
415
+ product_path_id = self._validate_id("product_id", product_id)
416
+ distribution_id = self._validate_id("distribution_id", distribution.id)
417
+ payload = distribution.to_dict()
418
+ self._validate_distributions(payload)
419
+
420
+ res = self.client.request(
421
+ "PATCH",
422
+ f"api/v1/product/{product_path_id}/distributions",
423
+ json=payload
424
+ )
425
+ return self._handle_response(res)
426
+
427
+
428
+ def delete_distribution(
429
+ self,
430
+ product_id: int,
431
+ distribution_ids: list[int]
432
+ ) -> dict[str, Any]:
433
+ """
434
+ Bulk delete product distributions
435
+ """
436
+ product_path_id = self._validate_id("product_id", product_id)
437
+ for id in distribution_ids:
438
+ self._validate_id("distribution_id", id)
439
+
440
+ res = self.client.request(
441
+ "DELETE",
442
+ f"api/v1/product/{product_path_id}/distributions"
443
+ )
444
+ return self._handle_response(res)
445
+
446
+ class Variation:
447
+ def __init__(self, client: httpx.Client) -> None:
448
+ self.client = client
449
+
450
+ @staticmethod
451
+ def _validate_id(name: str, value: int) -> str:
452
+ if isinstance(value, bool) or not isinstance(value, int):
453
+ raise TypeError(f"{name} must be an integer")
454
+ if value < 1:
455
+ raise ValueError(f"{name} must be greater than 0")
456
+ return quote(str(value), safe="")
457
+
458
+ def _handle_response(self, response: httpx.Response) -> Any:
459
+ if not response.is_success:
460
+ raise handle_api_exception(response)
461
+ return response.json()
462
+
463
+ def regenerate_variation(
464
+ self,
465
+ product_id: int,
466
+ variation: ProductVariationCreate
467
+ ) -> dict[str, Any]:
468
+ """
469
+ Regenerate Product Variations
470
+ """
471
+ payload = variation.to_dict()
472
+ if not payload:
473
+ raise ValueError("Atleast one field must be present")
474
+
475
+ product_id_path = self._validate_id("product_id", product_id)
476
+ res = self.client.request(
477
+ "POST",
478
+ f"/api/v1/product/{product_id_path}/variation",
479
+ json={ 'variations': payload }
480
+ )
481
+
482
+ return self._handle_response(res)
483
+
484
+ def update_variation(
485
+ self,
486
+ variation_id: int,
487
+ variation: ProductVariationCreate
488
+ ) -> dict[str, Any]:
489
+ """
490
+ Update product variation
491
+ """
492
+ payload: dict[str, Any] = {}
493
+ optional_fields = (
494
+ ("amount"),
495
+ ("name"),
496
+ ('stock')
497
+ )
498
+ for key in optional_fields:
499
+ value = getattr(variation, key)
500
+ if value is not None:
501
+ payload[key] = value
502
+
503
+ variation_path_id = self._validate_id("variation_id", variation_id)
504
+ res = self.client.request(
505
+ "PATCH",
506
+ f"api/v1/product/variation/{variation_path_id}",
507
+ json=payload
508
+ )
509
+ return self._handle_response(res)
510
+
511
+ def delete_variation(
512
+ self,
513
+ product_id: int,
514
+ variation_name: str,
515
+ attribute: str
516
+ ) -> dict[str, Any]:
517
+ """
518
+ Delete product variation
519
+ """
520
+ product_path_id = self._validate_id("product_id", product_id)
521
+ res = self.client.request(
522
+ "DELETE",
523
+ f"/api/v1/product/{product_path_id}/variation/{variation_name}/{attribute}"
524
+ )
525
+ return self._handle_response(res)
526
+
527
+ class Category:
528
+
529
+ def __init__(self, client: httpx.Client) -> None:
530
+ self.client = client
531
+
532
+ @staticmethod
533
+ def _validate_id(name: str, value: int) -> str:
534
+ if isinstance(value, bool) or not isinstance(value, int):
535
+ raise TypeError(f"{name} must be an integer")
536
+ if value < 1:
537
+ raise ValueError(f"{name} must be greater than 0")
538
+ return quote(str(value), safe="")
539
+
540
+ def _handle_response(self, response: httpx.Response) -> Any:
541
+ if not response.is_success:
542
+ raise handle_api_exception(response)
543
+ return response.json()
544
+
545
+ def create_category(
546
+ self,
547
+ category: ProductCategoryCreate
548
+ ) -> dict[str, Any]:
549
+ """
550
+ Create Product Category
551
+ """
552
+ if not isinstance(category.name, str) or not category.name.strip():
553
+ raise ValueError('Category name must be a non empty string')
554
+
555
+ payload = category.to_dict()
556
+ res = self.client.request(
557
+ "POST",
558
+ "/api/v1/product/category",
559
+ json=payload
560
+ )
561
+ return self._handle_response(res)
562
+
563
+
564
+ def list_categories(
565
+ self,
566
+ limit: int = 10
567
+ ) -> dict[str, Any]:
568
+ """
569
+ Get All Product Categories
570
+ """
571
+ if limit < 1:
572
+ raise ValueError("limit cannot be less than 0")
573
+ res = self.client.request(
574
+ "GET",
575
+ "/api/v1/product/category",
576
+ params={"limit": limit}
577
+ )
578
+ return self._handle_response(res)
579
+
580
+ def update_category(
581
+ self,
582
+ category_id: int,
583
+ category_data: ProductCategoryUpdate
584
+ ) -> dict[str, Any]:
585
+ """
586
+ Update product category
587
+ """
588
+ category_path_id = self._validate_id("category_id", category_id)
589
+ payload = category_data.to_dict()
590
+ if not payload:
591
+ raise ValueError("Provide a field to update category")
592
+
593
+ res = self.client.request(
594
+ "PATCH",
595
+ f"/api/v1/product/category/{category_path_id}",
596
+ json=payload
597
+ )
598
+ return self._handle_response(res)
599
+
600
+ def delete_category(
601
+ self,
602
+ category_id: int
603
+ ) -> dict[str, Any]:
604
+ """
605
+ Delete product category
606
+ """
607
+ category_path_id = self._validate_id("catgory_id", category_id)
608
+ res = self.client.request(
609
+ "DELETE",
610
+ f"/api/v1/product/category/{category_path_id}"
611
+ )
612
+ return self._handle_response(res)