Get a simple HTTP (REST) API with only this simple decorator : @bonapity !
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 ๐.
โ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/
!
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.
โI think the greatest way to learn is to learn by someoneโs example.โ
โ Tobey Maguire
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)
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"
Generated documentation for your first function is available at this url : http://localhost:8888/help/add
.
For more examples with explanations take a look to the examples/
dir.
Recommended order for reading :
simplest.py
.simplest.html
.np_server_api.py
, np_client_api.py
.python_server.py
, python_client.py
.โ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.
/myfun?param1=value1¶m2=value2
.v=True
is python but will be interpreted as the boolean True
as if v=true
which is the JSON encoding). Note that if your value is a string, it should be surrounded by "
./myfun?MY_BASE64_ENCODED_DATA
.
You can pass complex non JSON serializable python type by encoding in base64 the binary pickle dump representation of your data./myfun?v=23
would be /myfun?gAN9cQBYAQAAAHZxAUsXcy4=
because gAN9cQBYAQAAAHZxAUsXcy4=
the base64 of \x80\x03}q\x00X\x01\x00\x00\x00vq\x01K\x17s.
which is the pickle dump of {'v': 23}
.requests
module, be aware to not write requests.post(url, data={...})
but requests.post(url, json={...})
instead, or specifying the Content-Type in the header.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.
โIโll probably will do it, maybe definitelyโ
โ Donald J. Trump
POST
supportnumpy.ndarrays
as parameterPUT
, DELETE
, PATCH
http://localhost:8888/help/?js
)fetch
do not handle cookiesmyfun/<paramname1>/
)โWe protect monopolies with copyright.โ
โ Peter Thiel