Skip to main content

A lightweight Python WebAPI's documents and tests framework based on __doc__, VanillaJS-AJAX and Flask, but not limited to Flask.

Project description

APIDocTest

A lightweight Python WebAPI's documents and tests framework based on __doc__, VanillaJS-AJAX and Flask, but not limited to Flask.

Details

Each of API handlers' __doc__ line which starts with "@" will be treated as API's notes, excludes the leading spaces. The "::" is the separator between key and value of each APIs='s note. blanks in both ends of key and value are ignored.

key value(samples) comments
@path-parameters variablex=literalx & variabley=literaly & variablez=literalz placeholders in router's url, such as "/path/to//status".
@request-parameters variablex=literalx & variabley=literaly & variablez=literalz like things after "?" in url, such as "http://nagexiucai.com/topic.php?title=ml-in-ten-pictures&md=ml-in-ten-pictures".
@request-headers content-type:text/json;charset=utf-8 & user-agent:mozilla/5.0 gecko/20100101 firefox/38.0
@request-body {"author":"bob-nx"}
@response-headers content-type:text/html;charset=utf-8 & server:werkzeug/0.14.1 python/2.7.5
@response-body "hi"
@norm ==[caseignored] or ~=[regular expression] or ## "==" for "same", "~=" for "similar", "##" for "silent"; if "[extra]" supplied, "extra" works.

Examples

The core is use APIDocTest('router', ['methods'], name='name', description='description' version="version") as handlers' first decorator.

from flask import blueprints
from apidoctest import APIDocTest

bapidoctest = blueprints.Blueprint("apidoctest", __name__)

@bapidoctest.route("/fuck", methods=["GET", "POST"])
@APIDocTest("/fuck", ["GET", "POST"], name="fuck", description="uncourteous.")
def Fuck():
    '''
    @path-parameters::
    @request-parameters::
    @request-headers:: Content-Type:application/json
    @request-body:: {"who":"unknown"}
    @response-headers:: Content-Type:text/html;charset=utf-8
    @response-body:: "fuck"
    @norm:: ==
    '''
    return "fuck"

import sys
reload(sys)
sys.setdefaultencoding("utf8")

from flask import Flask, request, render_template_string, json
from apidoctest import APIDocTestTemplateString
serv = Flask("APIDocTestDemo")
@serv.route("/apidoctest")
def documents():
    return render_template_string(APIDocTestTemplateString, data=json.dumps(APIDocTest.apidocs, encoding="utf-8"))
serv.register_blueprint(bapidoctest, url_prefix="/what")
serv.run(debug=False, host="localhost", port=9527)

Result

Use browser(Google Chrome recommended), access http://localhost:9527/apidoctest(in examples above).

ToDo

  • I18n(Chinese friendly now)
  • Parameters validation
  • Input/Output grouped by test cases

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

APIDocTest-0.0.2.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

APIDocTest-0.0.2-py2-none-any.whl (599.3 kB view details)

Uploaded Python 2

File details

Details for the file APIDocTest-0.0.2.tar.gz.

File metadata

  • Download URL: APIDocTest-0.0.2.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.14

File hashes

Hashes for APIDocTest-0.0.2.tar.gz
Algorithm Hash digest
SHA256 7cf3164d6ba3db163dd747bce78f7a692fb10b15d956a1d61a83df99707660e3
MD5 93a877911185cacdd193e6d9a6afff62
BLAKE2b-256 b0b33a41afda54f4eb2ef9ade9a810a9f7282b63d907773282a1ccee84ecbb20

See more details on using hashes here.

File details

Details for the file APIDocTest-0.0.2-py2-none-any.whl.

File metadata

  • Download URL: APIDocTest-0.0.2-py2-none-any.whl
  • Upload date:
  • Size: 599.3 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.14

File hashes

Hashes for APIDocTest-0.0.2-py2-none-any.whl
Algorithm Hash digest
SHA256 7198345ee75d65e8409f92ae77285b98ed03ebd72084fe19a2b060c7b24d69cd
MD5 7b2104a6f251b4fbe9783a1154d0337a
BLAKE2b-256 54c4409ee648067b0c5b85089c5d7601c8e1c6d1263ec890cd38eb309ca2978f

See more details on using hashes here.

Supported by

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