Skip to main content

Web API Utils for Werkzeug

Project description

wau - Web API Utils

Web API Utils, or short wau, is a thin layer on top of Werkzeug and other (optional) libraries to provide a simple way for writing APIs in Python with zero boilerplate code.

It is built for educational purposes and is not intended for production use.

wau supports JSON as it's only data exchange format and uses type annotations to define and enforce the inputs of the API endpoints. It comes with built-in support for accessing databases (via dataset), user authentication, and real-time apps using server-sent events. A development server with sensible CORS support and live reload for API code and static files easies development and testing.

Installation

The use of uv is recommended for installing and managing dependencies, and managing virtual environments. Install wau into the current environment with:

uv add wau

An Introductory Example

The stereotypical "todo" API example looks like this with wau:

from wau import API, NotFound, database_connect, run

api = API()
db = database_connect("todos.db")
todos = db["todos"]

@api.GET("/todo/")
def list_todos():
    return list(todos.find())

@api.POST("/todo/")
def new_entry(text: str):
    todo_id = todos.insert({"text": text, "done": False})
    return {"id": todo_id, "text": text, "done": False}

@api.PUT("/todo/{id: int}")
def update_entry(id, done: bool):
    if not todos.find_one(id=id):
        raise NotFound()
    todos.update({"id": id, "done": done}, ["id"])

@api.DELETE("/todo/{id: int}")
def delete_entry(id):
    if not todos.find_one(id=id):
        raise NotFound()
    todos.delete(id=id)

if __name__ == "__main__":
    run(api)

Run the backend with:

$ python todo_api.py
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://localhost:3000
Press CTRL+C to quit
 * Restarting with watchdog (windowsapi)

Now you can send requests to the API, for example with curl:

curl -X POST -H "Content-Type: application/json" -d '{"text": "Buy bread"}' http://localhost:3000/todo/
{"id": 1, "text": "Buy bread", "done": false}

The development server will by default serve static files from the same directory as the script. For example, if you create an index.html file with some JavaScript that fetches the API, you can open the mentioned URL (http://localhost:3000/) in the browser and it will work without any additional configuration.

The development server also supports live reload, so if you change the API code or the static files, the browser will automatically refresh to reflect the changes.

License

This project is licensed under GNU LGPL v3 or later (LGPL-3.0-or-later).

If you distribute modified versions of this library, those library modifications must be published under the same license terms.

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

wau-0.1.9.tar.gz (27.2 kB view details)

Uploaded Source

Built Distribution

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

wau-0.1.9-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file wau-0.1.9.tar.gz.

File metadata

  • Download URL: wau-0.1.9.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.22 {"installer":{"name":"uv","version":"0.11.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for wau-0.1.9.tar.gz
Algorithm Hash digest
SHA256 040aa1208303e82c7ee3452b9e79ff0a13a3afbf48a4aa95c3c9f045fe06829d
MD5 f82582fdb88f6b3b9375e1aef84e9db6
BLAKE2b-256 5fd69fff042df612861583eb23dc1051efdca13e1d54fb4299eb62efb2b590a0

See more details on using hashes here.

File details

Details for the file wau-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: wau-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.22 {"installer":{"name":"uv","version":"0.11.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for wau-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 6fd4952e40b819b782950d17c0d80b0eaccaf6771e4ba61131dc29cd3f8baae8
MD5 6bdc5a6911e662bc86edc2b5cfc6802c
BLAKE2b-256 37445ab02b16b43907a26fa0d1536dedbdbc2b984bc5da9c35fc4f358ba0c46c

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