Skip to main content

An easy to use Python module for creating API.

Project description

speedyapi

An easy-to-use Python module for creating API.

Built on Flask / Async Flask with features for easily creating API endpoints

Features:

  • Parameter Parsing and Checking
  • API Authentication
  • Rate Limiting (user, address, global)
  • In-depth Endpoint Testing (Write tests to be run that confirm endpoints act as intended)
  • Automatic OpenAPI swagger.json Generation (with access to full specification)
  • Common JSON Response Formatting

Installation

Simply run pip install speedyapi. The PyPI package is at https://pypi.org/project/speedyapi/

Dependencies

Example Usage

from speedyapi import API, QueryParameter, PathParameter, respond, request, Test, types
from speedyapi.swagger_objects import InfoObject, XLogoObject


info = InfoObject(title="Example API", version="1.0.1")

app = API(__name__, info=info)
app.swagger.info.logo = XLogoObject(url="")
app.swagger.info.description = "# Introduction\nThis is an example API for the speedyapi python module."


@app.authentication(apikey_required=True, description="Example apikey: `CorrectApikey`")
def auth():
    return "allowed" if request.apikey == "CorrectApikey" else None


@app.endpoint(path="/maths/<method>", method="GET", authentication="allowed", name="Simple Maths", description="Simple operations.")
@app.limits(user_limits=["10/min"], ip_limits=["30/min"], global_limits=["5000/5 min"])
@app.tests(Test(url="/maths/multiply?a=3&b=5", headers={"Apikey": "CorrectApikey"}, expected_status_code=200, checks=[lambda x: x["result"] == 15]),
           Test(url="/maths/multiply?a=3&b=5", headers={"Apikey": "WrongApikey"}, expected_status_code=403),
           Test(url="/maths/modulo?a=3&b=5", headers={"Apikey": "CorrectApikey"}, expected_status_code=422),
           Test(url="/maths/multiply?a=3&b=5", expected_status_code=400))
@app.parameters(PathParameter(name="method", options=["multiply", "divide", "add", "subtract"], default="add", description="Choose operation."),
                QueryParameter(name="a", type=types.Number, required=True, description="First number to use."),
                QueryParameter(name="b", type=types.Number, required=True, description="Second number to use."))
def simple_maths_endpoint(method, a, b):
    methods = {"multiply": lambda x, y: x * y, "divide": lambda x, y: x / y, "add": lambda x, y: x + y, "subtract": lambda x, y: x - y}
    return respond.json({"result": methods[method](a, b), "method": method, "authentication": request.authentication})


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=80, tests=True, threaded=True)
------------------------------------------------------------------------------------------
 *  Test on path `/maths/multiply - - GET` returned correct status code 200 [Time: 2.12s] - - {"success": true, "result": 15, "method": "multiply", "authentication": "allowed"}
 *  Test on path `/maths/multiply - - GET` Success [Time: 2.12s] - - {"success": true, "result": 15, "method": "multiply", "authentication": "allowed"}
 *  Test on path `/maths/modulo - - GET` returned correct status code 422 [Time: 2.12s] - - {"success": false, "cause": "Malformed [method]"}
 *  Test on path `/maths/multiply - - GET` returned correct status code 400 [Time: 2.12s] - - {"success": false, "cause": "Missing one or more fields [key]"}
 *  Test on path `/maths/multiply - - GET` returned correct status code 403 [Time: 2.12s] - - {"success": false, "cause": "Access is forbidden, usually due to an invalid API key being used."}
 *  Tests Completed - - (Success: 5/5) - [Time: 2.13s]
 -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - 
 *  API Ready - - [Running on: http://0.0.0.0:80/] - - (Press CTRL+C to quit!)
------------------------------------------------------------------------------------------

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

speedyapi-1.0.11.tar.gz (43.3 kB view details)

Uploaded Source

Built Distribution

speedyapi-1.0.11-py3-none-any.whl (46.0 kB view details)

Uploaded Python 3

File details

Details for the file speedyapi-1.0.11.tar.gz.

File metadata

  • Download URL: speedyapi-1.0.11.tar.gz
  • Upload date:
  • Size: 43.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.0

File hashes

Hashes for speedyapi-1.0.11.tar.gz
Algorithm Hash digest
SHA256 baee7ec80e3592ff1646dfc0aa38e60ba640809ad4d365070d2796201a96724f
MD5 316d11e84fc114a07cb3b99f15e4c826
BLAKE2b-256 7b6d44ffb90b0a5e39348b68bccfbf64ffa787693704139ed9514adab719f04e

See more details on using hashes here.

File details

Details for the file speedyapi-1.0.11-py3-none-any.whl.

File metadata

  • Download URL: speedyapi-1.0.11-py3-none-any.whl
  • Upload date:
  • Size: 46.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.0

File hashes

Hashes for speedyapi-1.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 6fcb06d29c74d0780e37718eddc02da9ae0f7fe13255444f3943424ec590517b
MD5 e77b411f8e62ca9ca53b2f4d4e7b5b4e
BLAKE2b-256 d207460b6fe71a52ee97ac78e2ffd1fcaad9a959f9215f1ad779e835c687488c

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