Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert back resp.url, introduce resp.url_obj #1295

Merged
merged 2 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ CHANGES
* Drop old-style routes: `Route`, `PlainRoute`, `DynamicRoute`,
`StaticRoute`, `ResourceAdapter`.

-
- Revert `resp.url` back to `str`, introduce `resp.url_obj` #1292

-

Expand Down
2 changes: 1 addition & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _request(self, method, url, *,
except OSError as exc:
raise aiohttp.ClientOSError(*exc.args) from exc

self._cookie_jar.update_cookies(resp.cookies, resp.url)
self._cookie_jar.update_cookies(resp.cookies, resp.url_obj)

# redirects
if resp.status in (301, 302, 303, 307) and allow_redirects:
Expand Down
22 changes: 18 additions & 4 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def send(self, writer, reader):
self.write_bytes(request, reader), loop=self.loop)

self.response = self.response_class(
self.method, self.url, self.url.host,
self.method, self.url,
writer=self._writer, continue100=self._continue,
timeout=self._timeout)
self.response._post_init(self.loop)
Expand Down Expand Up @@ -512,13 +512,12 @@ class ClientResponse:
_loop = None
_closed = True # to allow __del__ for non-initialized properly response

def __init__(self, method, url, host='', *, writer=None, continue100=None,
def __init__(self, method, url, *, writer=None, continue100=None,
timeout=5*60):
assert isinstance(url, URL)

self.method = method
self.url = url
self.host = host
self._url_obj = url
self._content = None
self._writer = writer
self._continue = continue100
Expand All @@ -527,6 +526,21 @@ def __init__(self, method, url, host='', *, writer=None, continue100=None,
self._history = ()
self._timeout = timeout

@property
def url_obj(self):
return self._url_obj

@property
def url(self):
return str(self._url_obj)

@property
def host(self):
warnings.warn("Deprecated, use .url_obj.host",
DeprecationWarning,
stacklevel=2)
return self._url_obj.host

def _post_init(self, loop):
self._loop = loop
if loop.get_debug():
Expand Down
12 changes: 4 additions & 8 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1195,23 +1195,19 @@ Response object

HTTP status reason of response (:class:`str`), e.g. ``"OK"``.

.. attribute:: host

Host part of requested url (:class:`str`).

.. attribute:: method

Request's method (:class:`str`).

.. attribute:: url

URL of request (:class:`~yarl.URL`).
URL of request (:class:`str`).

.. versionchanged:: 1.1
.. attribute:: url_obj

The attribute is :class:`~yarl.URL` now instead of :class:`str`.
URL of request (:class:`~yarl.URL`).

For giving a string use ``str(resp.url)``.
.. versionadded:: 1.1

.. attribute:: connection

Expand Down
4 changes: 2 additions & 2 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def handler_ok(request):
resp = yield from client.get('/redirect')
try:
assert resp.status == 200
assert resp.url.path == '/ok'
assert resp.url_obj.path == '/ok'
finally:
yield from resp.release()

Expand All @@ -408,7 +408,7 @@ def handler_ok(request):
resp = yield from client.get('/ok#fragment')
try:
assert resp.status == 200
assert resp.url.path == '/ok'
assert resp.url_obj.path == '/ok'
finally:
yield from resp.release()

Expand Down
1 change: 0 additions & 1 deletion tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,6 @@ class CustomRequest(ClientRequest):
def send(self, writer, reader):
resp = self.response_class(self.method,
self.url,
self.host,
writer=self._writer,
continue100=self._continue)
resp._post_init(self.loop)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,8 @@ def test_raise_for_status_4xx(self):
self.response.raise_for_status()
self.assertEqual(str(cm.exception.code), '409')
self.assertEqual(str(cm.exception.message), "CONFLICT")

def test_resp_host(self):
response = ClientResponse('get', URL('http://del-cl-resp.org'))
with self.assertWarns(DeprecationWarning):
self.assertEqual('del-cl-resp.org', response.host)