docapi is a Python API Documentation Utility.
Write API documentation as you write codes and Generate Mock Server Code Immediately.
- It is always fun to learn something while building it.
- I wanted to build simple, yet expressive tool that makes writing API documentation fun, expressive, engaging and distinctive.
- I love to create, mimic and explore.
pip install git+https://github.com/sabbiramin113008/docapi.gitWriting documentation involves 4 entities.
- Doc
- API
- Request and
- Response
First, create a Doc object.
doc = Doc(name='My Demo API List')You can set Base URL of your APIs,
doc.base_url = 'http://127.0.0.1:8088'or can leave it default to http://127.0.0.1:1034
Now time to work with API entity.
An API object can be consist of these following properties, while instantiation.
But first, instantiate an API object.
get_book_details_by_id = API()Now, you can set name, details and url straight to this API.
...
get_book_details_by_id.name = 'Get Book Details By ID'
get_book_details_by_id.url = '/api/books/id'
get_book_details_by_id.details = 'Some Dummy Details'Now, lets set the Request and Response to this api.
Setting http method name, request header is simple. Try like this,
...
get_book_details_by_id.request.method = ['GET']
get_book_details_by_id.request.header = {
'http-api-key':'amkik.skskjuweoiwreoiudf'
}URL params can also be set in the same manner. Just add a python dictionary.
...
get_book_details_by_id.request.params = {
'isActive':1,
'sortBy':'Date',
'page':1,
'size':200
}A request body can also be set, like
...
get_book_details_by_id.request.body = {
'name':'John Doe',
'occupation':'Developer'
}Now let's process the Response entity of the API.
Add Response body, HTTP Status Code and Response Header Like this,
...
get_book_details_by_id.response.header = {
'http-api-key':'kdkkd.kdkkddkdkdddldl'
}
get_book_details_by_id.response.status_code = 200
get_book_details_by_id.response.body = {
'id':'1',
'name':'John Doe',
'hasDelivered':True
}Now, we have to register this api to our doc object. Let's
...
doc.add_api(get_book_details_by_id)After registering all the APIs to the doc, we simply generate the file with all the API documentation, simply by this command,
doc.build()A document.md will be generated afterwards.
The lates version of this module can generate simple Flask based Web Application
Server, from the documentation. Just add,
...
doc.build_server()This will generate a server.py file with all the API routes.
Detailed Example can be found on the Example directory.
NB: The Server code is written using Flask, so it is necessary to have this installed on your machine,
if you are willing to generate the server code for your mock service.
Voila, it is easy, simple and expressive.