LiteStar install
- python -m pip install litestar
- once, done, you can run in the project dir, (where app.py is)
litestar run
- if error module missing, just pip install those
- rich
- click
- uvicorn
- jsbeautifier
LiteStar syntax
- after build a simple api, to interact with that web api, sample code
- get
response = requests.get("http://127.0.0.1:8000/?done=true")
- post
response = requests.post("http://127.0.0.1:8000/",json={"title":"car-washing","done":False})
- put
response = requests.put("http://127.0.0.1:8000/profiting", json={"title":"profiting2","done":True}
- read
data = response.json()
- note:
- in json, true is “true”
- in python, true is “True”
- in http url, true is ok with “true” or “True”
Concepts
- it provides a active server, allow other pc or program to communicate with it
- it use url to point to each function of the app.py
# (python decorator: a function repackage method, put below function as a component of decorator function) # so, the decoration function is like a white-label procedure function, need input function as lego part to output different behaved white-label funciton. @get("/") async def func_a() -> str: # same as below, get("/") return a the get white-label function, and take func_a as component to output a different behaved white-label function, and use the same function variable func_a, (like a repackage of func_a in white-label box) func_a = get("/")(func_a)
- ASGI: Asynchronous Server Gateway Interface
- newer for python, non block commu
- WSGI: Web Server Gateway Interface
- litestar use Uvicorn ASGI server, which support http, websockets and other realtime protocol
- litestar auto create the scheme and scheme tryout page
- Litestar generates an OpenAPI schema based on the route handlers, which can then be used by Swagger to set up the interactive documentation.