Create Postman JSON API documentation files from Sanic.
Project description
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]
header = {"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]
header = {"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
folderbp1.__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.
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
Built Distribution
File details
Details for the file sanic-to-json-0.1.8.tar.gz
.
File metadata
- Download URL: sanic-to-json-0.1.8.tar.gz
- Upload date:
- Size: 5.8 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | f33a77091a59b9b7934c79a42342c2d365a96afe2f048afa72f26474a99c90db |
|
MD5 | 9970de66bd2b7d56bb789d1b7ef43869 |
|
BLAKE2b-256 | 32083a4f9ce91601c6805d80d22b583ca555cb60786bf750d1d9e2177fea1a7c |
File details
Details for the file sanic_to_json-0.1.8-py3-none-any.whl
.
File metadata
- Download URL: sanic_to_json-0.1.8-py3-none-any.whl
- Upload date:
- Size: 7.2 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93712b2a0f2df78dca12b7feb68e2461e5883529cb4ba05c4d601c524feb7bf4 |
|
MD5 | edad20f407110274794323aa1f2a2b68 |
|
BLAKE2b-256 | 19e9cd41db621948a91a624669af6da10496a853d92a6b1a163ea62da476d9d5 |