Skip to main content

doctor

Documentation Status Build Status Pypi

This module uses python types to validate request and response data in Flask Python APIs. It uses python 3 type hints to validate request paramters and generate API documentation. It also supports generic schema validation for plain dictionaries. An example of the generated API documentation can be found in the docs.

Install

doctor can easily be installed using pip:

$ pip install doctor

Quick Start

Define some types that will be used to validate your request parameters.

# mytypes.py
from doctor import types

# doctor provides helper functions to easily define simple types.
FooId = types.integer('The foo ID.')
FetchBars = types.boolean('A flag that indicates if we should fetch bars')

# You can also inherit from type classes to create more complex types.
class Foo(types.Object):
    description = 'A Foo object'
    example = {'foo_id': 1}
    properties = {'foo_id': FooId}
    required = ['foo_id']
    additional_properties = False

Define the logic function that our endpoint will route to:

# foo.py
from mytypes import Foo, FooId, FetchBars

# Note the type annotations on this function definition. This tells Doctor how
# to parse and validate parameters for routes attached to this logic function.
# The return type annotation will validate the response conforms to an
# expected definition in development environments.  In non-development
# environments a warning will be logged.
def get_foo(foo_id: FooId, fetch_bars: FetchBars=False) -> Foo:
    """Fetches the Foo object and optionally related bars."""
    return Foo.get_by_id(foo_id, fetch_bars=fetch_bars)

Now tie the endpoint to the logic function with a route:

from flask import Flask
from flask_restful import Api
from doctor.routing import create_routes, get, Route

from foo import get_foo

routes = (
    Route('/foo/<int:foo_id>/', methods=(
        get(get_foo),)
    ),
)

app = Flask(__name__)
api = Api(app)
for route, resource in create_routes(routes):
    api.add_resource(resource, route)

That’s it, you now have a functioning API endpoint you can curl and the request is automatically validated for you based on your schema. Positional arguments in your logic function are considered required request parameters and keyword arguments are considered optional. As a bonus, using the autoflask sphinx directive, you will also get automatically generated API documentation.

Generated API documentation

Documentation

Documentation and a full example is available at readthedocs.

Running Tests

Tests can be run with tox. It will handle installing dependencies into a virtualenv, running tests, and rebuilding documentation.

Then run Tox:

cd doctor
tox

You can pass arguments to pytest directly:

tox -- test/test_flask.py

Metadata

Release files for doctor 3.13.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for doctor 3.13.7
File Size Uploaded
doctor-3.13.7.tar.gz 60.9 kB Details

Release files / doctor-3.13.7.tar.gz

Download URL doctor-3.13.7.tar.gz
Size 60.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d2cf1521c83a94bfcb73c4ee5e8a7c7de4c597eb0f60764d190bd7a9f72e8075
BLAKE2b-256 checksum
How to use checksums
7caf8d085099cd0c083cbfc50a3b4c2ce557bf698797885728311061971f6e33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.8

Release history Release notifications | RSS feed

This release

3.13.7 This release

1 release file

3.13.6

1 release file

3.13.5

1 release file

3.13.4

1 release file

3.13.3

1 release file

3.13.2

1 release file

3.13.1

1 release file

3.13.0

2 release files

3.12.3

1 release file

3.12.2

1 release file

3.12.1

1 release file

3.12.0

1 release file

3.11.0

1 release file

3.10.3

1 release file

3.10.2

1 release file

3.10.1

1 release file

3.10.0

1 release file

3.9.0

1 release file

3.8.2

1 release file

3.8.1

1 release file

3.8.0

1 release file

3.7.0

1 release file

3.6.1

1 release file

3.6.0

1 release file

3.5.0

1 release file

3.4.0

1 release file

3.3.0

1 release file

3.2.0

1 release file

3.1.0

2 release files

3.0.1

1 release file

3.0.0

1 release file

1.4.0

1 release file

1.3.5

1 release file

1.3.4

2 release files

1.3.3

3 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.2

5 release files

1.2.1

2 release files

1.2.0

3 release files

1.1.4

3 release files

1.1.3

3 release files

1.1.2

3 release files

1.1.1

3 release files

1.1.0

3 release files

1.0.1

2 release files

1.0.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page