diff --git a/docs/features.md b/docs/features.md index 195095a48..e33d511ed 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, @@ -469,4 +495,15 @@ app.add_view("/", View) ``` +## Populating Environment Variables + +Robyn uses a custom `robyn.env` file to populate environment variables. You can just add a `robyn.env` file in your project directory and add the environment variables you want to populate. + +```bash +# robyn.env +ROBYN_ENV=development +ROBYN_PORT=8000 +RANDOM_ENV=123 +``` + 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} diff --git a/robyn/robyn.pyi b/robyn/robyn.pyi index 936ccb747..ad591ac32 100644 --- a/robyn/robyn.pyi +++ b/robyn/robyn.pyi @@ -17,12 +17,32 @@ class FunctionInfo: @dataclass class Url: + """ + The url object passed to the route handler. + + Attributes: + scheme (str): The scheme of the url. e.g. http, https + host (str): The host of the url. e.g. localhost, + path (str): The path of the url. e.g. /user + """ + scheme: str host: str path: str @dataclass class Request: + """ + The request object passed to the route handler. + + Attributes: + queries (dict[str, str]): The query parameters of the request. e.g. /user?id=123 -> {"id": "123"} + headers (dict[str, str]): The headers of the request. e.g. {"Content-Type": "application/json"} + params (dict[str, str]): The parameters of the request. e.g. /user/:id -> {"id": "123"} + body (Union[str, bytes]): The body of the request. If the request is a JSON, it will be a dict. + method (str): The method of the request. e.g. GET, POST, PUT, DELETE + """ + queries: dict[str, str] headers: dict[str, str] params: dict[str, str] @@ -32,6 +52,17 @@ class Request: @dataclass class Response: + """ + The response object passed to the route handler. + + Attributes: + status_code (int): The status code of the response. e.g. 200, 404, 500 etc. + response_type (str): The response type of the response. e.g. text, json, html, file etc. + headers (dict[str, str]): The headers of the response. e.g. {"Content-Type": "application/json"} + body (Union[str, bytes]): The body of the response. If the response is a JSON, it will be a dict. + file_path (str): The file path of the response. e.g. /home/user/file.txt + """ + status_code: int response_type: str headers: dict[str, str]