Skip to content

Commit

Permalink
add resource __doc__ support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollmops committed Mar 25, 2020
1 parent 4276dfc commit 0cfa482
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
3 changes: 1 addition & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ Welcome to the **RestIt** documentation.
Python HTTP REST library including OOP-readiness and Open-API generation

.. toctree::
:titlesonly:
:includehidden:
:maxdepth: 4

installation
quickstart
Expand Down
44 changes: 40 additions & 4 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Quickstart
==========
**********

A Minimal Application
---------------------
=====================

To get started with RestIt we can use the following code snippet:

Expand All @@ -29,8 +29,8 @@ One of the key aspects of *REST* and the *RestIt* library are *Resources*. Since
*URI*, in our Python code we assign it using the :func:`~restit.decorator.path` decorator.


Serve Swagger/OpenApi Documentation
--------------------------------------
Swagger/OpenApi Documentation
=============================

To get your HTTP app serving an *OpenApi* documentation, you have to create an instance of
:class:`~restit.open_api.OpenApiDocumentation` and pass it to your :class:`~restit.RestItApp` constructor.
Expand All @@ -55,3 +55,39 @@ documentation.
Since we did not yet provide any information about our API we do not see too much in the *OpenApi* documentation yet.


More OpenApi Documentation Details
------------------------------------

Request Details
^^^^^^^^^^^^^^^

A description for the request method is always a good starting point and so we are adding a simple doc string to our
``get`` method:

.. code-block:: python
@path("/")
class IndexResource(Resource):
def get(self, request: Request) -> Response:
"""This is a super get method.
It takes a request and responds with a text.
"""
return Response("Hello from index.")
The doc string then will be used to generate the `summary and description <https://swagger.io/specification/#operationObject>`_ fields.


Query Parameters
""""""""""""""""

Path Parameters
"""""""""""""""

Request Body
""""""""""""


Response Details
^^^^^^^^^^^^^^^^
6 changes: 5 additions & 1 deletion restit/open_api/open_api_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def _generate_paths(self, root_spec: dict):
def _add_resource(self, paths: dict, resource: Resource, root_spec: dict):
path, inferred_path_parameters = \
self._infer_path_params_and_open_api_path_syntax(resource.__request_mapping__)
paths[path] = {}
summary, description = self._get_summary_and_description_from_doc(resource.__doc__)
paths[path] = {
"summary": summary,
"description": description
}
for method_name, method_object in self._get_allowed_resource_methods(resource).items():
paths[path][method_name] = {
"responses": {},
Expand Down
8 changes: 8 additions & 0 deletions test/restit/open_api/open_api_spec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class MyRequestBodySchema(Schema):

@path("/path")
class FirstResource(Resource):
"""First resource summary
Super description.
"""

@query_parameter("param1", description="A query parameter", field_type=fields.Integer(default=10))
def get(self, request: Request, **path_params) -> Response:
"""This is a summary.
Expand Down Expand Up @@ -112,6 +117,9 @@ def test_generate_spec(self):
self.assertEqual("3.0.0", open_api_dict["openapi"])

paths_path = open_api_dict["paths"]["/path"]
self.assertEqual("First resource summary", paths_path["summary"])
self.assertEqual("Super description.", paths_path["description"])

self.assertEqual({
'responses': {},
'parameters': [{
Expand Down

0 comments on commit 0cfa482

Please sign in to comment.