Skip to main content

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, which then get converted to the appropiate headers and JSON body in Posttman.

For example, as found in endpoint-three

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

To add query parameters

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]
    headers = {"Content-Type": "application/json","x-amz-sns-message-type": "Notification"}
    query = ?day=1&temp=F
    """

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 include 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."

  • Note: You can't send a JSON body with GET method.

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.

Release files for sanic-to-json 0.1.9

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

Source distribution (sdist)

Source distribution for sanic-to-json 0.1.9
File Size Uploaded
sanic-to-json-0.1.9.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sanic-to-json 0.1.9
File Interpreter ABI Platform
sanic_to_json-0.1.9-py3-none-any.whl Python 3 none any Details

Total release size: 13.0 kB

Release files / sanic-to-json-0.1.9.tar.gz

Download URL sanic-to-json-0.1.9.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0d0f5c350b33e08519dd1e5195b938fe87bcb92f0b74ab738713c023e9bcbd0d
BLAKE2b-256 checksum
How to use checksums
ae815d850c19ce2a82945a3ffcfa5d8a1c4e472389957cd569c642acb86a629a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.7.1 Linux/4.15.0-1028-gcp

Release files / sanic_to_json-0.1.9-py3-none-any.whl

Download URL sanic_to_json-0.1.9-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4059b867e24d4a5a0a69ddc9b892bd6e182061771475afaa1b434f46dd29db2
BLAKE2b-256 checksum
How to use checksums
1e32948ac09561dfbba7cbd974981ed3955d96164c30bb60d2d980cd2f67fd2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.17 CPython/3.7.1 Linux/4.15.0-1028-gcp

Release history Release notifications | RSS feed

This release

0.1.9 This release

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

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