Skip to content
This repository was archived by the owner on Mar 7, 2023. It is now read-only.

Commit 30991aa

Browse files
committed
Initial release.
0 parents  commit 30991aa

File tree

15 files changed

+2960
-0
lines changed

15 files changed

+2960
-0
lines changed

.flake8

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
filename = *.py,src/
3+
max-line-length = 88
4+
extend-ignore = D105, D107, D401, E203, E402

.github/workflows/tests.yml

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
name: tests
3+
4+
on:
5+
push:
6+
paths-ignore:
7+
- "**.md"
8+
- "LICENSE"
9+
- ".gitignore"
10+
- ".pre-commit-config.yaml"
11+
12+
env:
13+
CACHE_DIR: /tmp/.workflow_cache
14+
POETRY_CACHE_DIR: /tmp/.workflow_cache/.pip_packages
15+
POETRY_VIRTUALENVS_PATH: /tmp/.workflow_cache/.venvs
16+
POETRY_HOME: /tmp/.workflow_cache/.poetry
17+
PIP_CACHE_DIR: /tmp/.workflow_cache/.pip_packages
18+
MYPY_CACHE_DIR: /tmp/.workflow_cache/.mypy
19+
20+
jobs:
21+
tests:
22+
runs-on: ${{ matrix.os }}
23+
strategy:
24+
matrix:
25+
os: ["ubuntu-latest"]
26+
python-version: ["3.x"]
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/checkout@v3
30+
31+
- name: Set up Python ${{ matrix.python-version }}
32+
uses: actions/setup-python@v4
33+
with:
34+
python-version: ${{ matrix.python-version }}
35+
36+
- name: Cache dependencies
37+
uses: actions/cache@v3
38+
id: cache
39+
with:
40+
path: ${{ env.CACHE_DIR }}
41+
key: tests-${{ matrix.os }}-${{ matrix.python-version }}--${{ hashFiles('**/poetry.lock') }}
42+
43+
- name: Install dependencies
44+
run: |
45+
curl -sSL https://install.python-poetry.org | python -
46+
$POETRY_HOME/bin/poetry install -n
47+
if: steps.cache.outputs.cache-hit != 'true'
48+
49+
- name: Python code style
50+
run: $POETRY_HOME/bin/poetry run black . --check --diff
51+
if: ${{ always() }}
52+
53+
- name: Python code quality
54+
run: $POETRY_HOME/bin/poetry run flake8 --docstring-convention google
55+
if: ${{ always() }}
56+
57+
- name: Python code typing
58+
run: $POETRY_HOME/bin/poetry run mypy --strict --install-types --non-interactive .
59+
if: ${{ always() }}
60+
61+
- name: Python code complexity
62+
run: $POETRY_HOME/bin/poetry run radon cc -n C fastapi_paginator 1>&2
63+
if: ${{ always() }}
64+
65+
- name: Python code maintainability
66+
run: $POETRY_HOME/bin/poetry run radon mi -n B fastapi_paginator 1>&2
67+
if: ${{ always() }}
68+
69+
- name: Python code security
70+
run: $POETRY_HOME/bin/poetry run bandit fastapi_paginator -rs B404,B603
71+
if: ${{ always() }}
72+
73+
- name: YAML code style
74+
run: $POETRY_HOME/bin/poetry run yamllint -s .
75+
if: ${{ always() }}
76+
77+
- name: Test
78+
run: $POETRY_HOME/bin/poetry run pytest --junitxml=test-results.xml --cov-report xml
79+
if: ${{ always() }}
80+
81+
- name: Collect coverage report
82+
uses: codecov/codecov-action@v3
83+
84+
publish:
85+
runs-on: ${{ matrix.os }}
86+
strategy:
87+
matrix:
88+
os: ["ubuntu-latest"]
89+
python-version: ["3.x"]
90+
if: ${{ github.repository == 'JGoutin/fastapi_paginator' && github.ref_type == 'tag' }}
91+
needs: [tests]
92+
environment: PyPI
93+
permissions:
94+
contents: write
95+
steps:
96+
- name: Checkout repository
97+
uses: actions/checkout@v3
98+
99+
- name: Set up Python ${{ matrix.python-version }}
100+
uses: actions/setup-python@v4
101+
with:
102+
python-version: ${{ matrix.python-version }}
103+
104+
- name: Cache dependencies
105+
uses: actions/cache@v3
106+
id: cache
107+
with:
108+
path: ${{ env.CACHE_DIR }}
109+
key: tests-${{ matrix.os }}-${{ matrix.python-version }}--${{ hashFiles('**/poetry.lock') }}
110+
111+
- name: Build packages
112+
run: $POETRY_HOME/bin/poetry version $(echo -e "${{ github.ref_name }}" | tr -d 'v')
113+
114+
- name: Publish packages on PyPI
115+
run: $POETRY_HOME/bin/poetry publish --build
116+
env:
117+
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_TOKEN }}
118+
119+
- name: Publish release on GitHub
120+
run: |
121+
go install github.com/tcnksm/ghr@latest
122+
~/go/bin/ghr -generatenotes $PRERELEASE -c ${{ github.sha }} ${{ github.ref_name }}
123+
env:
124+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
125+
PRERELEASE: ${{ contains(github.ref_name, '-') && '-prerelease' || '' }}

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Python compiled files
2+
__pycache__/
3+
*.py[cd]
4+
5+
# OS generated files
6+
.DS_Store
7+
.DS_Store?
8+
._*
9+
.Spotlight-V100
10+
.Trashes
11+
ehthumbs.db
12+
[Dd]esktop.ini
13+
Thumbs.db
14+
*~
15+
*.bak
16+
17+
# IDE generated files
18+
.project
19+
.pydevproject
20+
.spyproject
21+
.spyderproject
22+
.settings/
23+
.idea/
24+
.vscode/
25+
26+
# Tests generated files
27+
.cache/
28+
.coverage
29+
.coverage.*
30+
.mypy_cache/
31+
.pytest_cache/
32+
33+
# Build
34+
dist/

.pre-commit-config.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
repos:
3+
- repo: local
4+
hooks:
5+
- id: black
6+
name: Black (Formatting)
7+
entry: poetry run black . --preview
8+
language: system
9+
pass_filenames: false
10+
- id: mypy
11+
name: Mypy (Typing)
12+
entry: poetry run mypy --strict .
13+
language: system
14+
pass_filenames: false
15+
- id: flake8
16+
name: Flake8 (Quality)
17+
entry: poetry run flake8 --docstring-convention google
18+
language: system
19+
pass_filenames: false
20+
- id: radon_cc
21+
name: Radon (Cyclomatic complexity)
22+
entry: poetry run radon cc -n C fastapi_paginator
23+
language: system
24+
pass_filenames: false
25+
verbose: true
26+
- id: radon_mi
27+
name: Radon (Maintainability index)
28+
entry: poetry run radon mi -n B fastapi_paginator
29+
language: system
30+
pass_filenames: false
31+
verbose: true
32+
- id: bandit
33+
name: Bandit (Security)
34+
entry: poetry run bandit fastapi_paginator -qrs B404,B603
35+
language: system
36+
pass_filenames: false

.yamllint.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
extends: default
3+
rules:
4+
line-length: disable
5+
truthy: disable

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Copyright 2022 Accelize
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
7+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
9+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
![Tests](https://github.com/JGoutin/fastapi_paginator/workflows/tests/badge.svg)
2+
[![codecov](https://codecov.io/gh/JGoutin/fastapi_paginator/branch/main/graph/badge.svg?token=QR5nYeX11F)](https://codecov.io/gh/JGoutin/fastapi_paginator)
3+
[![PyPI](https://img.shields.io/pypi/v/fastapi_paginator.svg)](https://pypi.org/project/fastapi_paginator)
4+
5+
# FastAPI Paginator
6+
7+
Easy to use paginator for [FastAPI](https://fastapi.tiangolo.com/)
8+
9+
Currently, supports only [encode/databases](https://github.com/encode/databases) as
10+
database library and tested with SQlite and PostgreSQL.
11+
12+
## Features
13+
14+
* Simple FastAPI integration.
15+
* Navigation with page numbers (With total page count returned on first page).
16+
* Navigation from a specific row (since).
17+
* Ordering result (On multiple columns).
18+
* Filtering result using various SQL functions.
19+
20+
## Installation
21+
22+
FastAPI Paginator is available on PyPI, so it can be installed like any other Python
23+
package.
24+
25+
Example with Pip:
26+
```bash
27+
pip install fastapi_paginator
28+
```
29+
30+
## Usage
31+
32+
### Routes creations in FastAPI
33+
34+
To use it, you only need to create a `fastapi_paginator.Paginator` instance linked to
35+
the database and routes
36+
using `fastapi_paginator.PageParameters` and `fastapi_paginator.Page`.
37+
38+
```python
39+
import databases
40+
import fastapi
41+
import pydantic
42+
import sqlalchemy
43+
import fastapi_paginator
44+
45+
# Already existing database, FastAPI application, "item" table, and "item" model
46+
database = databases.Database(f"sqlite:///local.db}")
47+
app = fastapi.FastAPI()
48+
49+
table = sqlalchemy.Table(
50+
"table",
51+
sqlalchemy.MetaData(),
52+
Column("id", sqlalchemy.Integer, primary_key=True),
53+
Column("name", sqlalchemy.String, nullable=False),
54+
)
55+
56+
class Item(pydantic.BaseModel):
57+
"""Item in database."""
58+
59+
class Config:
60+
"""Config."""
61+
62+
orm_mode = True # Required
63+
64+
id: int
65+
name: str
66+
67+
68+
# Create a paginator for the database (Required only once per database)
69+
paginator = fastapi_paginator.Paginator(database)
70+
71+
# Create a paginated route
72+
@app.get("/list")
73+
async def list_data(
74+
page_parameters: fastapi_paginator.PageParameters = Depends(),
75+
) -> fastapi_paginator.Page[Item]:
76+
"""List data with pagination."""
77+
return await paginator(table.select(), Item, page_parameters)
78+
```
79+
80+
### Paginated routes usage from clients
81+
82+
83+
#### Request
84+
Paginator parameters are passed as query parameters, for instance:
85+
86+
```http request
87+
GET /list?order_by=id&page=2
88+
```
89+
90+
##### Query parameters
91+
92+
###### page
93+
The page to return.
94+
95+
When page is not specified or equal to `1`, the request returns `total_page` that is
96+
the maximum number of pages.
97+
98+
*Cannot be used with `since`.*
99+
100+
###### since
101+
102+
The item from where starting to return the result.
103+
104+
When navigating between successive pages, the `next_since` returned value should be used
105+
as `since` for the subsequent requests.
106+
107+
*Cannot be used with `page`*.
108+
109+
*Cannot be used with `order_by` if not ordering on the field used by `since`*.
110+
111+
###### order_by
112+
Sort the resulting items by the specified field name.
113+
114+
Order is descending if `-` is added before the field name, else order is ascending.
115+
116+
This query parameter can be specified multiple time to sort by multiple columns.
117+
118+
**Example:**
119+
"Ordering descending by the `created_at` column: `order_by=-created_at`
120+
121+
###### filter_by
122+
123+
Filter the resulting items.
124+
125+
The query must be in the form `field_name operator argument`, with:
126+
* `field_name`: the name on the field on where apply the filter.
127+
* `operator`: one operator from the list bellow.
128+
* `argument`: is the operator argument, it can be one or more value separated by `,`
129+
(Depending on the operator), valid values must be a primitive JSON type like
130+
numbers, double-quoted strings, `true`, `false` and `null`.
131+
132+
This query parameter can be specified multiple time to filter on more criteria
133+
(Using AND logical conjunction).
134+
135+
Available operators:
136+
* `=`: Equal to a single value (Also supports `null`, `true` and `false`)
137+
* `<`: Lower than a single value.
138+
* `<=`: Lower or equal than a single value.
139+
* `>`: Greater than a single value.
140+
* `>=`: Greater or equal than a single value.
141+
* `between`: Between a pair of values (`value_1` <= `field_value` <= `value_2`).
142+
* `in`: Present in a list of one or more values.
143+
* `like`: Like a single value (`%` can be used as wildcard for zero to multiple
144+
characters, `_` as wildcard for a single character, `/` can be used as escape
145+
character for `%` and `_`).
146+
* `ilike`: Same as `like`, but case insensitive.
147+
* `startswith`: String representation starts with a single value.
148+
* `endswith`: String representation ends with a single value.
149+
* `contains`: String representation contains a single value.
150+
151+
Any operator can be negated by adding `!` in front of it.
152+
153+
*Warning*: Depending on your HTTP client, the query parameter value may require to be
154+
URL encoded.
155+
156+
**Example:**
157+
Returning only data with a `name` field that does not start with
158+
`Product`: `filter_by=name%20%21like%20%22Product%25%22`
159+
(With URL encoded value of: `name !like "Product%"`')
160+
161+
##### Response
162+
163+
The response is a JSON dictionnary with the following fields:
164+
* `items`: The list returned items.
165+
* `next_since`: Next value to use with `since` query parameter.
166+
* `next_page`: Next value to use with `page` query parameter.
167+
* `total_pages`: Total pages, only computed and returned when on page 1
168+
169+
### Using alternates JSON libraries
170+
171+
It is possible to override the `json.loads` function used in all paginator as follows
172+
(Example with [orjson](https://github.com/ijl/orjson)):
173+
174+
```python
175+
import orjson
176+
import fastapi_paginator
177+
178+
179+
fastapi_paginator.Paginator.json_loads = orjson.loads
180+
```

0 commit comments

Comments
 (0)