8000 Home · mzraly/lute-v3 Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
mzraly edited this page Mar 28, 2024 · 6 revisions

Lute API proposal

Why?

There are any number of activities a user might like to do without interacting directly with the UI:

  1. Get version and configuration data for a bug report
  2. Create a backup for export to another device
  3. Import a backup from another device
  4. Load a new story from a file or URL or from the output of another command
  5. Load term entries from a file or the output of another command
  6. Export stories and terms and generate a list of most and/or least familiar terms
  7. Export a story and generate Anki cards, audio with https://pypi.org/project/gTTS/, etc.
  8. Run a cron job that checks usage statistics and sends a notification if there has been no recent activity.

Interfaces

In order of importance:

  1. A Python API for programmers
  2. A CLI wrapping the API for common use cases
  3. A REST API for common use cases

Execution environments

All permutations of:

  1. pip install, docker install
  2. app running concurrently, app not running concurrently

Common Guidelines

  1. Versioning -- An API is a promise, and breaking changes should be minimized.
  2. Avoid exposing assumptions about the data model, field names, etc.
  3. Test, of course!

Python API Guidelines

  1. Include docstrings for each function, method, class, and module; ideally with examples.
  2. Use python-style naming for functions, methods, and class names (e.g. "my_useful_function", NOT "myUsefulFunction").
  3. Report success/failure of each operation, use custom exceptions if warranted.
  4. Add type versioning where possible, compatible with lowest supported python version.
  5. Security: guard against SQL injection.

CLI Guidelines

  1. TODO -- separate scripts, e.g. lute, lute-xxx, lute-yyy, or one script with subcommands like git? The separate scripts approach makes additional capabilities discoverable with command-completion. The subcommand approach makes them discoverable with lute help but provides a cluttered interface.
  2. Include a --help option for each command, ideally with examples.
  3. Use consistent CLI options -- For example, don't add a --debug option to one command and a --verbose option to another if they both do the same thing.
  4. Use exit codes to report CLI command status: 0 for success, 1 for command error, 129 or higher for termination by signal.
  5. Handle BrokenPipeErrors properly in CLI commands -- see https://docs.python.org/3/library/signal.html#note-on-sigpipe.

REST API Guidelines

  1. Consider swagger (https://swagger.io/) documentation.
  2. Security: TODO -- what else for a REST API?

Concerns

  1. Is SQLite concurrency control adequate for all use cases where the API is being executed through a different process than the running Lute flask application?
  2. How stable is the data model? Is now the right time to expose an API?

Random Other Stuff

  1. To install scripts, add entries to project.scripts section of pyproject.toml
    1. Current script entry is lute = "lute.main:start" but lute/main.py only defines a _start method, what's going on?
  2. To install to current python environment (likely virtualenv) use flit install command (see https://flit.pypa.io/en/latest/cmdline.html) or just pip install .
0