add headers to HTTP Response objects and support for HTTP HEAD method

This commit is contained in:
lilydjwg 2021-01-28 17:43:09 +08:00
parent 8b9bd1ca1c
commit 2e069fe1c7
4 changed files with 18 additions and 5 deletions

View File

@ -63,7 +63,7 @@ class AiohttpSession(BaseSession):
raise err_cls(res.status, res.reason, res)
body = await res.content.read()
return Response(body)
return Response(res.headers, body)
@atexit.register
def cleanup():

View File

@ -2,7 +2,7 @@
# Copyright (c) 2019-2020 lilydjwg <lilydjwg@gmail.com>, et al.
import structlog
from typing import Optional, Dict
from typing import Optional, Dict, Mapping
import json as _json
from ..ctxvars import tries, proxy, user_agent
@ -14,8 +14,16 @@ class Response:
.. py:attribute:: body
:type: bytes
.. py:attribute:: headers
:type: Mapping[str, str]
'''
def __init__(self, body):
def __init__(
self,
headers: Mapping[str, str],
body: bytes,
) -> None:
self.headers = headers
self.body = body
def json(self):
@ -31,6 +39,11 @@ class BaseSession:
) -> None:
pass
async def head(self, *args, **kwargs):
'''Shortcut for ``HEAD`` request.'''
return await self.request(
method='HEAD', *args, **kwargs)
async def get(self, *args, **kwargs):
'''Shortcut for ``GET`` request.'''
return await self.request(

View File

@ -58,7 +58,7 @@ class HttpxSession(BaseSession):
raise TemporaryError(599, repr(e), e)
body = await r.aread()
return Response(body)
return Response(r.headers, body)
async def aclose(self):
for client in self.clients.values():

View File

@ -84,6 +84,6 @@ class TornadoSession(BaseSession):
res.code, res.reason, res
)
return Response(res.body)
return Response(res.headers, res.body)
session = TornadoSession()