nene2-python 1.2.0__py3-none-any.whl → 1.3.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.
nene2/http/pagination.py CHANGED
@@ -3,10 +3,11 @@
3
3
  Equivalent to PHP Nene2\\Http\\PaginationQueryParser, PaginationQuery, PaginationResponse.
4
4
  """
5
5
 
6
+ import dataclasses
6
7
  from dataclasses import dataclass, field
7
- from typing import Any
8
+ from typing import Annotated, Any
8
9
 
9
- from fastapi import Request
10
+ from fastapi import Query, Request
10
11
 
11
12
  from nene2.validation.exceptions import ValidationError, ValidationException
12
13
 
@@ -20,7 +21,33 @@ class PaginationQuery:
20
21
 
21
22
 
22
23
  class PaginationQueryParser:
23
- """Parses and validates pagination query parameters from a FastAPI Request."""
24
+ """Parses and validates pagination query parameters.
25
+
26
+ Two usage patterns:
27
+
28
+ **FastAPI Depends (recommended)**::
29
+
30
+ from typing import Annotated
31
+ from fastapi import Depends
32
+
33
+
34
+ def list_items(pagination: Annotated[PaginationQueryParser, Depends()]) -> JSONResponse:
35
+ result = use_case.execute(pagination.limit, pagination.offset)
36
+
37
+ **Manual parse from Request (legacy)**::
38
+
39
+ def list_items(request: Request) -> JSONResponse:
40
+ pagination = PaginationQueryParser.parse(request)
41
+ result = use_case.execute(pagination.limit, pagination.offset)
42
+ """
43
+
44
+ def __init__(
45
+ self,
46
+ limit: Annotated[int, Query(ge=1, le=100, description="Items per page (1–100)")] = 20,
47
+ offset: Annotated[int, Query(ge=0, description="Items to skip")] = 0,
48
+ ) -> None:
49
+ self.limit = limit
50
+ self.offset = offset
24
51
 
25
52
  @staticmethod
26
53
  def parse(
@@ -83,8 +110,18 @@ class PaginationResponse:
83
110
  total: int | None = field(default=None)
84
111
 
85
112
  def to_dict(self) -> dict[str, Any]:
113
+ """Return a JSON-serializable dict.
114
+
115
+ Items that are dataclass instances are converted via ``dataclasses.asdict()``
116
+ so that ``JSONResponse(result.to_dict())`` works without extra steps.
117
+ """
86
118
  data: dict[str, Any] = {
87
- "items": self.items,
119
+ "items": [
120
+ dataclasses.asdict(item)
121
+ if dataclasses.is_dataclass(item) and not isinstance(item, type)
122
+ else item
123
+ for item in self.items
124
+ ],
88
125
  "limit": self.limit,
89
126
  "offset": self.offset,
90
127
  }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nene2-python
3
- Version: 1.2.0
3
+ Version: 1.3.0
4
4
  Summary: NENE2 Python — minimal API framework following NENE2's design philosophy
5
5
  Project-URL: Homepage, https://github.com/hideyukiMORI/nene2-python
6
6
  Project-URL: Repository, https://github.com/hideyukiMORI/nene2-python
@@ -16,7 +16,7 @@ nene2/database/sqlalchemy_executor.py,sha256=-s-NmRyDhEdQPYeYJvcf2BJWLpqDqcbH1Qh
16
16
  nene2/database/utils.py,sha256=f5oMt6cMw1IFSPFpgvJVAlD_rNLnQdj55dvWO5jJPbA,944
17
17
  nene2/http/__init__.py,sha256=zRF4mrCD6582VHqtP6BhIiwFQQBrXzZ9SDFT2QaeOv8,440
18
18
  nene2/http/health.py,sha256=zYAeXkEWWCITFmza9u8yqcSkD2sJx7AGm650HhvU9Fs,496
19
- nene2/http/pagination.py,sha256=BmKyTcNBplQtGADYjqlSvWSGEih9bb3DG-rT_CyvOg8,2663
19
+ nene2/http/pagination.py,sha256=zpnLrbsUsD3csnUPfuj2dOxjkhcK1ZhGLiLUBdlJlJI,3930
20
20
  nene2/http/problem_details.py,sha256=-a4E6OV0caigcETf9YBupPEhjlYvqLEwZdCux1ujhPI,874
21
21
  nene2/log/__init__.py,sha256=umsIJ2QH0d-CpnqEQCR0oWCjpe7wVoGYAYP35f1Nmbc,113
22
22
  nene2/log/setup.py,sha256=JZJ3EnUjN9XcC1BB03gbgk4R4cpOYgu7it487pLCIb0,1407
@@ -35,7 +35,7 @@ nene2/use_case/__init__.py,sha256=xwdFxHsjfPORSw6sq6dUN8JkEX6X2CHmO3stdpjUNOU,19
35
35
  nene2/use_case/protocols.py,sha256=F-R99vLad7XX6fHncTVBo5XLsKFsVceWqnE4QlSn9kg,710
36
36
  nene2/validation/__init__.py,sha256=Nj4dMx4Re3l57B0raFSUPQWeHHFMJbBQaQcuvDSbwiE,205
37
37
  nene2/validation/exceptions.py,sha256=qFaK-LAk1sNs90TV0mrQLvwNlfUruSohRqpqOaaG-lg,1036
38
- nene2_python-1.2.0.dist-info/METADATA,sha256=PuRRMIp22IPMyMc7kypeUyPpU87GsHDhyCeoNm24lRw,7263
39
- nene2_python-1.2.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
40
- nene2_python-1.2.0.dist-info/licenses/LICENSE,sha256=fzFjofg1algLQJI6DBIhxBDQTpry6HNLUcjrXMPTwIk,1069
41
- nene2_python-1.2.0.dist-info/RECORD,,
38
+ nene2_python-1.3.0.dist-info/METADATA,sha256=6ESJNyPSPcvRl1YgPuQ8m8-uPPaxtt9cxUP9jOIPOok,7263
39
+ nene2_python-1.3.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
40
+ nene2_python-1.3.0.dist-info/licenses/LICENSE,sha256=fzFjofg1algLQJI6DBIhxBDQTpry6HNLUcjrXMPTwIk,1069
41
+ nene2_python-1.3.0.dist-info/RECORD,,