muffin-rest 10.0.0__tar.gz → 11.0.0__tar.gz

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 (40) hide show
  1. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/PKG-INFO +1 -1
  2. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/handler.py +12 -3
  3. muffin_rest-11.0.0/muffin_rest/marshmallow.py +20 -0
  4. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/peewee/handler.py +8 -12
  5. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/pyproject.toml +1 -1
  6. muffin_rest-10.0.0/muffin_rest/marshmallow.py +0 -26
  7. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/LICENSE +0 -0
  8. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/README.rst +0 -0
  9. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/__init__.py +0 -0
  10. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/api.py +0 -0
  11. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/errors.py +0 -0
  12. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/filters.py +0 -0
  13. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/limits.py +0 -0
  14. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/mongo/__init__.py +0 -0
  15. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/mongo/filters.py +0 -0
  16. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/mongo/schema.py +0 -0
  17. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/mongo/sorting.py +0 -0
  18. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/mongo/types.py +0 -0
  19. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/mongo/utils.py +0 -0
  20. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/openapi.py +0 -0
  21. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/options.py +0 -0
  22. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/peewee/__init__.py +0 -0
  23. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/peewee/filters.py +0 -0
  24. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/peewee/openapi.py +0 -0
  25. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/peewee/options.py +0 -0
  26. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/peewee/schemas.py +0 -0
  27. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/peewee/sorting.py +0 -0
  28. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/peewee/types.py +0 -0
  29. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/peewee/utils.py +0 -0
  30. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/py.typed +0 -0
  31. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/redoc.html +0 -0
  32. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/schemas.py +0 -0
  33. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/sorting.py +0 -0
  34. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/sqlalchemy/__init__.py +0 -0
  35. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/sqlalchemy/filters.py +0 -0
  36. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/sqlalchemy/sorting.py +0 -0
  37. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/sqlalchemy/types.py +0 -0
  38. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/swagger.html +0 -0
  39. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/types.py +0 -0
  40. {muffin_rest-10.0.0 → muffin_rest-11.0.0}/muffin_rest/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: muffin-rest
3
- Version: 10.0.0
3
+ Version: 11.0.0
4
4
  Summary: The package provides enhanced support for writing REST APIs with Muffin framework
5
5
  License: MIT
6
6
  Keywords: rest,api,muffin,asgi,asyncio,trio
@@ -8,6 +8,7 @@ from typing import (
8
8
  Generic,
9
9
  Iterable,
10
10
  Literal,
11
+ Mapping,
11
12
  Optional,
12
13
  Sequence,
13
14
  Union,
@@ -222,14 +223,22 @@ class RESTBase(Generic[TVResource], Handler, metaclass=RESTHandlerMeta):
222
223
  schema_options.setdefault("exclude", query.get("schema_exclude", ()))
223
224
  return self.meta.Schema(**schema_options)
224
225
 
226
+ async def load_data(self, request: Request):
227
+ """Load data from request and create/update a resource."""
228
+ try:
229
+ data = await request.data(raise_errors=True)
230
+ except (ValueError, TypeError) as err:
231
+ raise APIError.BAD_REQUEST(str(err)) from err
232
+
233
+ return data
234
+
225
235
  async def load(
226
236
  self, request: Request, resource: Optional[TVResource] = None, **schema_options
227
237
  ) -> TVData[TVResource]:
228
238
  """Load data from request and create/update a resource."""
229
239
  schema = self.get_schema(request, resource=resource, **schema_options)
230
- return cast(
231
- TVData[TVResource], await load_data(request, schema, partial=resource is not None)
232
- )
240
+ data = cast(Union[Mapping, list], await self.load_data(request))
241
+ return cast(TVData[TVResource], await load_data(data, schema, partial=resource is not None))
233
242
 
234
243
  @overload
235
244
  async def dump( # type: ignore[misc]
@@ -0,0 +1,20 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Optional, Union
4
+
5
+ from marshmallow import Schema, ValidationError
6
+
7
+ from muffin_rest.errors import APIError
8
+
9
+ if TYPE_CHECKING:
10
+ from collections.abc import Mapping
11
+
12
+
13
+ async def load_data(data: Union[Mapping, list], schema: Optional[Schema] = None, **params):
14
+ if schema is None:
15
+ return data
16
+
17
+ try:
18
+ return schema.load(data, many=isinstance(data, list), **params)
19
+ except ValidationError as err:
20
+ raise APIError.BAD_REQUEST("Bad request data", errors=err.messages) from err
@@ -43,15 +43,13 @@ class PWRESTBase(RESTBase[TVModel], PeeweeOpenAPIMixin):
43
43
  async def prepare_collection(
44
44
  self: PWRESTBase[TVAIOModel],
45
45
  _: Request,
46
- ) -> AIOModelSelect[TVAIOModel]:
47
- ...
46
+ ) -> AIOModelSelect[TVAIOModel]: ...
48
47
 
49
48
  @overload
50
49
  async def prepare_collection(
51
50
  self: PWRESTBase[pw.Model],
52
51
  _: Request,
53
- ) -> pw.ModelSelect:
54
- ...
52
+ ) -> pw.ModelSelect: ...
55
53
 
56
54
  # NOTE: there is not a default sorting for peewee (conflict with muffin-admin)
57
55
  async def prepare_collection(self, _: Request):
@@ -81,14 +79,12 @@ class PWRESTBase(RESTBase[TVModel], PeeweeOpenAPIMixin):
81
79
  @overload
82
80
  async def paginate(
83
81
  self: PWRESTBase[TVAIOModel], _: Request, *, limit: int = 0, offset: int = 0
84
- ) -> tuple[AIOModelSelect[TVAIOModel], int | None]:
85
- ...
82
+ ) -> tuple[AIOModelSelect[TVAIOModel], int | None]: ...
86
83
 
87
84
  @overload
88
85
  async def paginate(
89
86
  self: PWRESTBase[pw.Model], _: Request, *, limit: int = 0, offset: int = 0
90
- ) -> tuple[pw.ModelSelect, int | None]:
91
- ...
87
+ ) -> tuple[pw.ModelSelect, int | None]: ...
92
88
 
93
89
  async def paginate(self, _: Request, *, limit: int = 0, offset: int = 0):
94
90
  """Paginate the collection."""
@@ -136,9 +132,7 @@ class PWRESTBase(RESTBase[TVModel], PeeweeOpenAPIMixin):
136
132
  return
137
133
 
138
134
  model_pk = cast(pw.Field, meta.model_pk)
139
- resources = await meta.manager.fetchall(
140
- self.collection.where(model_pk << data),
141
- )
135
+ resources = await meta.manager.fetchall(self.collection.where(model_pk << data)) # type: ignore[]
142
136
 
143
137
  if not resources:
144
138
  raise APIError.NOT_FOUND()
@@ -151,7 +145,9 @@ class PWRESTBase(RESTBase[TVModel], PeeweeOpenAPIMixin):
151
145
  for res in resources:
152
146
  await meta.manager.delete_instance(res, recursive=meta.delete_recursive)
153
147
 
154
- async def delete(self, request: Request, resource: Optional[TVModel] = None):
148
+ return resource.get_id() if resource else [r.get_id() for r in resources]
149
+
150
+ async def delete(self, request: Request, resource: Optional[TVModel] = None): # type: ignore[override]
155
151
  return await self.remove(request, resource)
156
152
 
157
153
  def get_schema(
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "muffin-rest"
3
- version = "10.0.0"
3
+ version = "11.0.0"
4
4
  description = "The package provides enhanced support for writing REST APIs with Muffin framework"
5
5
  readme = "README.rst"
6
6
  homepage = "https://github.com/klen/muffin-rest"
@@ -1,26 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from collections.abc import Mapping
4
- from typing import TYPE_CHECKING, Optional, Union, cast
5
-
6
- from marshmallow import Schema, ValidationError
7
-
8
- from muffin_rest.errors import APIError
9
-
10
- if TYPE_CHECKING:
11
- from asgi_tools import Request
12
-
13
-
14
- async def load_data(request: Request, schema: Optional[Schema] = None, **params):
15
- try:
16
- data = await request.data(raise_errors=True)
17
- except (ValueError, TypeError) as err:
18
- raise APIError.BAD_REQUEST(str(err)) from err
19
-
20
- if schema is None:
21
- return data
22
-
23
- try:
24
- return schema.load(cast(Union[Mapping, list], data), many=isinstance(data, list), **params)
25
- except ValidationError as err:
26
- raise APIError.BAD_REQUEST("Bad request data", errors=err.messages) from err
File without changes
File without changes