jenkins-bot submitted this change.
[cleanup] rename HttpRequest properties to request.Response identifiers
HttpRequest properties and methods should be renamed to the corresponding
requests.Resonse methods/properties
HttpRequest:
- rename HttpRequest.headers attribute to all_headers
- rename HttpRequest.status property to status_code
- rename HttpRequest.uri to url
- show a FutureWarning for HttpRequest.content property
because it is needed for raw
- deprecate HttpRequest.parsed_uri and hostname
- deprecate HttpRequest.__str__ and HttpRequest.__bytes__
- update other files accordingly
Bug: T265206
Change-Id: I3f52e045364cf540254208b05e1c149de4d69646
---
M pywikibot/comms/http.py
M pywikibot/comms/threadedhttp.py
M pywikibot/page/__init__.py
M pywikibot/pagegenerators.py
M tests/aspects.py
M tests/http_tests.py
6 files changed, 98 insertions(+), 43 deletions(-)
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index fc201d7..8aa371a 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -298,7 +298,7 @@
uri = http_request.uri
params = http_request.params
body = http_request.body
- headers = http_request.headers
+ headers = http_request.all_headers
auth = get_authentication(uri)
if auth is not None and len(auth) == 4:
if isinstance(requests_oauthlib, ImportError):
@@ -337,20 +337,21 @@
if SSL_CERT_VERIFY_FAILED_MSG in str(request.data):
raise FatalServerError(str(request.data))
- if request.status == 504:
- raise Server504Error('Server %s timed out' % request.hostname)
+ if request.status_code == 504:
+ raise Server504Error('Server {} timed out'
+ .format(urlparse(request.url).netloc))
- if request.status == 414:
+ if request.status_code == 414:
raise Server414Error('Too long GET request')
if isinstance(request.data, Exception):
- error('An error occurred for uri ' + request.uri)
+ error('An error occurred for uri ' + request.url)
raise request.data from None
# HTTP status 207 is also a success status for Webdav FINDPROP,
# used by the version module.
- if request.status not in (200, 207):
- warning('Http response status {0}'.format(request.data.status_code))
+ if request.status_code not in (200, 207):
+ warning('Http response status {}'.format(request.status_code))
@deprecated(since='20201015', future_warning=True)
diff --git a/pywikibot/comms/threadedhttp.py b/pywikibot/comms/threadedhttp.py
index 8b4338e..d706d04 100644
--- a/pywikibot/comms/threadedhttp.py
+++ b/pywikibot/comms/threadedhttp.py
@@ -12,7 +12,7 @@
from urllib.parse import urlparse
import pywikibot
-from pywikibot.tools import deprecated
+from pywikibot.tools import deprecated, deprecated_args
_logger = 'comms.threadedhttp'
@@ -27,43 +27,76 @@
* an exception
"""
- def __init__(self, uri, method='GET', params=None, body=None, headers=None,
- callbacks=None, charset=None, **kwargs):
+ @deprecated_args(headers='all_headers', uri='url')
+ def __init__(self, url, method='GET', params=None, body=None,
+ all_headers=None, callbacks=None, charset=None, **kwargs):
"""Initializer.
See C{Http.request} for parameters.
"""
- self.uri = uri
+ self.url = url
self.method = method
self.params = params
self.body = body
- self.headers = headers
+ self.all_headers = all_headers
if isinstance(charset, codecs.CodecInfo):
self.charset = charset.name
elif charset:
self.charset = charset
- elif headers and 'accept-charset' in headers:
- self.charset = headers['accept-charset']
+ elif all_headers and 'accept-charset' in all_headers:
+ self.charset = all_headers['accept-charset']
else:
self.charset = None
self.callbacks = callbacks
- self.args = [uri, method, body, headers]
+ self.args = [url, method, body, all_headers]
self.kwargs = kwargs
self._parsed_uri = None
self._data = None
@property
+ @deprecated('the `url` attribute', since='20201011', future_warning=True)
+ def uri(self):
+ """DEPRECATED. Return the response URL."""
+ return self.url
+
+ @uri.setter
+ @deprecated('the `url` attribute', since='20201011', future_warning=True)
+ def uri(self, value):
+ """DEPRECATED. Set the response URL."""
+ self.url = value
+
+ @property
+ @deprecated('the `all_headers` property', since='20201011',
+ future_warning=True)
+ def headers(self):
+ """DEPRECATED. Return the response headers."""
+ return self.all_headers
+
+ @headers.setter
+ @deprecated('the `all_headers` property', since='20201011',
+ future_warning=True)
+ def headers(self, value):
+ """DEPRECATED. Set the response headers."""
+ self.all_headers = value
+
+ @property
def data(self):
- """Return the requests response tuple."""
+ """DEPRECATED. Return the requests response tuple.
+
+ @note: This property will removed.
+ """
assert(self._data is not None)
return self._data
@data.setter
def data(self, value):
- """Set the requests response and invoke each callback."""
+ """DEPRECATED. Set the requests response and invoke each callback.
+
+ @note: This property setter will removed.
+ """
self._data = value
if self.callbacks:
@@ -72,12 +105,18 @@
@property
def exception(self) -> Optional[Exception]:
- """Get the exception, if any."""
+ """DEPRECATED. Get the exception, if any.
+
+ @note: This property will removed.
+ """
return self.data if isinstance(self.data, Exception) else None
@property
def response_headers(self) -> Optional[Dict[str, str]]:
- """Return the response headers."""
+ """DEPRECATED. Return the response headers.
+
+ @note: This property will renamed to headers.
+ """
return self.data.headers if not self.exception else None
@property
@@ -86,23 +125,31 @@
return self.data.content if not self.exception else None
@property
+ @deprecated('urlparse(HttpRequest.url)',
+ since='20201011', future_warning=True)
def parsed_uri(self):
- """Return the parsed requested uri."""
+ """DEPRECATED. Return the parsed requested uri."""
if not self._parsed_uri:
self._parsed_uri = urlparse(self.uri)
return self._parsed_uri
@property
+ @deprecated('urlparse(HttpRequest.url).netloc',
+ since='20201011', future_warning=True)
def hostname(self):
- """Return the host of the request."""
+ """DEPRECATED. Return the host of the request."""
return self.parsed_uri.netloc
@property
- def status(self):
- """Return the HTTP response status.
+ @deprecated('the `status_code` property', since='20201011',
+ future_warning=True)
+ def status(self) -> Optional[int]:
+ """DEPRECATED. Return the HTTP response status."""
+ return self.status_code
- @rtype: int
- """
+ @property
+ def status_code(self) -> Optional[int]:
+ """Return the HTTP response status."""
return self.data.status_code if not self.exception else None
@property
@@ -173,25 +220,32 @@
result = encoding
return result
+ @deprecated('the `text` property', since='20201011', future_warning=True)
def decode(self, encoding, errors='strict') -> str:
"""Return the decoded response."""
return self.raw.decode(encoding, errors)
@property
- @deprecated('the `text` property', since='20180321')
+ @deprecated('the `text` property', since='20180321', future_warning=True)
def content(self) -> str:
- """DEPRECATED. Return the response decoded by the detected encoding."""
+ """DEPRECATED. Return the response decoded by the detected encoding.
+
+ @note: The behaviour will be changed.
+ This method will return the content of the response in bytes.
+ """
return self.text
@property
def text(self) -> str:
"""Return the response decoded by the detected encoding."""
- return self.decode(self.encoding)
+ return self.raw.decode(self.encoding)
+ @deprecated('the `text` property', since='20201011', future_warning=True)
def __str__(self) -> str:
"""Return the response decoded by the detected encoding."""
return self.text
+ @deprecated(since='20201011', future_warning=True)
def __bytes__(self) -> Optional[bytes]:
"""Return the undecoded response."""
return self.raw
diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py
index 55c9a23..48c1499 100644
--- a/pywikibot/page/__init__.py
+++ b/pywikibot/page/__init__.py
@@ -2645,7 +2645,8 @@
return sha1 == revision.sha1
else:
pywikibot.warning(
- 'Unsuccessfull request (%s): %s' % (req.status, req.uri))
+ 'Unsuccessfull request ({}): {}'
+ .format(req.status_code, req.url))
return False
def globalusage(self, total=None):
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py
index b301109..c29c21c 100644
--- a/pywikibot/pagegenerators.py
+++ b/pywikibot/pagegenerators.py
@@ -2933,10 +2933,10 @@
req = http.fetch(url, params=self.opts)
except ReadTimeout:
raise ServerError(
- 'received ReadTimeout from {0}'.format(url))
+ 'received ReadTimeout from {}'.format(url))
if 500 <= req.status < 600:
raise ServerError(
- 'received {0} status from {1}'.format(req.status, req.uri))
+ 'received {} status from {}'.format(req.status_code, req.url))
j = json.loads(req.text)
raw_pages = j['*'][0]['a']['*']
yield from raw_pages
diff --git a/tests/aspects.py b/tests/aspects.py
index 24fb72c..2ab9f6e 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -505,8 +505,9 @@
if r.exception:
e = r.exception
else:
- if r.status not in [200, 301, 302, 303, 307, 308]:
- raise ServerError('HTTP status: {}'.format(r.status))
+ if r.status_code not in {200, 301, 302, 303, 307, 308}:
+ raise ServerError('HTTP status: {}'
+ .format(r.status_code))
except Exception as e2:
pywikibot.error('{}: accessing {} caused exception:'
.format(cls.__name__, hostname))
diff --git a/tests/http_tests.py b/tests/http_tests.py
index 9a8b0b7..f4f1cb4 100644
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -43,7 +43,7 @@
"""Test http._enqueue using http://www.wikipedia.org/."""
r = http._enqueue('http://www.wikipedia.org/')
self.assertIsInstance(r, threadedhttp.HttpRequest)
- self.assertEqual(r.status, 200)
+ self.assertEqual(r.status_code, 200)
self.assertIn('<html lang="mul"', r.text)
self.assertIsInstance(r.text, str)
self.assertIsInstance(r.raw, bytes)
@@ -52,11 +52,9 @@
"""Test http.fetch using http://www.wikipedia.org/."""
r = http.fetch('http://www.wikipedia.org/')
self.assertIsInstance(r, threadedhttp.HttpRequest)
- self.assertEqual(r.status, 200)
+ self.assertEqual(r.status_code, 200)
self.assertIn('<html lang="mul"', r.text)
self.assertIsInstance(r.text, str)
- with suppress_warnings(r'.*HttpRequest\.content is deprecated'):
- self.assertEqual(r.content, r.text)
self.assertIsInstance(r.raw, bytes)
@@ -614,10 +612,10 @@
def test_no_params(self):
"""Test fetch method with no parameters."""
r = http.fetch(uri=self.url, params={})
- if r.status == 503: # T203637
+ if r.status_code == 503: # T203637
self.skipTest(
'503: Service currently not available for ' + self.url)
- self.assertEqual(r.status, 200)
+ self.assertEqual(r.status_code, 200)
content = json.loads(r.text)
self.assertDictEqual(content['args'], {})
@@ -630,10 +628,10 @@
should be the same as what we get out.
"""
r = http.fetch(uri=self.url, params={'fish&chips': 'delicious'})
- if r.status == 503: # T203637
+ if r.status_code == 503: # T203637
self.skipTest(
'503: Service currently not available for ' + self.url)
- self.assertEqual(r.status, 200)
+ self.assertEqual(r.status_code, 200)
content = json.loads(r.text)
self.assertDictEqual(content['args'], {'fish&chips': 'delicious'})
@@ -646,10 +644,10 @@
should be the same as what we get out.
"""
r = http.fetch(uri=self.url, params={'fish%26chips': 'delicious'})
- if r.status == 503: # T203637
+ if r.status_code == 503: # T203637
self.skipTest(
'503: Service currently not available for ' + self.url)
- self.assertEqual(r.status, 200)
+ self.assertEqual(r.status_code, 200)
content = json.loads(r.text)
self.assertDictEqual(content['args'], {'fish%26chips': 'delicious'})
To view, visit change 633352. To unsubscribe, or for help writing mail filters, visit settings.