Skip to content

Commit 6262bd9

Browse files
committed
Added docs
1 parent 0a5e177 commit 6262bd9

File tree

86 files changed

+3162
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+3162
-20
lines changed

docs/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ qthelp:
9191
@echo
9292
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
9393
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
94-
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/fastapi-rest-jsonapi.qhcp"
94+
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/jsonapi-utils.qhcp"
9595
@echo "To view the help file:"
96-
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/fastapi-rest-jsonapi.qhc"
96+
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/jsonapi-utils.qhc"
9797

9898
.PHONY: applehelp
9999
applehelp:
@@ -110,8 +110,8 @@ devhelp:
110110
@echo
111111
@echo "Build finished."
112112
@echo "To view the help file:"
113-
@echo "# mkdir -p $$HOME/.local/share/devhelp/fastapi-rest-jsonapi"
114-
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/fastapi-rest-jsonapi"
113+
@echo "# mkdir -p $$HOME/.local/share/devhelp/jsonapi-utils"
114+
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/jsonapi-utils"
115115
@echo "# devhelp"
116116

117117
.PHONY: epub

docs/api_filtering_example.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
Filtering API example
2+
======================
3+
4+
.. literalinclude:: ../examples/api_complex_filtering.py
5+
:language: python
6+
7+
8+
Check existing persons
9+
10+
Request:
11+
12+
.. literalinclude:: ./http_snippets/snippets/api_filtering__get_persons
13+
:language: HTTP
14+
15+
Response:
16+
17+
.. literalinclude:: ./http_snippets/snippets/api_filtering__get_persons_result
18+
:language: HTTP
19+
20+
21+
22+
Filter by word
23+
24+
.. code-block:: json
25+
26+
[
27+
{
28+
"name": "words",
29+
"op": "in",
30+
"val": "spam"
31+
}
32+
]
33+
34+
Request:
35+
36+
.. literalinclude:: ./http_snippets/snippets/api_filtering__get_persons__filter_word_in_array
37+
:language: HTTP
38+
39+
Response:
40+
41+
.. literalinclude:: ./http_snippets/snippets/api_filtering__get_persons__filter_word_in_array_result
42+
:language: HTTP
43+
44+
45+
Filter by words
46+
47+
.. code-block:: json
48+
49+
[
50+
{
51+
"name": "words",
52+
"op": "in",
53+
"val": ["bar", "eggs"]
54+
}
55+
]
56+
57+
Request:
58+
59+
.. literalinclude:: ./http_snippets/snippets/api_filtering__get_persons__filter_words_in_array
60+
:language: HTTP
61+
62+
Response:
63+
64+
.. literalinclude:: ./http_snippets/snippets/api_filtering__get_persons__filter_words_in_array_result
65+
:language: HTTP
66+
67+
68+
Filter by any word containing value
69+
70+
.. code-block:: json
71+
72+
[
73+
{
74+
"name": "words",
75+
"op": "ilike_in_str_array",
76+
"val": "green"
77+
}
78+
]
79+
80+
Request:
81+
82+
.. literalinclude:: ./http_snippets/snippets/api_filtering__get_persons__filter_word_contains_in_array
83+
:language: HTTP
84+
85+
Response:
86+
87+
.. literalinclude:: ./http_snippets/snippets/api_filtering__get_persons__filter_word_contains_in_array_result
88+
:language: HTTP
89+

docs/changelog.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Changelog
2+
*********
3+
4+
5+
**0.2.0**
6+
=========
7+
8+
Enhancements and bug fixes
9+
==========================
10+
11+
* Rename `from fastapi_rest_jsonapi import...` to `from fastapi_jsonapi import ...`
12+
* Add documentation
13+
14+
.. _`@znbiz`: https://github.com/znbiz

docs/conf.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
#
3-
# FastAPI-REST-JSONAPI documentation build configuration file
3+
# fastapi-jsonapi documentation build configuration file, created by
4+
# sphinx-quickstart on Fri Oct 21 14:33:15 2016.
45
#
56
# This file is execfile()d with the current directory set to its
67
# containing dir.
@@ -51,7 +52,7 @@
5152
master_doc = "index"
5253

5354
# General information about the project.
54-
project = "FastAPI-REST-JSONAPI"
55+
project = "FastAPI-JSONAPI"
5556
copyright = "2022, MTS AI"
5657
author = "MTS AI"
5758

@@ -60,9 +61,9 @@
6061
# built documents.
6162
#
6263
# The short X.Y version.
63-
version = "0.1"
64+
version = "1.1"
6465
# The full version, including alpha/beta/rc tags.
65-
release = "0.1"
66+
release = "1.1.0"
6667

6768
# The language for content autogenerated by Sphinx. Refer to documentation
6869
# for a list of supported languages.
@@ -129,10 +130,9 @@
129130
# documentation.
130131
#
131132
html_theme_options = {
132-
"github_user": "",
133-
"github_repo": "",
133+
"github_user": "mts-ai",
134+
"github_repo": "FastAPI-JSONAPI",
134135
"github_banner": True,
135-
"travis_button": True,
136136
"show_related": True,
137137
"page_width": "1080px",
138138
"fixed_sidebar": True,
@@ -145,7 +145,7 @@
145145
# The name for this set of Sphinx documents.
146146
# "<project> v<release> documentation" by default.
147147
#
148-
# html_title = 'FastAPI-REST-JSONAPI v0.1'
148+
# html_title = 'jsonapi-utils v0.1'
149149

150150
# A shorter title for the navigation bar. Default is the same as html_title.
151151
#
@@ -245,7 +245,7 @@
245245
# html_search_scorer = 'scorer.js'
246246

247247
# Output file base name for HTML help builder.
248-
htmlhelp_basename = "fastapi-rest-jsonapi"
248+
htmlhelp_basename = "fastapi-jsonapidoc"
249249

250250
# -- Options for LaTeX output ---------------------------------------------
251251

@@ -268,7 +268,7 @@
268268
# (source start file, target name, title,
269269
# author, documentclass [howto, manual, or own class]).
270270
latex_documents = [
271-
(master_doc, "fastapi-rest-jsonapi.tex", "fastapi-rest-jsonapi Documentation", "MTS AI", "manual"),
271+
(master_doc, "fastapi-jsonapi.tex", "fastapi-jsonapi Documentation", "mts ai", "manual"),
272272
]
273273

274274
# The name of an image file (relative to this directory) to place at the top of
@@ -308,7 +308,7 @@
308308

309309
# One entry per manual page. List of tuples
310310
# (source start file, name, description, authors, manual section).
311-
man_pages = [(master_doc, "fastapi-rest-jsonapi", "FastAPI-REST-JSONAPI Documentation", [author], 1)]
311+
man_pages = [(master_doc, "fastapi-jsonapi", "fastapi-jsonapi Documentation", [author], 1)]
312312

313313
# If true, show URL addresses after external links.
314314
#
@@ -323,10 +323,10 @@
323323
texinfo_documents = [
324324
(
325325
master_doc,
326-
"fastapi-rest-jsonapi",
327-
"FastAPI-REST-JSONAPI Documentation",
326+
"fastapi-jsonapi",
327+
"fastapi-jsonapi Documentation",
328328
author,
329-
"fastapi-rest-jsonapi",
329+
"fastapi-jsonapi",
330330
"One line description of project.",
331331
"Miscellaneous",
332332
),

docs/configuration.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. _configuration:
2+
3+
Configuration
4+
=============
5+
6+
You have access to 5 configuration keys:
7+
8+
* PAGE_SIZE: the number of items in a page (default is 30)
9+
* MAX_PAGE_SIZE: the maximum page size. If you specify a page size greater than this value you will receive a 400 Bad Request response.
10+
* MAX_INCLUDE_DEPTH: the maximum length of an include through schema relationships
11+
* ALLOW_DISABLE_PAGINATION: if you want to disallow to disable pagination you can set this configuration key to False
12+
* CATCH_EXCEPTIONS: if you want fastapi_jsonapi to catch all exceptions and return them as JsonApiException (default is True)

docs/data_layer.rst

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
.. _data_layer:
2+
3+
Data layer
4+
==========
5+
6+
.. currentmodule:: fastapi_jsonapi
7+
8+
| The data layer is a CRUD interface between resource manager and data. It is a very flexible system to use any ORM or data storage. You can even create a data layer that uses multiple ORMs and data storage to manage your own objects. The data layer implements a CRUD interface for objects and relationships. It also manages pagination, filtering and sorting.
9+
|
10+
| FastAPI-JSONAPI has a full-featured data layer that uses the popular ORM `SQLAlchemy <https://www.sqlalchemy.org/>`_.
11+
12+
.. note::
13+
14+
The default data layer used by a resource manager is the SQLAlchemy one. So if that's what you want to use, you don't have to specify the class of the data layer in the resource manager
15+
16+
To configure the data layer you have to set its required parameters in the resource manager.
17+
18+
Example:
19+
20+
.. code-block:: python
21+
22+
from http import HTTPStatus
23+
from typing import (
24+
List,
25+
Union,
26+
)
27+
28+
from fastapi import Depends
29+
from sqlalchemy import select, desc
30+
from sqlalchemy.ext.asyncio import AsyncSession
31+
from sqlalchemy.sql import Select
32+
from tortoise.exceptions import DoesNotExist
33+
34+
from examples.api_for_sqlalchemy.extensions.sqlalchemy import Connector
35+
from examples.api_for_sqlalchemy.helpers.factories.meta_base import FactoryUseMode
36+
from examples.api_for_sqlalchemy.helpers.factories.user import UserFactory, ErrorCreateUserObject
37+
from examples.api_for_sqlalchemy.helpers.updaters.exceptions import ObjectNotFound
38+
from examples.api_for_sqlalchemy.helpers.updaters.update_user import UpdateUser, ErrorUpdateUserObject
39+
from examples.api_for_sqlalchemy.models.pydantic import UserSchema, UserPatchSchema
40+
from examples.api_for_sqlalchemy.models.pydantic.user import UserInSchema
41+
from examples.api_for_sqlalchemy.models.sqlalchemy import User
42+
from fastapi_jsonapi import SqlalchemyEngine
43+
from fastapi_jsonapi.exceptions import (
44+
BadRequest,
45+
HTTPException,
46+
)
47+
from fastapi_jsonapi.querystring import QueryStringManager
48+
from fastapi_jsonapi.schema import JSONAPIResultListSchema
49+
50+
51+
class UserDetail:
52+
@classmethod
53+
async def get_user(cls, user_id, query_params: QueryStringManager, session: AsyncSession) -> User:
54+
"""
55+
Get user by id from ORM.
56+
57+
:param user_id: int
58+
:param query_params: QueryStringManager
59+
:return: User model.
60+
:raises HTTPException: if user not found.
61+
"""
62+
user: User
63+
try:
64+
user = (await session.execute(select(User).where(User.id == user_id))).scalar_one()
65+
except DoesNotExist:
66+
raise HTTPException(
67+
status_code=HTTPStatus.FORBIDDEN,
68+
detail="User with id {id} not found".format(id=user_id),
69+
)
70+
71+
return user
72+
73+
@classmethod
74+
async def get(cls, obj_id, query_params: QueryStringManager, session: AsyncSession = Depends(Connector.get_session)) -> UserSchema:
75+
user: User = await cls.get_user(user_id=obj_id, query_params=query_params, session=session)
76+
return UserSchema.from_orm(user)
77+
78+
@classmethod
79+
async def patch(cls, obj_id, data: UserPatchSchema, query_params: QueryStringManager, session: AsyncSession = Depends(Connector.get_session)) -> UserSchema:
80+
user_obj: User
81+
try:
82+
user_obj = await UpdateUser.update(
83+
obj_id,
84+
data.dict(exclude_unset=True),
85+
query_params.headers,
86+
session=session,
87+
)
88+
except ErrorUpdateUserObject as ex:
89+
raise BadRequest(ex.description, ex.field)
90+
except ObjectNotFound as ex:
91+
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail=ex.description)
92+
93+
user = UserSchema.from_orm(user_obj)
94+
return user
95+
96+
97+
class UserList:
98+
@classmethod
99+
async def get(cls, query_params: QueryStringManager, session: AsyncSession = Depends(Connector.get_session)) -> Union[Select, JSONAPIResultListSchema]:
100+
user_query = select(User)
101+
dl = SqlalchemyEngine(query=user_query, schema=UserSchema, model=User, session=session)
102+
count, users_db = await dl.get_collection(qs=query_params)
103+
total_pages = count // query_params.pagination.size + (count % query_params.pagination.size and 1)
104+
users: List[UserSchema] = [UserSchema.from_orm(i_user) for i_user in users_db]
105+
return JSONAPIResultListSchema(
106+
meta={"count": count, "totalPages": total_pages},
107+
data=[{"id": i_obj.id, "attributes": i_obj.dict(), "type": "user"} for i_obj in users],
108+
)
109+
110+
@classmethod
111+
async def post(cls, data: UserInSchema, query_params: QueryStringManager, session: AsyncSession = Depends(Connector.get_session)) -> UserSchema:
112+
try:
113+
user_obj = await UserFactory.create(
114+
data=data.dict(),
115+
mode=FactoryUseMode.production,
116+
header=query_params.headers,
117+
session=session,
118+
)
119+
except ErrorCreateUserObject as ex:
120+
raise BadRequest(ex.description, ex.field)
121+
122+
user = UserSchema.from_orm(user_obj)
123+
return user
124+

0 commit comments

Comments
 (0)