Skip to content

Commit

Permalink
WIP - Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Jul 13, 2018
1 parent 5d1e041 commit da7567f
Show file tree
Hide file tree
Showing 16 changed files with 625 additions and 18 deletions.
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
SPHINXPROJ = flask-rest-api
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
35 changes: 35 additions & 0 deletions docs/api_reference.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.. _api:

*************
API Reference
*************

.. module:: flask_rest_api

Api
===


.. autoclass:: flask_rest_api.Api
:members:

Blueprint
=========

.. autoclass:: flask_rest_api.Blueprint
:members:

Pagination
==========

.. autoclass:: flask_rest_api.Page
:members:
.. autofunction:: flask_rest_api.set_item_count

ETag
====

.. autofunction:: flask_rest_api.etag.is_etag_enabled
.. autofunction:: flask_rest_api.etag.is_etag_enabled_for_request
.. autofunction:: flask_rest_api.etag.check_etag
.. autofunction:: flask_rest_api.etag.set_etag
1 change: 1 addition & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. include:: ../AUTHORS.rst
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. _changelog:

.. include:: ../CHANGELOG.rst
104 changes: 104 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))


# -- Project information -----------------------------------------------------

import flask_rest_api

project = 'flask-rest-api'
copyright = '2018, Nobatek'

version = release = flask_rest_api.__version__


# -- General configuration ---------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.intersphinx',
'sphinx.ext.viewcode',
]

intersphinx_mapping = {
'python': ('http://python.readthedocs.io/en/latest/', None),
'marshmallow': ('http://marshmallow.readthedocs.io/en/latest/', None),
'webargs': ('http://webargs.readthedocs.io/en/latest/', None),
'flask': ('http://flask.readthedocs.io/en/latest/', None),
}

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The master toctree document.
master_doc = 'index'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path .
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# The default sidebars (for documents that don't match any pattern) are
# defined by theme itself. Builtin themes are using these templates by
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
# 'searchbox.html']``.
#
# html_sidebars = {}
54 changes: 54 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
flask-rest-api: build a REST API on Flask using Marshmallow
===========================================================

Release v\ |version|. (:ref:`Changelog <changelog>`)

**flask-rest-api** is a framework library for creating REST APIs.

It uses Flask as a webserver, and marshmallow to serialize and deserialize data.
It relies extensively on the marshmallow ecosystem, using webargs to get arguments
from requests, and apispec to generate an OpenAPI specification file as
automatically as possible.


Install
=======

flask-rest-api requires Python >= 3.5.

.. code-block:: bash
$ pip install flask-rest-api
Guide
=====

.. toctree::
:maxdepth: 1

quickstart
parameters
response
etag
pagination
openapi


API Reference
=============

.. toctree::
:maxdepth: 2

api_reference

Project Info
============

.. toctree::
:maxdepth: 1

changelog
license
authors
4 changes: 4 additions & 0 deletions docs/license.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
License
=======

.. include:: ../LICENSE
36 changes: 36 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build
set SPHINXPROJ=flask-rest-api

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%

:end
popd
114 changes: 114 additions & 0 deletions docs/pagination.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
.. _pagination:
.. module:: flask_rest_api

Pagination
==========

When returning a list of objects, it is generally good practice to paginate the
resource. This is when :meth:`Blueprint.paginate <Blueprint.paginate>` steps in.

Pagination is more or less transparent to view function depending on the source
of the data that is returned.


Pagination in the View Function
-------------------------------

In this mode, :meth:`Blueprint.paginate <Blueprint.paginate>` injects the
pagination parameters ``first_item`` and ``last_item`` as kwargs into the view
function.

It is the responsability of the view function to return only selected elements.

The view function must also specify the total number of elements using
:meth:`set_item_count <pagination.set_item_count>`.

.. code-block:: python
:emphasize-lines: 7,8,9,10
from flask_rest_api import set_item_count
@blp.route('/')
class Pets(MethodView):
@blp.response(PetSchema(many=True))
@blp.paginate()
def get(self, first_item, last_item):
set_item_count(Pet.size)
return Pet.get_elements(first_item=first_item, last_item=last_item)
Post-Pagination
---------------

This is the mode to use when the data is returned as a lazy database cursor.
The view function does not need to know the pagination parameters. It just
returns the cursor.

This mode is also used if the view function returns the complete ``list`` at no
extra cost and there is no interest in specifying the pagination parameters to
avoid fetching unneeded data. For instance, if the whole list is in already
memory.

This mode makes the view look nicer because everything happens in the decorator.
Or so it looks like.

In this case, :meth:`Blueprint.paginate <Blueprint.paginate>` must be passed a
cursor pager to take care of the pagination. `flask-rest-api` provides a pager
for `list`-like objects: :class:`Page <pagination.Page>`. When dealing with a
lazy database cursor, a custom cursor pager can be defined using a cursor
wrapper.


.. code-block:: python
:emphasize-lines: 18
from flask_rest_api import Page
class CursorWrapper():
def __init__(self, obj):
self.obj = obj
def __getitem__(self, key):
return self.obj[key]
def __len__(self):
return self.obj.count()
class CursorPage(Page):
_wrapper_class = CursorWrapper
@blp.route('/')
class Pets(MethodView):
@blp.response(PetSchema(many=True))
@blp.paginate(CursorPage)
def get(self):
return Pet.get()
The custom wrapper defined in the example above works for SQLAlchemy or PyMongo
cursors.


Pagination Parameters
---------------------

Once a view function is decorated with
:meth:`Blueprint.paginate <Blueprint.paginate>`, the client can request a
specific range of data by passing query arguments:


``GET /pets/?page=2&page_size=10``


The view function gets default values for the pagination parameters, as well as
a maximum value for ``page_size``.

Those default values are defined globally as

.. code-block:: python
DEFAULT_PAGINATION_PARAMETERS = {
'page': 1, 'page_size': 10, 'max_page_size': 100}
They can be modified globally by mutating ``DEFAULT_PAGINATION_PARAMETERS``, or
overwritten in a specific view function by passing them as keyword arguments to
:meth:`Blueprint.paginate <Blueprint.paginate>`.

0 comments on commit da7567f

Please sign in to comment.