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 67318e9
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 0 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)
32 changes: 32 additions & 0 deletions docs/api_reference.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.. _api:

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

.. module:: flask_rest_api

Api
===


.. autoclass:: flask_rest_api.Api
:members:

Blueprint
=========

.. autoclass:: flask_rest_api.Blueprint
:members:

ETag
====

.. automodule:: flask_rest_api.etag
:members:

Pagination
==========

.. automodule:: flask_rest_api.pagination
:members:
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
103 changes: 103 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# 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),
'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: 2

quickstart
parameter
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
121 changes: 121 additions & 0 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
.. _quickstart:
.. module:: flask_rest_api

Quickstart
==========

``flask-rest-api`` makes a few assumptions about how the code should be structured.

The application should be split in :class:`Blueprint <Blueprint>`.
It is possible to use basic Flask view functions but it is generally a good idea
to use Flask :class:`MethodView <flask.views.MethodView>` classes instead.

Marshmallow :class:`Schema <marshmallow.Schema>` are used to serialize parameters
and responses. It may look overkill for a method with a single parameter, but it
makes the code consistent and it is easier to support.

Here is a basic Petstore example, where The ``Pet`` class is an imaginary ORM.

First instantiate an :class:`Api <Api>` with a :class:`Flask <flask.Flask>` application.


.. code-block:: python
from flask import Flask
from flask.views import MethodView
import marshmallow as ma
from flask_rest_api import Api, Blueprint
from .model import Pet
app = Flask('My API')
api = Api(app)
Define a marshmallow :class:`Schema <marshmallow.Schema>` to expose the model.

.. code-block:: python
@api.definition('Pet')
class PetSchema(ma.Schema):
class Meta:
strict = True
ordered = True
id = ma.fields.Int(dump_only=True)
name = ma.fields.String()
Define a marshmallow :class:`Schema <marshmallow.Schema>` to validate the
query arguments.

.. code-block:: python
class PetQueryArgsSchema(ma.Schema):
class Meta:
strict = True
ordered = True
name = ma.fields.String()
Instantiate a :class:`Blueprint <Blueprint>`.

.. code-block:: python
blp = Blueprint(
'pets', 'pets', url_prefix='/pets',
description='Operations on pets'
)
:class:`MethodView <flask.views.MethodView>` classes come in handy when dealing
with REST APIs.

.. code-block:: python
@blp.route('/')
class Pets(MethodView):
@api.doc(summary='List pets')
@api.arguments(PetQueryArgsSchema, location='query')
@api.response(PetSchema(many=True))
def get(self, args):
return Pet.get(filters=args)
@api.doc(summary='Add a new pet')
@api.arguments(PetSchema)
@api.response(PetSchema, code=201)
def post(self, new_data):
item = Pet.create(**new_data)
return item
@blp.route('/<pet_id>')
class PetsById(MethodView):
@api.doc(summary='Get pet by ID')
@api.response(PetSchema)
def get(self, pet_id):
item = Pet.get_by_id(pet_id)
return item
@api.doc(summary='Update an existing pet')
@api.arguments(PetSchema)
@api.response(PetSchema)
def put(self, update_data, pet_id):
item = Pet.get_by_id(pet_id)
item.update(update_data)
return item
@api.doc(summary='Delete a pet')
@api.response(code=204)
def delete(self, pet_id):
Pet.delete(pet_id)
Finally, register the :class:`Blueprint <Blueprint>` in the :class:`Api <Api>`.

.. code-block:: python
api.register_blueprint(blp)
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sphinx>=1.7.5

0 comments on commit 67318e9

Please sign in to comment.