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 5900f32
Show file tree
Hide file tree
Showing 18 changed files with 818 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 = {}
188 changes: 188 additions & 0 deletions docs/etag.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
.. _etag:
.. module:: flask_rest_api

ETag
====

ETag is a web cache validation mechanism. It allows an API client to make
conditional requests, such as

- GET a resource unless it is the same as the version in cache.
- PUT/PATCH/DELETE a resource unless the version in cache is outdated.

The first case is mostly useful to limit the bandwidth usage, the latter
addresses the case where two clients update a resource at the same time (known
as the "*lost update problem*").

The ETag featured is enabled with the `ETAG_ENABLED` application parameter. It
can be disabled function-wise by passing `disable_etag=False` to the
:meth:`Blueprint.response <Blueprint.response>` decorator.

`flask-rest-api` provides helpers to compute ETag, but ultimately, only the
developer knows what data is relevant to use as ETag source, so there can be
manual work involved.


ETag Computed with API Response Data
------------------------------------

The simplest case is when the ETag is computed using returned data, using the
:class:`Schema <marshmallow.Schema>` that serializes the data.

In this case, almost eveything is automatic. Only the call to
:meth:`check_etag <etag.check_etag>` is manual.

The :class:`Schema <marshmallow.Schema>` must be provided explicitly, even
though it is the same as the response schema.

.. code-block:: python
:emphasize-lines: 27,35
from flask_rest_api import check_etag
@blp.route('/')
class Pet(MethodView):
@blp.response(PetSchema(many=True))
def get(self):
return Pet.get()
@blp.arguments(PetSchema)
@blp.response(PetSchema)
def post(self, new_data):
return Pet.create(**new_data)
@blp.route('/<pet_id>')
class PetById(MethodView):
@blp.response(PetSchema)
def get(self, pet_id):
return Pet.get_by_id(pet_id)
@blp.arguments(PetSchema)
@blp.response(PetSchema)
def put(self, update_data, pet_id):
pet = Pet.get_by_id(pet_id)
# Check ETag is a manual action and schema must be provided
check_etag(pet, PetSchema)
pet.update(update_data)
return pet
@blp.response(code=204)
def delete(self, pet_id):
pet = Pet.get_by_id(pet_id)
# Check ETag is a manual action and schema must be provided
check_etag(pet, PetSchema)
Pet.delete(pet_id)
ETag Computed with API Response Data Using Another Schema
---------------------------------------------------------

Sometimes, it is not possible to use the data returned by the view function as
ETag data because it contains extra information that is irrelevant, like
HATEOAS information, for instance.

In this case, a specific ETag schema can be provided as ``etag_schema`` keyword
argument to :meth:`Blueprint.response <Blueprint.response>`. Then, it does not
need to be passed to :meth:`check_etag <etag.check_etag>`.

.. code-block:: python
:emphasize-lines: 7,12,19,24,28,32,36
from flask_rest_api import check_etag
@blp.route('/')
class Pet(MethodView):
@blp.response(
PetSchema(many=True), etag_schema=PetEtagSchema(many=True))
def get(self):
return Pet.get()
@blp.arguments(PetSchema)
@blp.response(PetSchema, etag_schema=PetEtagSchema)
def post(self, new_pet):
return Pet.create(**new_data)
@blp.route('/<int:pet_id>')
class PetById(MethodView):
@blp.response(PetSchema, etag_schema=PetEtagSchema)
def get(self, pet_id):
return Pet.get_by_id(pet_id)
@blp.arguments(PetSchema)
@blp.response(PetSchema, etag_schema=PetEtagSchema)
def put(self, new_pet, pet_id):
pet = Pet.get_by_id(pet_id)
# Check ETag is a manual action and schema must be provided
check_etag(pet)
pet.update(update_data)
return pet
@blp.response(code=204, etag_schema=PetEtagSchema)
def delete(self, pet_id):
pet = self._get_pet(pet_id)
# Check ETag is a manual action, ETag schema is used
check_etag(pet)
Pet.delete(pet_id)
ETag Computed on Arbitrary Data
-------------------------------

The ETag can also be computed from arbitrary data by calling
:meth:`set_etag <etag.set_etag>` manually.

The example below illustrates this with no ETag schema, but it is also possible
to pass an ETag schema to :meth:`set_etag <etag.set_etag>` and
:meth:`check_etag <etag.check_etag>` or equivalently to
:meth:`Blueprint.response <Blueprint.response>`.

.. code-block:: python
:emphasize-lines: 10,17,26,34,37,44
from flask_rest_api import check_etag, set_etag
@blp.route('/')
class Pet(MethodView):
@blp.response(PetSchema(many=True))
def get(self):
pets = Pet.get()
# Compute ETag using arbitrary data
set_etag([pet.update_time for pet in pets])
return pets
@blp.arguments(PetSchema)
@blp.response(PetSchema)
def post(self, new_data):
# Compute ETag using arbitrary data
set_etag(new_data['update_time'])
return Pet.create(**new_data)
@blp.route('/<pet_id>')
class PetById(MethodView):
@blp.response(PetSchema)
def get(self, pet_id):
# Compute ETag using arbitrary data
set_etag(new_data['update_time'])
return Pet.get_by_id(pet_id)
@blp.arguments(PetSchema)
@blp.response(PetSchema)
def put(self, update_data, pet_id):
pet = Pet.get_by_id(pet_id)
# Check ETag is a manual action
check_etag(pet, ['update_time'])
pet.update(update_data)
# Compute ETag using arbitrary data
set_etag(new_data['update_time'])
return pet
@blp.response(code=204)
def delete(self, pet_id):
pet = Pet.get_by_id(pet_id)
# Check ETag is a manual action
check_etag(pet, ['update_time'])
Pet.delete(pet_id)

0 comments on commit 5900f32

Please sign in to comment.