Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add docs for v0.26.0 #451

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,31 @@ async def h(request):
```python
@app.post("/post")
async def postreq(request):
return bytearray(request["body"]).decode("utf-8")
return request.body
```

#### PUT Request

```python
@app.put("/put")
async def postreq(request):
return bytearray(request["body"]).decode("utf-8")
return request.body
```

#### PATCH Request

```python
@app.patch("/patch")
async def postreq(request):
return bytearray(request["body"]).decode("utf-8")
return request.body
```

#### DELETE Request

```python
@app.delete("/delete")
async def postreq(request):
return bytearray(request["body"]).decode("utf-8")
return request.body
```

#### Directory Serving
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
```

Expand All @@ -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}


Expand All @@ -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}
```

Expand All @@ -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,
Expand All @@ -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
```


4 changes: 2 additions & 2 deletions docs/graphql-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand Down
31 changes: 31 additions & 0 deletions robyn/robyn.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down