Skip to main content

An automatic code API generator for Flask and JS.

Project description

Sharp

Sharp is an automatic API generation library for Python Flask and JavaScript.

You write functions in the backend, and Sharp generates the necessary JavaScript to make API calls to those functions. Sharp also validates argument types based on Python type hints,

@sharp.function()
def add(a: int, b: int):
    return a + b

sharp.generate("api.js")

Now api.js can be used from a JavaScript client,

const result = await API.add(4, 3);

Install

pip install Flask-Sharp

Basic Example

Server-side code,

app = Flask(__name__)
sharp = Sharp(app)

@sharp.function()
def add(a: int, b: int):
    return a + b

Sharp can be combined with a modern frontend framework and any web asset bundler, the code-gen is emmitted to a standalone JavaScript file,

sharp.generate("src/js/sharp.gen.js")

And your front-end JavaScript is automatically generated and can be used as,

const result = await API.add(4, 3);

On the backend Sharp will automatically validate that both a and b parameters are valid ints.

All Sharp messages are in plain JSON, producing human-readable messages and API routes.

For a more fully fledged example, please see the example/ directory.

Naming Routes

By default Sharp will try to name routes by combining the prefix with the function name. However, Sharp can also name routes based on filenames and any arbitrary custom naming function,

from sharp import Sharp, naming

srp = Sharp(app, prefix="/api", naming=naming.file_based)

In this example, Sharp will name a function names add in a file called math.py as /api/math/add.

Type Checking

Sharp can verify basic Python type-hints and deal with default variables,

@srp.function()
def repeat(name: str, times: int = 5):
    return name * times


@srp.function()
def reverse(names: List[str]):
    return names[::-1]

Errors

If Sharp notices a missing parameter or a wrong type, it will generate a JSON response body {"error": "Message"} with a response code 400. To throw errors within a sharp function,

from sharp.error import error

@sharp.function()
def add(a: int, b: int):
    if a < 0:
        return error("a cannot be negative.")

    return a + b

All API calls from the client return a promise. Errors can be caught with a try-catch or a catch promise callback.

API.add(-4, 3).then((r) => {
    // r is the result of the function.
}).catch((e) => {
    // e is the error message.
});

License

Copyright (c) 2020 Shreyas Kapur. Released under MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

Flask_Sharp-0.0.7-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file Flask_Sharp-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: Flask_Sharp-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/1.6.0 pkginfo/1.7.0 requests/2.21.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.6.10

File hashes

Hashes for Flask_Sharp-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 78a54f5102e061f759115c8b85e10cd666571ce28acf4e04d5913f3b4d326b6b
MD5 5d55293b042056f09eb09074dd65c327
BLAKE2b-256 8183744f0808c78488daba1811af90bdeabd19935f4f460fde625ca08ba88100

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