Skip to content

Commit eeadae1

Browse files
committed
Fix flake8 errors and URL problem
1 parent 3fb2b0c commit eeadae1

2 files changed

Lines changed: 18 additions & 22 deletions

File tree

wayback/_client.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,16 @@
2828
# Timeout)
2929
import time
3030
from typing import Generator, Optional
31-
from urllib.parse import urljoin, urlparse
31+
from urllib.parse import urlencode, urljoin, urlparse
3232
from urllib3 import PoolManager, HTTPResponse, Timeout as Urllib3Timeout
3333
from urllib3.connectionpool import HTTPConnectionPool
34-
from urllib3.exceptions import (ClosedPoolError,
35-
ConnectTimeoutError,
34+
from urllib3.exceptions import (ConnectTimeoutError,
3635
DecodeError,
3736
MaxRetryError,
3837
ProtocolError,
3938
ReadTimeoutError,
4039
ProxyError,
41-
TimeoutError,
42-
ProtocolError)
40+
TimeoutError)
4341
from warnings import warn
4442
from . import _utils, __version__
4543
from ._models import CdxRecord, Memento
@@ -350,7 +348,8 @@ def iter_byte_slices(data: bytes, size: int) -> Generator[bytes, None, None]:
350348
def parse_header_links(value):
351349
"""Return a list of parsed link headers proxies.
352350
353-
i.e. Link: <http:/.../front.jpeg>; rel=front; type="image/jpeg",<http://.../back.jpeg>; rel=back;type="image/jpeg"
351+
i.e. Link: <http:/.../front.jpeg>; rel=front; type="image/jpeg",
352+
<http://.../back.jpeg>; rel=back;type="image/jpeg"
354353
355354
:rtype: list
356355
"""
@@ -384,7 +383,6 @@ def parse_header_links(value):
384383
return links
385384

386385

387-
from urllib.parse import urlencode
388386
# XXX: pretty much wholesale taken from requests. May need adjustment.
389387
# https://github.com/psf/requests/blob/147c8511ddbfa5e8f71bbf5c18ede0c4ceb3bba4/requests/models.py#L107-L134
390388
def serialize_querystring(data):
@@ -441,7 +439,7 @@ def _parse_content_type_header(header):
441439
index_of_equals = param.find("=")
442440
if index_of_equals != -1:
443441
key = param[:index_of_equals].strip(items_to_strip)
444-
value = param[index_of_equals + 1 :].strip(items_to_strip)
442+
value = param[index_of_equals + 1:].strip(items_to_strip)
445443
params_dict[key.lower()] = value
446444
return content_type, params_dict
447445

@@ -503,7 +501,7 @@ def __init__(self, raw: HTTPResponse, request_url: str) -> None:
503501
self.raw = raw
504502
self.status_code = raw.status
505503
self.headers = raw.headers
506-
self.url = getattr(raw, 'url', request_url)
504+
self.url = urljoin(request_url, getattr(raw, 'url', ''))
507505
self.encoding = get_encoding_from_headers(self.headers)
508506

509507
# XXX: shortcut to essentially what requests does in `iter_content()`.
@@ -539,9 +537,7 @@ def stream(self, chunk_size: int = 10 * 1024) -> Generator[bytes, None, None]:
539537
@property
540538
def content(self) -> bytes:
541539
if self._content is None:
542-
logger.warning(f'Getting content!!!')
543540
self._content = b"".join(self.stream()) or b""
544-
logger.warning(f'Getting content DONE: "{self._content}"')
545541

546542
return self._content
547543

@@ -612,7 +608,7 @@ def close(self, cache: bool = True) -> None:
612608
if self.raw:
613609
try:
614610
if cache:
615-
# Inspired by requests: https://github.com/psf/requests/blob/eedd67462819f8dbf8c1c32e77f9070606605231/requests/sessions.py#L160-L163
611+
# Inspired by requests: https://github.com/psf/requests/blob/eedd67462819f8dbf8c1c32e77f9070606605231/requests/sessions.py#L160-L163 # noqa
616612
try:
617613
self.content
618614
except (DecodeError, ProtocolError, RuntimeError):
@@ -838,15 +834,17 @@ def send(self, method, url, *, params=None, allow_redirects=True, timeout=-1) ->
838834
response.close(cache=False)
839835
# XXX: urllib3's MaxRetryError can wrap all the other errors, so
840836
# we should probably be checking `error.reason` on it. See how
841-
# requests handles this: https://github.com/psf/requests/blob/a25fde6989f8df5c3d823bc9f2e2fc24aa71f375/src/requests/adapters.py#L502-L537
837+
# requests handles this:
838+
# https://github.com/psf/requests/blob/a25fde6989f8df5c3d823bc9f2e2fc24aa71f375/src/requests/adapters.py#L502-L537
842839
#
843840
# XXX: requests.RetryError used to be in our list of handleable
844841
# errors; it gets raised when urllib3 raises a MaxRetryError with a
845842
# ResponseError for its `reason` attribute. Need to test the
846843
# situation here...
847844
#
848845
# XXX: Consider how read-related exceptions need to be handled (or
849-
# not). In requests: https://github.com/psf/requests/blob/a25fde6989f8df5c3d823bc9f2e2fc24aa71f375/src/requests/models.py#L794-L839
846+
# not). In requests:
847+
# https://github.com/psf/requests/blob/a25fde6989f8df5c3d823bc9f2e2fc24aa71f375/src/requests/models.py#L794-L839
850848
except WaybackSession.handleable_errors as error:
851849
response = getattr(error, 'response', None)
852850
if response is not None:

wayback/tests/test_client.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
from datetime import date, datetime, timezone, timedelta
2+
from io import BytesIO
23
from itertools import islice
34
from pathlib import Path
45
import time
56
import pytest
67
from unittest import mock
8+
from urllib.parse import urlparse, ParseResult, parse_qs
9+
from urllib3 import (HTTPConnectionPool,
10+
HTTPResponse,
11+
HTTPHeaderDict,
12+
Timeout as Urllib3Timeout)
713
from .support import create_vcr
814
from .._client import (CdxRecord,
915
Mode,
@@ -217,10 +223,6 @@ def test_search_with_filter_tuple():
217223
assert all(('feature' in v.url for v in versions))
218224

219225

220-
from io import BytesIO
221-
from urllib.parse import urlparse, ParseResult, parse_qs
222-
from urllib3 import HTTPConnectionPool, HTTPResponse, HTTPHeaderDict
223-
import logging
224226
class Urllib3MockManager:
225227
def __init__(self) -> None:
226228
self.responses = []
@@ -769,7 +771,6 @@ def return_timeout(self, *args, **kwargs) -> HTTPResponse:
769771
>>> def test_timeout(self, mock_class):
770772
>>> assert urllib3.get('http://test.com', timeout=5).data == b'5'
771773
"""
772-
logging.warning(f'Called with args={args}, kwargs={kwargs}')
773774
res = HTTPResponse(
774775
body=str(kwargs.get('timeout', None)).encode(),
775776
headers=HTTPHeaderDict(),
@@ -778,9 +779,6 @@ def return_timeout(self, *args, **kwargs) -> HTTPResponse:
778779
return res
779780

780781

781-
from urllib3 import Timeout as Urllib3Timeout
782-
783-
784782
class TestWaybackSession:
785783
def test_request_retries(self, urllib3_mock):
786784
urllib3_mock.get('http://test.com', [{'text': 'bad1', 'status_code': 503},

0 commit comments

Comments
 (0)