{"id":689,"date":"2014-12-15T00:00:01","date_gmt":"2014-12-14T15:00:01","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=689"},"modified":"2016-11-30T18:24:50","modified_gmt":"2016-11-30T09:24:50","slug":"restful-api-docs-with-sphinxcontrib-httpdomain","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2014\/12\/restful-api-docs-with-sphinxcontrib-httpdomain\/","title":{"rendered":"sphinxcontrib.httpdomain \u3067 RESTful API \u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u4f5c\u308b"},"content":{"rendered":"

\u3053\u306e\u8a18\u4e8b\u306f CAMPHOR- Advent Calendar 2014<\/a> 15\u65e5\u76ee\u306e\u8a18\u4e8b\u3067\u3059.<\/p>\n

Python \u88fd\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u4f5c\u6210\u30c4\u30fc\u30eb\u3067\u3042\u308bSphinx (\u516c\u5f0f<\/a>, \u65e5\u672c<\/a>) \u3068, \u305d\u306e\u62e1\u5f35\u6a5f\u80fd\u3067\u3042\u308b sphinxcontrib.httpdomain<\/a> \u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3068 RESTful API \u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u7c21\u5358\u306b\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059.<\/p>\n

\"sphinxcontrib.httpdomain<\/a>
\n<\/p>\n

Sphinx \u3068\u306f<\/h2>\n

Sphinx \u306f Python \u88fd\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\u30d3\u30eb\u30c0\u30fc\u3067\u3059. reStructuredText<\/a> \u3068\u3044\u3046\u5f62\u5f0f\u3067\u30de\u30fc\u30af\u30a2\u30c3\u30d7\u3057\u305f\u3082\u306e\u3092\u30d3\u30eb\u30c9\u3057\u3066, HTML \u3084 PDF, ePub \u306a\u3069\u69d8\u3005\u306a\u5f62\u5f0f\u3067\u51fa\u529b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059.
\n\"Sphinx<\/p>\n

Sphinx \u306f Python \u306e\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3084\u69d8\u3005\u306a Python \u306e\u30e9\u30a4\u30d6\u30e9\u30ea (Django<\/a>, NumPy<\/a>, SciPy<\/a>, etc…) \u3067\u5229\u7528\u3055\u308c\u3066\u3044\u308b\u307b\u304b, Python \u4ee5\u5916\u306e\u8a00\u8a9e\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8 (CakePHP<\/a>, Chef<\/a>, etc…) \u3067\u3082\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e\u4f5c\u6210\u306b\u5229\u7528\u3055\u308c\u3066\u3044\u307e\u3059.<\/p>\n

Python \u306e\u30b3\u30fc\u30c9\u304b\u3089\u76f4\u63a5\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u751f\u6210\u3059\u308b\u3068\u3044\u3063\u305f\u4fbf\u5229\u306a\u6a5f\u80fd\u3082\u3042\u308a\u307e\u3059\u304c, Sphinx \u81ea\u4f53\u306f Python \u306e\u77e5\u8b58\u304c\u306a\u304f\u3066\u3082\u4f7f\u3044\u59cb\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059.<\/p>\n

sphinxcontrib.httpdomain \u3068\u306f<\/h2>\n

sphinxcontrib.httpdomain<\/a> \u306f Sphinx \u306e\u62e1\u5f35\u6a5f\u80fd\u3067, Sphinx \u3067 RESTful HTTP API \u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u4f5c\u6210\u3057\u3084\u3059\u304f\u3059\u308b\u30c4\u30fc\u30eb\u3067\u3059.
\nFlask, Bottle, Tornado \u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u4f5c\u6210\u3055\u308c\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304b\u3089\u306f\u81ea\u52d5\u7684\u306b\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u304c, \u3053\u3053\u3067\u306f\u624b\u52d5\u3067\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u307e\u3059.<\/p>\n

Sphinx \u3068 sphinxcontrib.httpdomain \u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n

\u73fe\u6642\u70b9\u3067\u306e\u6700\u65b0\u306e\u5b89\u5b9a\u7248\u3067\u3042\u308b Sphinx 1.2.3 \u3092\u5229\u7528\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3059. Sphinx \u306f Python 2 \u3068 3 \u306e\u3069\u3061\u3089\u3067\u3082\u52d5\u4f5c\u3057\u307e\u3059\u304c, \u73fe\u5728\u306f Python 2.7 \u306e\u5229\u7528\u304c\u63a8\u5968\u3055\u308c\u3066\u3044\u307e\u3059.<\/p>\n

\u3053\u3053\u3067\u306f\u3059\u3067\u306b Python 2.7 \u3068 pip \u7b49\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u6e08\u3093\u3067\u3044\u308b\u3082\u306e\u3068\u3057\u3066\u9032\u3081\u307e\u3059. Python \u3068 Sphinx \u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306b\u3064\u3044\u3066\u306e\u8a73\u7d30\u306f\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a>\u3082\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044.<\/p>\n

    \n
  1. \u5fc5\u8981\u306b\u5fdc\u3058\u3066 virtualenv \u3084 pyenv \u3067\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u6210\u3057\u307e\u3059.<\/li>\n
  2. Sphinx \u3068 sphinxcontrib.httpdomain \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059.<\/li>\n<\/ol>\n
    pip install Sphinx\r\npip install sphinxcontrib-httpdomain<\/pre>\n

    Sphinx \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e\u4f5c\u6210<\/h2>\n

    Sphinx \u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e\u4f5c\u6210\u306b\u5fc5\u8981\u306a\u30d5\u30a1\u30a4\u30eb\u4e00\u5f0f\u3092\u6e96\u5099\u3057\u3066\u304f\u308c\u308b sphinx-quickstart \u3068\u3044\u3046\u30b3\u30de\u30f3\u30c9\u304c\u3042\u308b\u305f\u3081, \u3053\u308c\u3092\u7528\u3044\u3066\u5fc5\u8981\u306a\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059. \u57fa\u672c\u7684\u306b\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u8a2d\u5b9a\u3067\u554f\u984c\u306a\u3044\u3067\u3059\u304c, Project name, Author name(s), Project version \u306a\u3069\u306f\u8a2d\u5b9a\u304c\u5fc5\u8981\u3067\u3059.<\/p>\n

    $ sphinx-quickstart\r\nWelcome to the Sphinx 1.2.3 quickstart utility.\r\n\r\nPlease enter values for the following settings (just press Enter to\r\naccept a default value, if one is given in brackets).\r\n\r\nEnter the root path for documentation.\r\n> Root path for the documentation [.]:\r\n\r\nYou have two options for placing the build directory for Sphinx output.\r\nEither, you use a directory \"_build\" within the root path, or you separate\r\n\"source\" and \"build\" directories within the root path.\r\n> Separate source and build directories (y\/n) [n]:\r\n\r\nInside the root directory, two more directories will be created; \"_templates\"\r\nfor custom HTML templates and \"_static\" for custom stylesheets and other static\r\nfiles. You can enter another prefix (such as \".\") to replace the underscore.\r\n> Name prefix for templates and static dir [_]:\r\n\r\nThe project name will occur in several places in the built documentation.\r\n> Project name: RESTful HTTP APIs\r\n> Author name(s): Yusuke Miyazaki\r\n\r\nSphinx has the notion of a \"version\" and a \"release\" for the\r\nsoftware. Each version can have multiple releases. For example, for\r\nPython the version is something like 2.5 or 3.0, while the release is\r\nsomething like 2.5.1 or 3.0a1.  If you don't need this dual structure,\r\njust set both to the same value.\r\n> Project version: 1.0.0\r\n> Project release [1.0.0]:\r\n\r\nThe file name suffix for source files. Commonly, this is either \".txt\"\r\nor \".rst\".  Only files with this suffix are considered documents.\r\n> Source file suffix [.rst]:\r\n\r\nOne document is special in that it is considered the top node of the\r\n\"contents tree\", that is, it is the root of the hierarchical structure\r\nof the documents. Normally, this is \"index\", but if your \"index\"\r\ndocument is a custom template, you can also set this to another filename.\r\n> Name of your master document (without suffix) [index]:\r\n\r\nSphinx can also add configuration for epub output:\r\n> Do you want to use the epub builder (y\/n) [n]:\r\n\r\nPlease indicate if you want to use one of the following Sphinx extensions:\r\n> autodoc: automatically insert docstrings from modules (y\/n) [n]:\r\n> doctest: automatically test code snippets in doctest blocks (y\/n) [n]:\r\n> intersphinx: link between Sphinx documentation of different projects (y\/n) [n]:\r\n> todo: write \"todo\" entries that can be shown or hidden on build (y\/n) [n]:\r\n> coverage: checks for documentation coverage (y\/n) [n]:\r\n> pngmath: include math, rendered as PNG images (y\/n) [n]:\r\n> mathjax: include math, rendered in the browser by MathJax (y\/n) [n]:\r\n> ifconfig: conditional inclusion of content based on config values (y\/n) [n]:\r\n> viewcode: include links to the source code of documented Python objects (y\/n) [n]:\r\n\r\nA Makefile and a Windows command file can be generated for you so that you\r\nonly have to run e.g. `make html' instead of invoking sphinx-build\r\ndirectly.\r\n> Create Makefile? (y\/n) [y]:\r\n> Create Windows command file? (y\/n) [y]:\r\n\r\nCreating file .\/conf.py.\r\nCreating file .\/index.rst.\r\nCreating file .\/Makefile.\r\nCreating file .\/make.bat.\r\n\r\nFinished: An initial directory structure has been created.\r\n\r\nYou should now populate your master file .\/index.rst and create other documentation\r\nsource files. Use the Makefile to build the docs, like so:\r\n   make builder\r\nwhere \"builder\" is one of the supported builders, e.g. html, latex or linkcheck.\r\n<\/pre>\n

    \u3053\u308c\u3067\u5fc5\u8981\u306a\u30d5\u30a1\u30a4\u30eb\u304c\u751f\u6210\u3055\u308c\u307e\u3057\u305f.<\/p>\n

    make html<\/pre>\n

    \u3092\u5b9f\u884c\u3059\u308b\u3068, _build\/html\/index.html \u306bHTML \u30d5\u30a1\u30a4\u30eb\u304c\u751f\u6210\u3055\u308c\u308b\u306e\u3067\u30d6\u30e9\u30a6\u30b6\u3067\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046.
    \n
    \"Sphinx<\/a><\/p>\n

    sphinxcontrib.httpdomain \u306e\u8a2d\u5b9a<\/h2>\n

    sphinxcontrib.httpdomain \u3092\u5229\u7528\u3059\u308b\u305f\u3081\u306b Sphinx \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u4fee\u6b63\u3057\u307e\u3059. \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e conf.py \u306e extensions \u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u307e\u3059.<\/p>\n

    # -*- coding: utf-8 -*-\r\n#\r\n# RESTful HTTP APIs documentation build configuration file, created by\r\n# sphinx-quickstart on Fri Dec 12 15:43:58 2014.\r\n#\r\n# This file is execfile()d with the current directory set to its\r\n# containing dir.\r\n#\r\n# Note that not all possible configuration values are present in this\r\n# autogenerated file.\r\n#\r\n# All configuration values have a default; values that are commented out\r\n# serve to show the default.\r\n\r\nimport sys\r\nimport os\r\n\r\n# If extensions (or modules to document with autodoc) are in another directory,\r\n# add these directories to sys.path here. If the directory is relative to the\r\n# documentation root, use os.path.abspath to make it absolute, like shown here.\r\n#sys.path.insert(0, os.path.abspath('.'))\r\n\r\n# -- General configuration ------------------------------------------------\r\n\r\n# If your documentation needs a minimal Sphinx version, state it here.\r\n#needs_sphinx = '1.0'\r\n\r\n# Add any Sphinx extension module names here, as strings. They can be\r\n# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom\r\n# ones.\r\nextensions = ['sphinxcontrib.httpdomain']\r\n\r\n# Add any paths that contain templates here, relative to this directory.\r\ntemplates_path = ['_templates']<\/pre>\n

    \u30da\u30fc\u30b8\u306e\u8ffd\u52a0<\/h2>\n

    API \u3068\u3044\u3046\u540d\u524d\u3067\u30da\u30fc\u30b8\u3092\u8ffd\u52a0\u3057\u3066\u305d\u3053\u306b\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8a18\u8ff0\u3057\u307e\u3059. \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b api.rst \u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066, \u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5185\u5bb9\u3092\u8a18\u8ff0\u3057\u307e\u3059.<\/p>\n

    API\r\n===\r\n\r\nRESTful API \u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8.\r\n<\/pre>\n

    \u6b21\u306b index.rst \u306e toctree \u3092\u7de8\u96c6\u3057\u3066, api.rst \u3068\u306e\u95a2\u4fc2\u3092\u8ffd\u52a0\u3057\u307e\u3059.<\/p>\n

    .. RESTful HTTP APIs documentation master file, created by\r\n   sphinx-quickstart on Fri Dec 12 15:43:58 2014.\r\n   You can adapt this file completely to your liking, but it should at least\r\n   contain the root `toctree` directive.\r\n\r\nWelcome to RESTful HTTP APIs's documentation!\r\n=============================================\r\n\r\nContents:\r\n\r\n.. toctree::\r\n   :maxdepth: 2\r\n\r\n   api\r\n\r\n\r\nIndices and tables\r\n==================\r\n\r\n* :ref:`genindex`\r\n* :ref:`modindex`\r\n* :ref:`search`\r\n<\/pre>\n

    API \u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u4f5c\u6210\u3059\u308b<\/h2>\n

    api.rst \u3092\u7de8\u96c6\u3057\u3066\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059. \u3053\u3053\u3067\u306f\u7279\u5b9a\u306e\u30e6\u30fc\u30b6\u30fc (user_id) \u306e\u66f8\u7c4d\u306e\u30ea\u30b9\u30c8\u3092\u53d6\u5f97\u3059\u308b API (\/users\/(int:user_id)\/books) \u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u307e\u3059. api.rst \u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059:<\/p>\n

    API\r\n===\r\n\r\nRESTful API \u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8.\r\n\r\n.. http:get:: \/users\/(int:user_id)\/books\r\n\r\n   \u30e6\u30fc\u30b6\u30fc (`user_id`) \u306e\u66f8\u7c4d\u306e\u30ea\u30b9\u30c8\u3092\u53d6\u5f97\u3059\u308b API.\r\n\r\n   **\u30ea\u30af\u30a8\u30b9\u30c8\u4f8b**\r\n\r\n   .. sourcecode:: http\r\n\r\n      GET \/users\/123\/books HTTP\/1.1\r\n      Host: example.com\r\n      Accept: application\/json\r\n\r\n   **\u30ec\u30b9\u30dd\u30f3\u30b9\u4f8b**\r\n\r\n   .. sourcecode:: http\r\n\r\n      HTTP\/1.1 200 OK\r\n      Vary: Accept\r\n      Content-Type: application\/json\r\n\r\n      [\r\n        {\r\n          \"book_id\": 123,\r\n          \"author_id\": 35,\r\n          \"title\": \"Sphinx Book\"\r\n        },\r\n        {\r\n          \"book_id\": 125,\r\n          \"author_id\": 13,\r\n          \"title\": \"Python Book\"\r\n        }\r\n      ]\r\n\r\n   :query sort: ``created_at`` \u307e\u305f\u306f ``title``\r\n   :query limit: \u53d6\u5f97\u4ef6\u6570 (\u30c7\u30d5\u30a9\u30eb\u30c8: 10)\r\n   :reqheader Authorization: OAuth \u306e\u30c8\u30fc\u30af\u30f3 (\u30aa\u30d7\u30b7\u30e7\u30f3)\r\n   :statuscode 200: \u6210\u529f\r\n   :statuscode 404: \u30e6\u30fc\u30b6\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\r\n<\/pre>\n

    make html \u3067\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n

    \"Sphinx<\/a><\/p>\n

    GET \u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f, \u4ee5\u4e0b\u306e\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3092\u7528\u3044\u307e\u3059. \u4ed6\u306b\u3082 POST, PUT, DELETE \u306a\u3069\u304c\u3082\u3061\u308d\u3093\u4f7f\u3048\u307e\u3059<\/a>.<\/p>\n

    http:get:: \/users\/(int:user_id)\/books\r\n<\/pre>\n

    API \u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3084\u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9\u306b\u3064\u3044\u3066\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3068, \u7dba\u9e97\u306b\u6574\u5f62\u3055\u308c\u3066\u51fa\u529b\u3055\u308c\u307e\u3059.<\/p>\n

        :query sort: ``created_at`` \u307e\u305f\u306f ``title``\r\n    :query limit: \u53d6\u5f97\u4ef6\u6570 (\u30c7\u30d5\u30a9\u30eb\u30c8: 10)\r\n    :reqheader Authorization: OAuth \u306e\u30c8\u30fc\u30af\u30f3 (\u30aa\u30d7\u30b7\u30e7\u30f3)\r\n    :statuscode 200: \u6210\u529f\r\n    :statuscode 404: \u30e6\u30fc\u30b6\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\r\n<\/pre>\n

    \u3053\u306e\u307b\u304b\u306b\u3082\u69d8\u3005\u306a\u60c5\u5831<\/a>\u3092\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059.<\/p>\n