kitchenowl-python 0.0.1__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.
@@ -0,0 +1,2 @@
1
+ """KitchenOwl API."""
2
+
@@ -0,0 +1 @@
1
+ """Constants for the KitchenOwl API."""
@@ -0,0 +1,13 @@
1
+ """KitchenOwl exceptions."""
2
+
3
+
4
+ class KitchenOwlException(Exception):
5
+ """Raised when a general exception occours."""
6
+
7
+
8
+ class KitchenOwlRequestException(KitchenOwlException):
9
+ """Raised on a bad request to the KitchenOwl instance."""
10
+
11
+
12
+ class KitchenOwlAuthException(KitchenOwlException):
13
+ """Raised when the authentication token is not valid."""
@@ -0,0 +1,364 @@
1
+ """KitchnOwl API wrapper."""
2
+
3
+ import asyncio
4
+ import logging
5
+ from http import HTTPStatus
6
+ from typing import Any
7
+
8
+ import aiohttp
9
+ from aiohttp.hdrs import METH_DELETE, METH_GET, METH_HEAD, METH_POST
10
+
11
+ from .exceptions import KitchenOwlAuthException, KitchenOwlRequestException
12
+ from .types import (
13
+ KitchenOwlHouseholdsResponse,
14
+ KitchenOwlItem,
15
+ KitchenOwlShoppingListItem,
16
+ KitchenOwlShoppingListItemsResponse,
17
+ KitchenOwlShoppingListsResponse,
18
+ KitchenOwlUser,
19
+ )
20
+
21
+ _LOGGER = logging.getLogger(__name__)
22
+
23
+
24
+ class KitchenOwl:
25
+ """Unnoficial KitchenOwl API interface.
26
+
27
+ Handles communication with the KitchenOwl REST API.
28
+
29
+ Attributes:
30
+ session: An ClientSession that handles the communication with the KitchenOwl REST API.
31
+ url: A string representing the URL of the KitchenOwl instance.
32
+ token: A string representing the Long-Lived Access Token for accessing the KitchenOwl API.
33
+
34
+ """
35
+
36
+ def __init__(self, session: aiohttp.ClientSession, url: str, token: str) -> None:
37
+ """Init function for KitchenOwl API.
38
+
39
+ Args:
40
+ session: An ClientSession that handles the communication with the KitchenOwl REST API.
41
+ url: A string representing the URL of the KitchenOwl instance.
42
+ token: A string representing the Long-Lived Access Token for accessing the KitchenOwl API.
43
+
44
+ """
45
+
46
+ self._session = session
47
+
48
+ self._base_url = url
49
+ self._token = token
50
+ self._request_timeout = 25
51
+
52
+ self._headers = {
53
+ "accept": "application/json",
54
+ "Authorization": f"Bearer {self._token}",
55
+ }
56
+
57
+ async def _request(
58
+ self,
59
+ method: str,
60
+ path: str,
61
+ params: dict[str, Any] | None = None,
62
+ json_data: dict[str, Any] | None = None,
63
+ return_json=False,
64
+ ) -> Any:
65
+ """Perform a HTTP request to the KitchenOwl instance."""
66
+
67
+ url = f"{self._base_url}/{path}"
68
+
69
+ try:
70
+ async with asyncio.timeout(self._request_timeout):
71
+ r = await self._session.request(
72
+ method, url, headers=self._headers, params=params, json=json_data
73
+ )
74
+ if r.status == HTTPStatus.UNAUTHORIZED:
75
+ raise KitchenOwlAuthException("Login not possible: not authorized")
76
+ if r.status == HTTPStatus.UNPROCESSABLE_ENTITY:
77
+ raise KitchenOwlAuthException(
78
+ "Login not possible: authorization incorrect, please check your authorization token."
79
+ )
80
+ r.raise_for_status()
81
+
82
+ except aiohttp.ClientError as e:
83
+ raise KitchenOwlRequestException("Error during request") from e
84
+
85
+ if return_json:
86
+ content_type = r.headers.get("Content-type", "")
87
+ text = await r.text()
88
+ if "application/json" not in content_type:
89
+ raise KitchenOwlRequestException(
90
+ "Expected JSON response from server",
91
+ {"content_type": content_type, "response": text},
92
+ )
93
+
94
+ return await r.json()
95
+
96
+ return r.status == HTTPStatus.OK
97
+
98
+ async def _post(self, path: str, json_data: dict, return_json=False) -> Any:
99
+ """Perform a POST request to the KitchenOwl instance."""
100
+
101
+ return await self._request(
102
+ METH_POST, path=path, json_data=json_data, return_json=return_json
103
+ )
104
+
105
+ async def _get(self, path: str) -> dict:
106
+ """Perform a GET request to the KitchenOwl instance."""
107
+
108
+ return await self._request(METH_GET, path=path, return_json=True)
109
+
110
+ async def _head(self, path: str) -> bool:
111
+ """Perform a HEAD request to the KitchenOwl instance."""
112
+
113
+ return await self._request(METH_HEAD, path=path, return_json=False)
114
+
115
+ async def _delete(self, path: str, json_data: dict) -> bool:
116
+ """Perform a DELETE request to the KitchenOwl instance."""
117
+
118
+ return await self._request(METH_DELETE, path=path, json_data=json_data, return_json=False)
119
+
120
+ async def test_connection(self) -> bool:
121
+ """Test the kitchenowl token by performing HEAD on the user endpoint.
122
+
123
+ Returns:
124
+ A True bool value if a connection to the API endpoint can be established.
125
+ Note: False is never returned as Errors are raised in this case.
126
+
127
+ Raises:
128
+ KitchenOwlTimeoutException: If the request times out
129
+ KitchenOwlRequestException: If there is an error during the request
130
+ KitchenOwlAuthException: If the token is not provided or incorrect
131
+
132
+ """
133
+ return await self._head("api/user")
134
+
135
+ async def get_user_info(self) -> KitchenOwlUser:
136
+ """Return the user informaiton.
137
+
138
+ Returns:
139
+ A KitchenOwlUser object containing the information about the user.
140
+
141
+ Raises:
142
+ TimeoutError: If the request times out
143
+ KitchenOwlRequestException: If there is an error during the request
144
+ KitchenOwlAuthException: If the token is not provided or incorrect
145
+
146
+ """
147
+
148
+ return KitchenOwlUser(await self._get("api/user"))
149
+
150
+ async def get_households(self) -> KitchenOwlHouseholdsResponse:
151
+ """Return all households for the user.
152
+
153
+ Returns:
154
+ A KitchenOwlHouseholdsResponse object containing the information about all households the user is registered for.
155
+
156
+ Raises:
157
+ TimeoutError: If the request times out
158
+ KitchenOwlRequestException: If there is an error during the request
159
+ KitchenOwlAuthException: If the token is not provided or incorrect
160
+
161
+ """
162
+
163
+ return KitchenOwlHouseholdsResponse(await self._get("api/household"))
164
+
165
+ async def get_shoppinglists(self, household_id) -> KitchenOwlShoppingListsResponse:
166
+ """Get all shopping lists for the household.
167
+
168
+ Args:
169
+ household_id: A positive integer value of the household id.
170
+
171
+ Returns:
172
+ A KitchenOwlShoppingListsResponse object containing the information about all shopping lists for the household.
173
+
174
+ Raises:
175
+ TimeoutError: If the request times out
176
+ KitchenOwlRequestException: If there is an error during the request
177
+ KitchenOwlAuthException: If the token is not provided or incorrect
178
+
179
+ """
180
+
181
+ return KitchenOwlShoppingListsResponse(
182
+ await self._get(f"api/household/{household_id}/shoppinglist")
183
+ )
184
+
185
+ async def get_shoppinglist_items(self, list_id: int) -> KitchenOwlShoppingListItemsResponse:
186
+ """Get all shopping list items on the list.
187
+
188
+ Args:
189
+ list_id: A positive integer value of the shopping list id.
190
+
191
+ Returns:
192
+ A KitchenOwlShoppingListsResponse object containing the information about all shopping lists for the household.
193
+
194
+ Raises:
195
+ TimeoutError: If the request times out
196
+ KitchenOwlRequestException: If there is an error during the request
197
+ KitchenOwlAuthException: If the token is not provided or incorrect
198
+
199
+ """
200
+
201
+ return KitchenOwlShoppingListItemsResponse(
202
+ await self._get(f"api/shoppinglist/{list_id}/items")
203
+ )
204
+
205
+ async def get_shoppinglist_recent_items(
206
+ self, list_id: int
207
+ ) -> KitchenOwlShoppingListItemsResponse:
208
+ """Get the recent shopping list items for the shopping list.
209
+
210
+ Args:
211
+ list_id: A positive integer value of the shopping list id.
212
+
213
+ Returns:
214
+ A KitchenOwlShoppingListItemsResponse object containing all items recently used on the shopping list
215
+
216
+ Raises:
217
+ TimeoutError: If the request times out
218
+ KitchenOwlRequestException: If there is an error during the request
219
+ KitchenOwlAuthException: If the token is not provided or incorrect
220
+
221
+ """
222
+
223
+ return KitchenOwlShoppingListItemsResponse(
224
+ await self._get(f"api/shoppinglist/{list_id}/recent-items")
225
+ )
226
+
227
+ async def get_shoppinglist_suggested_items(
228
+ self, list_id: int
229
+ ) -> KitchenOwlShoppingListItemsResponse:
230
+ """Get the suggested shopping list items for the shopping list.
231
+
232
+ Args:
233
+ list_id: A positive integer value of the shopping list id.
234
+
235
+ Returns:
236
+ A KitchenOwlShoppingListItemsResponse object containing all items that are suggestedfor the shopping list
237
+
238
+ Raises:
239
+ TimeoutError: If the request times out
240
+ KitchenOwlRequestException: If there is an error during the request
241
+ KitchenOwlAuthException: If the token is not provided or incorrect
242
+
243
+
244
+ """
245
+
246
+ return KitchenOwlShoppingListItemsResponse(
247
+ await self._get(f"api/shoppinglist/{list_id}/suggested-items")
248
+ )
249
+
250
+ async def add_shoppinglist_item(
251
+ self, list_id: int, item_name: str, item_description: str = ""
252
+ ) -> KitchenOwlShoppingListItem:
253
+ """Add an item to the shopping list by name.
254
+
255
+ Args:
256
+ list_id: A positive integer value of the shopping list id.
257
+ item_name: A string representing the name of the item to add to the list.
258
+ item_description: An optional string to add to the description of the item.
259
+
260
+ Returns:
261
+ A KitchenOwlShoppingListItem object containing the added item on success.
262
+
263
+ Raises:
264
+ TimeoutError: If the request times out
265
+ KitchenOwlRequestException: If there is an error during the request
266
+ KitchenOwlAuthException: If the token is not provided or incorrect
267
+
268
+
269
+ """
270
+
271
+ item = {"name": item_name, "description": item_description}
272
+ return KitchenOwlShoppingListItem(
273
+ await self._post(f"api/shoppinglist/{list_id}/add-item-by-name", item, True)
274
+ )
275
+
276
+ async def update_shoppinglist_item_description(
277
+ self, list_id: int, item_id: int, item_description: str
278
+ ) -> KitchenOwlShoppingListItem:
279
+ """Update the description of an item on the shopping list.
280
+
281
+ Args:
282
+ list_id: A positive integer value of the shopping list id.
283
+ item_id: An integer as the id of the item to update.
284
+ item_description: The description string to add to the item.
285
+
286
+ Returns:
287
+ A KitchenOwlShoppingListItem object containing the updated item on success.
288
+
289
+ Raises:
290
+ TimeoutError: If the request times out
291
+ KitchenOwlRequestException: If there is an error during the request
292
+ KitchenOwlAuthException: If the token is not provided or incorrect
293
+
294
+
295
+ """
296
+
297
+ json_data = {"description": item_description}
298
+
299
+ return KitchenOwlShoppingListItem(
300
+ await self._post(f"api/shoppinglist/{list_id}/item/{item_id}", json_data, True)
301
+ )
302
+
303
+ async def remove_shoppinglist_item(self, list_id: int, item_id: int) -> bool:
304
+ """Remove the item from the shopping list.
305
+
306
+ Args:
307
+ list_id: A positive integer value of the shopping list id.
308
+ item_id: An integer as the id of the item to update.
309
+
310
+ Returns:
311
+ A bool (True) in case of success.
312
+
313
+ Raises:
314
+ TimeoutError: If the request times out
315
+ KitchenOwlRequestException: If there is an error during the request
316
+ KitchenOwlAuthException: If the token is not provided or incorrect
317
+
318
+
319
+ """
320
+
321
+ json_data = {"item_id": item_id}
322
+
323
+ return await self._delete(f"api/shoppinglist/{list_id}/item", json_data)
324
+
325
+ async def update_item(self, item_id: int, item: KitchenOwlItem) -> KitchenOwlItem:
326
+ """Update an item.
327
+
328
+ Args:
329
+ item_id: An integer as the id of the item to update.
330
+ item: A dict containing the item data to update.
331
+
332
+
333
+ Returns:
334
+ A KitchenOwlShoppingListItem object containing the updated item on success.
335
+
336
+ Raises:
337
+ TimeoutError: If the request times out
338
+ KitchenOwlRequestException: If there is an error during the request
339
+ KitchenOwlAuthException: If the token is not provided or incorrect
340
+
341
+
342
+ """
343
+
344
+ return KitchenOwlItem(await self._post(f"api/item/{item_id}", item, True))
345
+
346
+ async def delete_item(self, item_id: int) -> KitchenOwlItem:
347
+ """Delete an item.
348
+
349
+ Args:
350
+ item_id: An integer as the id of the item to delete.
351
+
352
+
353
+ Returns:
354
+ A bool (True) in case of success.
355
+
356
+ Raises:
357
+ TimeoutError: If the request times out
358
+ KitchenOwlRequestException: If there is an error during the request
359
+ KitchenOwlAuthException: If the token is not provided or incorrect
360
+
361
+
362
+ """
363
+
364
+ return await self._delete(path=f"api/item/{item_id}", json_data={})
@@ -0,0 +1,93 @@
1
+ """KitchenOwl API types."""
2
+
3
+ # TypedDict for now as it allows for changes in the API return values
4
+
5
+ from typing import List, NotRequired, TypedDict
6
+
7
+
8
+ class KitchenOwlShoppingListCategory(TypedDict):
9
+ """A KitchenOwl category for an item for a shopping list."""
10
+
11
+ name: str
12
+ id: int
13
+ ordering: int
14
+ household_id: int
15
+ description: str
16
+ updated_at: int
17
+ created_at: int
18
+ default: bool
19
+ default_key: str
20
+
21
+
22
+ class KitchenOwlItem(TypedDict):
23
+ """A KitchenOwl item for a shopping list."""
24
+
25
+ name: str
26
+ id: int
27
+ ordering: NotRequired[int]
28
+ category: NotRequired[KitchenOwlShoppingListCategory]
29
+ category_id: NotRequired[int]
30
+ household_id: NotRequired[int]
31
+ updated_at: NotRequired[int]
32
+ created_at: NotRequired[int]
33
+ default: NotRequired[bool]
34
+ default_key: NotRequired[str]
35
+ icon: NotRequired[str]
36
+ support: NotRequired[int]
37
+
38
+
39
+ class KitchenOwlShoppingListItem(KitchenOwlItem):
40
+ """A KitchenOwl item on a shopping list."""
41
+
42
+ description: str
43
+
44
+
45
+ class KitchenOwlShoppingList(TypedDict):
46
+ """A KitchenOwl shopping list."""
47
+
48
+ created_at: int
49
+ household_id: int
50
+ id: int
51
+ name: str
52
+ updated_at: int
53
+
54
+
55
+ class KitchenOwlUser(TypedDict):
56
+ """A user entry."""
57
+
58
+ admin: bool
59
+ created_at: int
60
+ id: int
61
+ name: str
62
+ owner: bool
63
+ photo: str | None
64
+ updated_at: int
65
+ username: str
66
+
67
+
68
+ class KitchenOwlHousehold(TypedDict):
69
+ """A KitchenOwl household."""
70
+
71
+ created_at: int
72
+ default_shopping_list: KitchenOwlShoppingList
73
+ expenses_feature: bool
74
+ id: int
75
+ language: str
76
+ member: List[KitchenOwlUser]
77
+ name: str
78
+ photo: str | None
79
+ planner_feature: bool
80
+ updated_at: int
81
+ view_ordering: List[str]
82
+
83
+
84
+ class KitchenOwlHouseholdsResponse(List[KitchenOwlHousehold]):
85
+ """The households response from KitchenOwl."""
86
+
87
+
88
+ class KitchenOwlShoppingListsResponse(List[KitchenOwlShoppingList]):
89
+ """The shopping lists response from KitchenOwl."""
90
+
91
+
92
+ class KitchenOwlShoppingListItemsResponse(List[KitchenOwlShoppingListItem]):
93
+ """The response for shopping list items from KitchenOwl."""
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 super-qua, Tom Bursch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,87 @@
1
+ Metadata-Version: 2.1
2
+ Name: kitchenowl-python
3
+ Version: 0.0.1
4
+ Summary: A python wrapper for the KitchenOwl API
5
+ Author: super-qua
6
+ Author-email: Tom Bursch <tom@kitchenowl.org>
7
+ License: MIT License
8
+
9
+ Copyright (c) 2024 super-qua, Tom Bursch
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+
29
+ Project-URL: Homepage, https://kitchenowl.org
30
+ Project-URL: Repository, https://github.com/tombursch/kitchenowl-python
31
+ Project-URL: Issues, https://github.com/TomBursch/kitchenowl/issues
32
+ Project-URL: Changelog, https://github.com/tombursch/kitchenowl-python/blob/main/CHANGELOG.md
33
+ Keywords: kitchenowl
34
+ Classifier: Development Status :: 3 - Alpha
35
+ Classifier: Programming Language :: Python
36
+ Requires-Python: >=3.8
37
+ Description-Content-Type: text/markdown
38
+ License-File: LICENSE
39
+ Requires-Dist: aiohttp==3.10.5
40
+ Provides-Extra: test
41
+ Requires-Dist: aioresponses==0.7.6; extra == "test"
42
+ Requires-Dist: pytest==8.2.2; extra == "test"
43
+ Requires-Dist: pytest-asyncio==0.23.7; extra == "test"
44
+ Requires-Dist: pytest-cov==6.0.0; extra == "test"
45
+ Requires-Dist: syrupy==4.6.1; extra == "test"
46
+ Provides-Extra: lint
47
+ Requires-Dist: ruff==0.6.1; extra == "lint"
48
+
49
+ # kitchenowl-python
50
+ A simple wrapper around the KitchenOwl API.
51
+
52
+ This is a small python package to be used as a wrapper for the KitchenOwl API in python.
53
+
54
+ Currently, there is only support for managing shopping list items.
55
+
56
+ ## Installation
57
+
58
+ ```shell
59
+ python -m venv .venv
60
+ source .venv/bin/activate
61
+ pip install -e .
62
+ ```
63
+ Installs all required dependencies.
64
+
65
+ ## Usage
66
+
67
+ ```python
68
+ from aiohttp import ClientSession
69
+ from kitchenowl_python.kitchenowl import KitchenOwl
70
+
71
+ async with ClientSession() as session:
72
+ kitchenowl = KitchenOwl(session=session, url=url, token=token)
73
+ await kitchenowl.test_connection()
74
+
75
+ ```
76
+
77
+ ## Development
78
+
79
+ ### Run tests
80
+
81
+ ```shell
82
+ source .venv/bin/activate
83
+ pip install -e .\[test\]
84
+ pytest .
85
+ ```
86
+
87
+
@@ -0,0 +1,10 @@
1
+ kitchenowl_python/__init__.py,sha256=g83X8m-ylRrorkwt16iXNIOxvFI6-ZgUacrvVlhR07o,23
2
+ kitchenowl_python/const.py,sha256=B9mhQl6C1EN6KrzRliXI3fCg12PULIztbn9P8xBhipU,40
3
+ kitchenowl_python/exceptions.py,sha256=VPMcefOC9dDT_Tb-8c7TGVHvVEorHlR6qZmyhrDAYX0,354
4
+ kitchenowl_python/kitchenowl.py,sha256=r9PN4S-6-nPUz6M6KEf-EbBVuozchulrb6fgvD7nXSM,12730
5
+ kitchenowl_python/types.py,sha256=Z4DtA0skTgeDbxywKStNIYN9ofQ4hdPOGTBmtx8dBWc,2109
6
+ kitchenowl_python-0.0.1.dist-info/LICENSE,sha256=o7hx9SZiGRYrq2gq2LbP7scrPXEAq0aHusuSrP2MuqA,1078
7
+ kitchenowl_python-0.0.1.dist-info/METADATA,sha256=pI_bDfsEtlZvfEajguzCt2ldBYq0J5pQEVoQm8f6MxA,2989
8
+ kitchenowl_python-0.0.1.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
9
+ kitchenowl_python-0.0.1.dist-info/top_level.txt,sha256=NBfVqNoXQONSyXaxqYQJeQYmCQhPEi2vaQTyFx2jJWY,18
10
+ kitchenowl_python-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.6.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ kitchenowl_python