浏览代码

Support adding headers to individual requests

tags/v0.1.3
JustAnotherArchivist 5 年前
父节点
当前提交
ad22a2327a
共有 1 个文件被更改,包括 5 次插入2 次删除
  1. +5
    -2
      qwarc/__init__.py

+ 5
- 2
qwarc/__init__.py 查看文件

@@ -30,7 +30,7 @@ class Item:


self.childItems = [] self.childItems = []


async def fetch(self, url, responseHandler = qwarc.utils.handle_response_default, method = 'GET', data = None):
async def fetch(self, url, responseHandler = qwarc.utils.handle_response_default, method = 'GET', data = None, headers = []):
''' '''
HTTP GET or POST a URL HTTP GET or POST a URL


@@ -38,6 +38,7 @@ class Item:
responseHandler: a callable that determines how the response is handled. See qwarc.utils.handle_response_default for details. responseHandler: a callable that determines how the response is handled. See qwarc.utils.handle_response_default for details.
method: str, must be 'GET' or 'POST' method: str, must be 'GET' or 'POST'
data: dict or list/tuple of lists/tuples of length two or bytes or file-like or None, the data to be sent in the request body data: dict or list/tuple of lists/tuples of length two or bytes or file-like or None, the data to be sent in the request body
headers: list of 2-tuples, additional headers for this request only


Returns response (a ClientResponse object or None) and history (a tuple of (response, exception) tuples). Returns response (a ClientResponse object or None) and history (a tuple of (response, exception) tuples).
response can be None and history can be an empty tuple, depending on the circumstances (e.g. timeouts). response can be None and history can be an empty tuple, depending on the circumstances (e.g. timeouts).
@@ -47,6 +48,8 @@ class Item:


url = yarl.URL(url) # Explicitly convert for normalisation, percent-encoding, etc. url = yarl.URL(url) # Explicitly convert for normalisation, percent-encoding, etc.
assert method in ('GET', 'POST'), 'method must be GET or POST' assert method in ('GET', 'POST'), 'method must be GET or POST'
headers = self.headers + headers
#TODO Deduplicate headers with later values overriding earlier ones
history = [] history = []
attempt = 0 attempt = 0
#TODO redirectLevel #TODO redirectLevel
@@ -60,7 +63,7 @@ class Item:
try: try:
with _aiohttp.Timeout(60): with _aiohttp.Timeout(60):
logging.info('Fetching {}'.format(url)) logging.info('Fetching {}'.format(url))
response = await self.session.request(method, url, data = data, headers = self.headers, allow_redirects = False)
response = await self.session.request(method, url, data = data, headers = headers, allow_redirects = False)
try: try:
ret = await response.text(errors = 'surrogateescape') ret = await response.text(errors = 'surrogateescape')
except: except:


正在加载...
取消
保存