Add ASGI documentation

This commit is contained in:
Adam Hopkins
2019-06-18 09:57:42 +03:00
parent ab706dda7d
commit fb61834a2e
4 changed files with 63 additions and 16 deletions

View File

@@ -1393,6 +1393,9 @@ class Sanic:
# -------------------------------------------------------------------- #
async def __call__(self, scope, receive, send):
"""To be ASGI compliant, our instance must be a callable that accepts
three arguments: scope, receive, send. See the ASGI reference for more
details: https://asgi.readthedocs.io/en/latest/"""
self.asgi = True
asgi_app = await ASGIApp.create(self, scope, receive, send)
await asgi_app()

View File

@@ -260,7 +260,6 @@ class ASGIApp:
message = await self.transport.receive()
chunk = message.get("body", b"")
await self.request.stream.put(chunk)
# self.sanic_app.loop.create_task(self.request.stream.put(chunk))
more_body = message.get("more_body", False)
@@ -288,7 +287,6 @@ class ASGIApp:
headers = [
(str(name).encode("latin-1"), str(value).encode("latin-1"))
for name, value in response.headers.items()
# if name not in ("Set-Cookie",)
]
except AttributeError:
logger.error(

View File

@@ -183,6 +183,14 @@ class SanicASGIAdapter(requests.asgi.ASGIAdapter):
*args: typing.Any,
**kwargs: typing.Any,
) -> requests.Response:
"""This method is taken MOSTLY verbatim from requests-asyn. The
difference is the capturing of a response on the ASGI call and then
returning it on the response object. This is implemented to achieve:
request, response = await app.asgi_client.get("/")
You can see the original code here:
https://github.com/encode/requests-async/blob/614f40f77f19e6c6da8a212ae799107b0384dbf9/requests_async/asgi.py#L51""" # noqa
scheme, netloc, path, query, fragment = urlsplit(
request.url
) # type: ignore
@@ -345,9 +353,6 @@ class SanicASGITestClient(requests.ASGISession):
self.app = app
self.base_url = base_url
# async def send(self, prepared_request, *args, **kwargs):
# return await super().send(*args, **kwargs)
async def request(self, method, url, gather_request=True, *args, **kwargs):
self.gather_request = gather_request
print(url)