소스 검색

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:


불러오는 중...
취소
저장