Skip to main content

Create Postman JSON API documentation files from Sanic.

Project description

Code style: black

Build Status

sanic-to-json

Generate a Postman JSON file from a Sanic app. The JSON file can directly uploaded into the Postman client or through their API.

Using the postman schema we can build Postman Collections using python endpoints from Sanic (Flask apps need testing). The script parses the Sanic app. It searches for blueprints. The blueprints, through routes, provide docs strings data. The doc string data is used to populate a Postman formatted JSON file. The JSON file can then be uploaded to Postman as a collection.

Once we have Postman formatted JSON we can create API documentation through the Postman API

How to use

  • to execute an example run python -m examples.example_script which executes
from sanic_to_json import generate_sanic_json
from examples.app import app

generate_sanic_json("Test API", app, filename="postman_collection.json")

The above code formats the Postman collection with 'Test API' and doc strings from Sanic app, app, and yields postman_collection.json

To add body and header elements to Postman JSON

Placing INI in doc string will cause the text below to be loaded into a python config object.

For example, as found in endpoint-three

    INI
    [request]
    header = {"Content-Type": "application/json"}
    body = {"username": "{{username}}", "password": "{{password}}"}

To add example requests

Similar to above placing sections under INI that are prefixed will start a example request. For example, as found in endpoint-one

    """Return text from request.
       
    INI
    [request]
    header = {"Content-Type": "application/json","x-amz-sns-message-type": "Notification"}
    body = {"username": "{{username}}", "password": "{{password}}"}

    [example.single]
    name = single query
    method = POST
    query = ?days=1&units=metric
    header = {"Content-Type": "application/json"}
    body = {"token": "POST token"}

    [example.multiple]
    name = multiple query
    method = POST
    query = ?days=3&units=metric&time=1400
    header = {"Content-Type": "application/json"}
    body = {"token": "token"}

    [example.another]
    name = another query
    method = POST
    query = ?days=1&units=metric
    header = {"Content-Type": "application/json"}
    body = {"token": "POST token"}
    """

How to document Sanic app and Blueprints

  • As the example shows, the Sanic app should have a .doc attribute. This doc string will serve as the introduction to the API in Postman docs, e.g., app.__doc__ = "This API does stuff."

  • Blueprints should also a doc string, this will serve as the description to each collection folder in Postman. Again see examples folder bp1.__doc__ = "This is the doc string for blueprint1."

How to install

pip install sanic-to-json

Contributors

See the GitHub contributor page

License

sanic-to-json is open source software licensed as MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanic-to-json-0.1.7.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

sanic_to_json-0.1.7-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file sanic-to-json-0.1.7.tar.gz.

File metadata

  • Download URL: sanic-to-json-0.1.7.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.1 Linux/4.15.0-1028-gcp

File hashes

Hashes for sanic-to-json-0.1.7.tar.gz
Algorithm Hash digest
SHA256 5b5c87aa845a64edaeb252ca3e1ae37ad835a0ea59b85fd66c5719a2a2283c3f
MD5 0d9c9ffa50e8226579095c01c126451d
BLAKE2b-256 a3da4f04e1d8ddd3107fa0b8ad2a0e86e5c673fc3c31774ac6c5de2a83d6f32a

See more details on using hashes here.

File details

Details for the file sanic_to_json-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: sanic_to_json-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.1 Linux/4.15.0-1028-gcp

File hashes

Hashes for sanic_to_json-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 65f6c438f3e945929dcfc6d4bb83b0c50651bcd66beea9f5ca61501b328afc46
MD5 e70a9a6b8ef6738c92a65a47ab4fd452
BLAKE2b-256 3e3a87f79f87d0e22f652202b416923fb7a26b66fe167413983a8e359e0e1193

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page