Pyiiko 0.4.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.
Pyiiko/iiko_web.py ADDED
@@ -0,0 +1,392 @@
1
+ """iiko Public Web API client."""
2
+ from __future__ import annotations
3
+
4
+ import logging
5
+ from typing import Any
6
+
7
+ from ._base import DEFAULT_TIMEOUT, BaseIikoClient
8
+ from ._web_docs import _DocsMixin
9
+ from .exceptions import IikoAuthError
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+ _API_BASE = "https://public-api.iikoweb.ru"
14
+
15
+
16
+ class IikoWeb(_DocsMixin, BaseIikoClient):
17
+ """Client for the iiko Public Web API.
18
+
19
+ Authenticates with an API key (and optional app_id / client_secret) and
20
+ stores a Bearer token for subsequent requests::
21
+
22
+ client = IikoWeb(api_key="your-api-key")
23
+ stores = client.stores()
24
+
25
+ Pass a pre-fetched *token* dict to skip the auth round-trip::
26
+
27
+ client = IikoWeb(token={"token": "...", "expires_in": ...})
28
+ """
29
+
30
+ def __init__(
31
+ self,
32
+ api_key: str | None = None,
33
+ app_id: str | None = None,
34
+ client_secret: str | None = None,
35
+ token: dict | None = None,
36
+ timeout: int = DEFAULT_TIMEOUT,
37
+ ) -> None:
38
+ super().__init__(_API_BASE, timeout)
39
+ self.api_key = api_key
40
+ self.app_id = app_id
41
+ self.client_secret = client_secret
42
+ self._token: dict = token if token is not None else self.get_token()
43
+
44
+ # ------------------------------------------------------------------
45
+ # Auth
46
+ # ------------------------------------------------------------------
47
+
48
+ def get_token(self) -> dict:
49
+ """Fetch a new Bearer token using the API key.
50
+
51
+ :returns: Token response dict, e.g. ``{"token": "...", "expires_in": ...}``.
52
+ :raises IikoAuthError: If the server returns an empty token.
53
+ """
54
+ body: dict[str, Any] = {"api_key": self.api_key}
55
+ if self.app_id is not None:
56
+ body = {**body, "app_id": self.app_id}
57
+ if self.client_secret is not None:
58
+ body = {**body, "client_secret": self.client_secret}
59
+ response = self._post("auth", json=body)
60
+ data: dict = response.json()
61
+ if not data.get("token"):
62
+ raise IikoAuthError("IikoWeb API returned an empty token")
63
+ return data
64
+
65
+ def token(self) -> dict:
66
+ """Return the current token response dict."""
67
+ return self._token
68
+
69
+ def _auth(self) -> dict[str, str]:
70
+ """Return Authorization header for the current token."""
71
+ return {"Authorization": f"Bearer {self._token['token']}"}
72
+
73
+ # ------------------------------------------------------------------
74
+ # Entities
75
+ # ------------------------------------------------------------------
76
+
77
+ def stores(self) -> Any:
78
+ """Return the list of all stores."""
79
+ return self._post(
80
+ "entities/store/list",
81
+ headers=self._auth(),
82
+ json={},
83
+ )
84
+
85
+ def store(self, store_id: int) -> Any:
86
+ """Return a single store by ID.
87
+
88
+ :param store_id: Numeric store identifier.
89
+ """
90
+ return self._post(
91
+ "entities/store/get",
92
+ headers=self._auth(),
93
+ json={"id": store_id},
94
+ )
95
+
96
+ def products(
97
+ self,
98
+ filters: Any = None,
99
+ limit: int | None = None,
100
+ offset: int | None = None,
101
+ ) -> Any:
102
+ """Return the list of products with optional filtering and pagination.
103
+
104
+ :param filters: Filter criteria dict (optional).
105
+ :param limit: Maximum number of records to return (optional).
106
+ :param offset: Number of records to skip (optional).
107
+ """
108
+ body = {
109
+ k: v
110
+ for k, v in {"filters": filters, "limit": limit, "offset": offset}.items()
111
+ if v is not None
112
+ }
113
+ return self._post(
114
+ "entities/products/list",
115
+ headers=self._auth(),
116
+ json=body,
117
+ )
118
+
119
+ def product_categories(
120
+ self,
121
+ filters: Any = None,
122
+ limit: int | None = None,
123
+ offset: int | None = None,
124
+ ) -> Any:
125
+ """Return the list of product categories.
126
+
127
+ :param filters: Filter criteria dict (optional).
128
+ :param limit: Maximum number of records to return (optional).
129
+ :param offset: Number of records to skip (optional).
130
+ """
131
+ body = {
132
+ k: v
133
+ for k, v in {"filters": filters, "limit": limit, "offset": offset}.items()
134
+ if v is not None
135
+ }
136
+ return self._post(
137
+ "entities/product-category/list",
138
+ headers=self._auth(),
139
+ json=body,
140
+ )
141
+
142
+ def product_sizes(
143
+ self,
144
+ filters: Any = None,
145
+ limit: int | None = None,
146
+ offset: int | None = None,
147
+ ) -> Any:
148
+ """Return the list of product sizes.
149
+
150
+ :param filters: Filter criteria dict (optional).
151
+ :param limit: Maximum number of records to return (optional).
152
+ :param offset: Number of records to skip (optional).
153
+ """
154
+ body = {
155
+ k: v
156
+ for k, v in {"filters": filters, "limit": limit, "offset": offset}.items()
157
+ if v is not None
158
+ }
159
+ return self._post(
160
+ "entities/product-size/list",
161
+ headers=self._auth(),
162
+ json=body,
163
+ )
164
+
165
+ def users(
166
+ self,
167
+ filters: Any = None,
168
+ limit: int | None = None,
169
+ offset: int | None = None,
170
+ ) -> Any:
171
+ """Return the list of users.
172
+
173
+ :param filters: Filter criteria dict (optional).
174
+ :param limit: Maximum number of records to return (optional).
175
+ :param offset: Number of records to skip (optional).
176
+ """
177
+ body = {
178
+ k: v
179
+ for k, v in {"filters": filters, "limit": limit, "offset": offset}.items()
180
+ if v is not None
181
+ }
182
+ return self._post(
183
+ "entities/user/list",
184
+ headers=self._auth(),
185
+ json=body,
186
+ )
187
+
188
+ def payment_types(
189
+ self,
190
+ filters: Any = None,
191
+ limit: int | None = None,
192
+ offset: int | None = None,
193
+ ) -> Any:
194
+ """Return the list of payment types.
195
+
196
+ :param filters: Filter criteria dict (optional).
197
+ :param limit: Maximum number of records to return (optional).
198
+ :param offset: Number of records to skip (optional).
199
+ """
200
+ body = {
201
+ k: v
202
+ for k, v in {"filters": filters, "limit": limit, "offset": offset}.items()
203
+ if v is not None
204
+ }
205
+ return self._post(
206
+ "entities/payment-type/list",
207
+ headers=self._auth(),
208
+ json=body,
209
+ )
210
+
211
+ def cash_flow_categories(
212
+ self,
213
+ filters: Any = None,
214
+ limit: int | None = None,
215
+ offset: int | None = None,
216
+ ) -> Any:
217
+ """Return the list of cash flow categories.
218
+
219
+ :param filters: Filter criteria dict (optional).
220
+ :param limit: Maximum number of records to return (optional).
221
+ :param offset: Number of records to skip (optional).
222
+ """
223
+ body = {
224
+ k: v
225
+ for k, v in {"filters": filters, "limit": limit, "offset": offset}.items()
226
+ if v is not None
227
+ }
228
+ return self._post(
229
+ "entities/cash-flow-category/list",
230
+ headers=self._auth(),
231
+ json=body,
232
+ )
233
+
234
+ # ------------------------------------------------------------------
235
+ # Nomenclature
236
+ # ------------------------------------------------------------------
237
+
238
+ def update_barcodes(self, payload: dict) -> Any:
239
+ """Update barcodes in the nomenclature.
240
+
241
+ :param payload: Barcode update payload.
242
+ """
243
+ return self._post(
244
+ "nomenclature/update-barcodes",
245
+ headers=self._auth(),
246
+ json=payload,
247
+ )
248
+
249
+ # ------------------------------------------------------------------
250
+ # Purchasing — orders
251
+ # ------------------------------------------------------------------
252
+
253
+ def order_create(
254
+ self,
255
+ store_id: int,
256
+ workflow_id: int,
257
+ due_date: str,
258
+ planned_delivery_date: str | None = None,
259
+ ) -> Any:
260
+ """Create a new purchasing order.
261
+
262
+ :param store_id: Numeric store identifier.
263
+ :param workflow_id: Numeric workflow identifier.
264
+ :param due_date: Due date (ISO string).
265
+ :param planned_delivery_date: Planned delivery date (ISO string, optional).
266
+ """
267
+ body: dict[str, Any] = {
268
+ "storeId": store_id,
269
+ "workflowId": workflow_id,
270
+ "dueDate": due_date,
271
+ }
272
+ if planned_delivery_date is not None:
273
+ body = {**body, "plannedDeliveryDate": planned_delivery_date}
274
+ return self._post(
275
+ "purchasing/orders/create",
276
+ headers=self._auth(),
277
+ json=body,
278
+ )
279
+
280
+ def order_get(self, order_id: int) -> Any:
281
+ """Retrieve a purchasing order by ID.
282
+
283
+ :param order_id: Numeric order identifier.
284
+ """
285
+ return self._post(
286
+ "purchasing/orders/get",
287
+ headers=self._auth(),
288
+ json={"id": order_id},
289
+ )
290
+
291
+ def orders_list(self, payload: dict) -> Any:
292
+ """Return a list of purchasing orders matching the given filters.
293
+
294
+ :param payload: Filter and pagination payload.
295
+ """
296
+ return self._post(
297
+ "purchasing/orders/list",
298
+ headers=self._auth(),
299
+ json=payload,
300
+ )
301
+
302
+ def order_add_products(self, payload: dict) -> Any:
303
+ """Add products to an existing purchasing order.
304
+
305
+ :param payload: Payload containing order ID and product list.
306
+ """
307
+ return self._post(
308
+ "purchasing/orders/products/add",
309
+ headers=self._auth(),
310
+ json=payload,
311
+ )
312
+
313
+ def order_select_supplier(self, payload: dict) -> Any:
314
+ """Select a supplier for a purchasing order.
315
+
316
+ :param payload: Payload containing order ID and supplier details.
317
+ """
318
+ return self._post(
319
+ "purchasing/orders/supplier/select",
320
+ headers=self._auth(),
321
+ json=payload,
322
+ )
323
+
324
+ def order_task_status(self, task_id: str) -> Any:
325
+ """Check the status of an async purchasing order task.
326
+
327
+ :param task_id: Task identifier string.
328
+ """
329
+ return self._post(
330
+ "purchasing/orders/task-status",
331
+ headers=self._auth(),
332
+ json={"taskId": task_id},
333
+ )
334
+
335
+ def order_select_units(self, payload: dict) -> Any:
336
+ """Select measurement units for a purchasing order.
337
+
338
+ :param payload: Payload containing order ID and unit selections.
339
+ """
340
+ return self._post(
341
+ "purchasing/orders/units/select",
342
+ headers=self._auth(),
343
+ json=payload,
344
+ )
345
+
346
+ # ------------------------------------------------------------------
347
+ # Purchasing — workflows
348
+ # ------------------------------------------------------------------
349
+
350
+ def workflow_activate(self, workflow_id: int) -> Any:
351
+ """Activate a purchasing workflow.
352
+
353
+ :param workflow_id: Numeric workflow identifier.
354
+ """
355
+ return self._post(
356
+ "purchasing/workflows/activate",
357
+ headers=self._auth(),
358
+ json={"id": workflow_id},
359
+ )
360
+
361
+ def workflow_deactivate(self, workflow_id: int) -> Any:
362
+ """Deactivate a purchasing workflow.
363
+
364
+ :param workflow_id: Numeric workflow identifier.
365
+ """
366
+ return self._post(
367
+ "purchasing/workflows/deactivate",
368
+ headers=self._auth(),
369
+ json={"id": workflow_id},
370
+ )
371
+
372
+ def workflow_get(self, workflow_id: int) -> Any:
373
+ """Retrieve a purchasing workflow by ID.
374
+
375
+ :param workflow_id: Numeric workflow identifier.
376
+ """
377
+ return self._post(
378
+ "purchasing/workflows/get",
379
+ headers=self._auth(),
380
+ json={"id": workflow_id},
381
+ )
382
+
383
+ def workflows_list(self, payload: dict | None = None) -> Any:
384
+ """Return the list of purchasing workflows.
385
+
386
+ :param payload: Optional filter payload; defaults to empty dict.
387
+ """
388
+ return self._post(
389
+ "purchasing/workflows/list",
390
+ headers=self._auth(),
391
+ json=payload or {},
392
+ )
Pyiiko/py.typed ADDED
File without changes