bonapity

Get a simple HTTP (REST) API with only this simple decorator : @bonapity !

๐Ÿ‘€ README ๐Ÿ‘€

bonAPIty is a python (3.7) package allowing you to create simple API (REST) for your functions without writing any complicated line of server code and it is much simpler than Flask !

You are the ๐Ÿ‘จโ€๐Ÿณ, just do what you do the best: cook code ! Do not lose your time to ๐Ÿ’, we do it for you :).

By type hinting your code we cast the received inputs to the right type, so do not worry about it ๐Ÿ˜€.

Install

โ€œSee, unlike most hackers, I get little joy out of figuring out how to install the latest toy.โ€

โ€“ Jamie Zawinski

Install it with pip3 install bonapity and take a look at examples/ !

Current support

Your API is REST compliant (accept the following verbs : GET, POST, PUT, PATCH, DELETE) !

For each function, your API will return your computation as a JSON object if your return type is JSON serializable, or in the pickle format otherwise. For example, numpy.ndarray objects are not JSON serializable and will be returned as a binary pickle dump.

If you want to learn more about how itโ€™s working, take a loot to the โ€œHow Toโ€ section.

Example

โ€œI think the greatest way to learn is to learn by someoneโ€™s example.โ€

โ€“ Tobey Maguire

Writting your first API

from bonapity import bonapity

@bonapity
def add(a: int, b: int) -> int:
    """ Add two numbers. """
    return a + b

if __name__ == "__main__":
    bonapity.serve(port=8888)

Requesting your first API

Testing REST verbs requests with curl :

curl -X GET "http://localhost:8888/add?a=1&b=3"
curl -X DELETE "http://localhost:8888/add?a=1&b=3"
curl --data '{"a": 1, "b": 3}' -X POST   "http://localhost:8888/add" -H "Content-Type: application/json"
curl --data '{"a": 1, "b": 3}' -X PATCH  "http://localhost:8888/add" -H "Content-Type: application/json"
curl --data '{"a": 1, "b": 3}' -X PUT    "http://localhost:8888/add" -H "Content-Type: application/json"

Looking your first generated API function documentation

Generated documentation for your first function is available at this url : http://localhost:8888/help/add.

More Examples

For more examples with explanations take a look to the examples/ dir.
Recommended order for reading :

How To

โ€œI am not accustomed to protocol.โ€

โ€“ Evo Morales

We currently support 2 kinds of requests : GET, DELETE vs. POST, PUT, PATCH. Each of them accept 2 different ways to get your data/parameters sent from the client to the your API.

But remember, we process those encoded data and present them to your code as if they were not received from a complex protocol, everything is transparent for you !

You donโ€™t need to read this section for a quick start, just use the generated wrappers in the generated documentation of your API (run the server and go to /help/), read this only if you want to get your hands dirty and not use the generated wrapper but write your one instead.

GET, DELETE

POST, PUT, PATCH

Goodies

By default, each served function will have an documentation page automatically build from your doctring and the signature of the function available at http:/domain:port/help/myfunction.

Furthermore, to make your life easier when writting a JS client, we automatically generate wrapper of all your function into a single JS script you can include from this address: http://domain:port/help/?js. The generation is quite naรฏve and just an help, if you encounter difficulties with the generated lib, consider to writte wrappers for your spetial use cases your self.

In Development

โ€œIโ€™ll probably will do it, maybe definitelyโ€

โ€“ Donald J. Trump

License CC-BY

โ€œWe protect monopolies with copyright.โ€

โ€“ Peter Thiel

You are free to

Under the following terms