fix docs, add res.send and res.cork_send

pull/75/head
Ciro 2023-01-07 08:47:50 -03:00
rodzic e5bf0b201f
commit 14ab02f5f3
8 zmienionych plików z 406 dodań i 157 usunięć

Wyświetl plik

@ -34,7 +34,7 @@ With no precedents websocket performance and an really fast HTTP server that can
- [Templates](templates.md)
- [GraphiQL](graphiql.md)
- [WebSockets and Backpressure](websockets-backpressure.md)
- [SSL](ssl.md)
- [Plugins / Extensions](extensions.md)
- [SSL](ssl.md)
- [CLI, ASGI and WSGI](cli.md)
- [API Reference](api.md)

Wyświetl plik

@ -12,7 +12,7 @@
- [Templates](templates.md)
- [GraphiQL](graphiql.md)
- [WebSockets and Backpressure](websockets-backpressure.md)
- [SSL](ssl.md)
- [Plugins / Extensions](extensions.md)
- [SSL](ssl.md)
- [CLI, ASGI and WSGI](cli.md)
- [API Reference](api.md)

Wyświetl plik

@ -3,6 +3,7 @@
class App:
def __init__(self, options=None):
def router(self, prefix: str="", *middlewares):
def register(self, extension):
def template(self, template_engine):
def json_serializer(self, json_serializer):
@ -55,6 +56,8 @@ class AppResponse:
def get_remote_address(self):
def get_proxied_remote_address_bytes(self):
def get_proxied_remote_address(self):
def cork_send(self, message: any, content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
def send(self, message: any, content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
def end(self, message, end_connection=False):
def pause(self):
def resume(self):

Wyświetl plik

@ -7,8 +7,15 @@ This section is to show the basics of `AppResponse` and `AppRequest`
`res.write(message)` were message can be String, bytes or an Object that can be converted to json, send the message to the response without ending.
`res.cork_end(message, end_connection=False)` or `res.end(message, end_connection=False)` were message can be String, bytes or an Object that can be converted to json, send the message to the response and end the response.
The above `res.end()` or `res.cork_end()` call will actually call three separate send functions; res.write_status, res.write_header and whatever it does itself. By wrapping the call in `res.cork` or `res.cork_end` you make sure these three send functions are efficient and only result in one single send syscall and one single SSL block if using SSL.
`res.send(message, content_type=b'text/plain, status=b'200 OK', headers=None, end_connection=False)` and `res.cork_send(message, content_type=b'text/plain', status=b'200 OK', headers=None, end_connection=False)`
combines `res.write_status()`, `res.write_headers()`, and `res.end()` in a way that is easier to use, if you want to send all in one call just using named parameters. Headers can receive any iterator of iterators/tuple like `iter(tuple(str, str))` where the first value is the header name and the following the value, using `res.cork_send` will make sure to send all the
data in a corked state.
Using `res.write_continue()` writes HTTP/1.1 100 Continue as response
@ -51,9 +58,25 @@ def not_found(res, req):
res.write_status(404).end("Not Found")
def ok(res, req):
res.write_status("200 OK").end("Not Found")
res.write_status("200 OK").end("OK")
```
### Using send
```python
def not_found(res, req):
res.send("Not Found", status=404)
def ok(res, req):
res.send("OK", status="200 OK")
def json(res, req):
res.send({"Hello", "World!"})
def with_headers(res, req):
res.send({"Hello": "World!"}, headers=(("X-Rate-Limit-Remaining", "10"), (b'Another-Headers', b'Value')))
```
### Check the URL or Method
`req.get_full_url()` will return the path with query string
`req.get_url()` will return the path without query string

Wyświetl plik

@ -52,4 +52,4 @@ def extension(request, response, ws):
app.register(extension)
```
### Next [CLI, ASGI and WSGI](cli.md)
### Next [SSL](ssl.md)

Wyświetl plik

@ -31,4 +31,4 @@ class AppOptions:
ssl_ciphers: str = None,
ssl_prefer_low_memory_usage: int = 0
```
### Next [CLI Reference](cli.md)
### Next [CLI, ASGI and WSGI](cli.md)

Wyświetl plik

@ -14,12 +14,6 @@ def extension(request, response, ws):
async def get_cart(self):
return [{ "quantity": 10, "name": "T-Shirt" }]
@response.method
def send(self, content: any, content_type: str = b'text/plain', status=200):
self.write_header(b'Content-Type', content_type)
self.write_status(status)
self.end(content)
request.property("token", "testing")
# extensions must be registered before routes
@ -31,18 +25,18 @@ def auth_middleware(res, req, data):
req.token = token
return { "name": "Test" } if token else { "name", "Anonymous" }
router = app.router("", auth_middleware)
router = app.router("")
@router.get("/")
async def home(res, req, data=None):
print(data)
print("token", req.token)
cart = await req.get_cart()
print("cart", cart)
user = await req.get_user()
print("user", user)
print("token", req.token)
res.send("Hello World!")
def home(res, req, data=None):
# print(data)
# print("token", req.token)
# cart = await req.get_cart()
# print("cart", cart)
# user = await req.get_user()
# print("user", user)
# print("token", req.token)
res.send({"Hello": "World!"}, headers=(("X-Rate-Limit-Remaining", "10"), (b'Another-Headers', b'Value')))
app.listen(