From 0c00a066347597e2131f444ae88ff3ed74d3c113 Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Sat, 25 Mar 2023 02:14:12 +0000 Subject: [PATCH] docs: Add docs for v0.26.0 --- docs/features.md | 48 ++++++++++++++++++++++++++++--------- docs/graphql-integration.md | 4 ++-- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/docs/features.md b/docs/features.md index 195095a48..0637cd543 100644 --- a/docs/features.md +++ b/docs/features.md @@ -37,7 +37,7 @@ async def h(request): ```python @app.post("/post") async def postreq(request): - return bytearray(request["body"]).decode("utf-8") + return request.body ``` #### PUT Request @@ -45,7 +45,7 @@ async def postreq(request): ```python @app.put("/put") async def postreq(request): - return bytearray(request["body"]).decode("utf-8") + return request.body ``` #### PATCH Request @@ -53,7 +53,7 @@ async def postreq(request): ```python @app.patch("/patch") async def postreq(request): - return bytearray(request["body"]).decode("utf-8") + return request.body ``` #### DELETE Request @@ -61,7 +61,7 @@ async def postreq(request): ```python @app.delete("/delete") async def postreq(request): - return bytearray(request["body"]).decode("utf-8") + return request.body ``` #### Directory Serving @@ -133,6 +133,20 @@ async def response(request): return Response(status_code=200, headers={}, body="OK") ``` +#### Status Codes + +Robyn provides `StatusCodes` if you want to return type safe Status Responses. + +```python + +from robyn import StatusCodes + + +@app.get("/response") +async def response(request): + return Response(status_code=StatusCodes.HTTP_200_OK.value, headers={}, body="OK") +``` + #### Returning a byte response You can also return byte response when serving HTTP requests using the following way @@ -210,6 +224,16 @@ async def response_headers(): } ``` + +Additionally, you can access headers for per route. + +```python +@app.get("/test-headers") +def sync_before_request(request: Request): + request.headers["test"] = "we are modifying the request headers in the middle of the request!" + print(rquest) +``` + ## Query Params You can access query params from every HTTP method. @@ -326,13 +350,15 @@ You can use both sync and async functions for middlewares! ```python @app.before_request("/") -async def hello_before_request(request): +async def hello_before_request(request: Request): + request.headers["before"] = "sync_before_request" print(request) @app.after_request("/") -def hello_after_request(request): - print(request) +def hello_after_request(response: Response): + response.headers["after"] = "sync_after_request" + print(response) ``` ## MultiCore Scaling @@ -409,7 +435,7 @@ def sample_view(): return "Hello, world!" def post(request): - body = bytearray(request["body"]).decode("utf-8") + body = request.body return {"status_code": 200, "body": body} ``` @@ -425,7 +451,7 @@ def sync_decorator_view(): return "Hello, world!" def post(request): - body = bytearray(request["body"]).decode("utf-8") + body = request.body return {"status_code": 200, "body": body} @@ -435,7 +461,7 @@ def async_decorator_view(): return "Hello, world!" async def post(request): - body = bytearray(request["body"]).decode("utf-8") + body = request.body return {"status_code": 200, "body": body} ``` @@ -449,7 +475,7 @@ def View(): return "Hello, world!" async def post(request): - body = bytes(request["body"]).decode("utf-8") + body = request.body return { "status": 200, "body": body, diff --git a/docs/graphql-integration.md b/docs/graphql-integration.md index 3e5ef556c..d03060252 100644 --- a/docs/graphql-integration.md +++ b/docs/graphql-integration.md @@ -60,7 +60,7 @@ async def get(): @app.post("/") async def post(request): - body = json.loads(bytearray(request["body"]).decode("utf-8")) + body = json.loads(request.body) query = body["query"] variables = body.get("variables", None) context_value = {"request": request} @@ -140,7 +140,7 @@ We are populating the html page with the GraphiQL IDE using `strawberry`. We are ```python @app.post("/") async def post(request): - body = json.loads(bytearray(request["body"]).decode("utf-8")) + body = json.loads(request.body) query = body["query"] variables = body.get("variables", None) context_value = {"request": request}