jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/648752 )
Change subject: [IMPR] align http.fetch() params with requests ......................................................................
[IMPR] align http.fetch() params with requests
Rename 'disable_ssl_certificate_validation' to 'verify'.
Note: - 'verify' is the negated version of 'disable_ssl_certificate_validation'.
In family.py: - add verify_SSL_certificate() - deprecate ignore_certificate_error() to align logic with requests library.
Bug: T265206 Change-Id: Iaaa3b04227cbf1874dc6a542f4d3507a06ea62d6 --- M pywikibot/comms/http.py M pywikibot/family.py M tests/http_tests.py 3 files changed, 34 insertions(+), 10 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py index 1b97ee1..5a103be 100644 --- a/pywikibot/comms/http.py +++ b/pywikibot/comms/http.py @@ -37,7 +37,8 @@ from pywikibot.logging import critical, debug, error, log, warning from pywikibot.tools import ( deprecated, - deprecate_arg, + deprecated_args, + issue_deprecation_warning, file_mode_checker, PYTHON_VERSION, ) @@ -243,8 +244,14 @@ if data: body = data
- kwargs.setdefault('disable_ssl_certificate_validation', - site.ignore_certificate_error()) + kwargs.setdefault('verify', site.verify_SSL_certificate()) + old_validation = kwargs.pop('disable_ssl_certificate_validation', None) + if old_validation is not None: + issue_deprecation_warning('disable_ssl_certificate_validation', + instead='verify', + warning_class=FutureWarning, + since='20201220') + kwargs.update(verify=not old_validation)
if not headers: headers = {} @@ -314,7 +321,7 @@ warning('Http response status {}'.format(request.status_code))
-@deprecate_arg('callback', True) +@deprecated_args(callback=True) def fetch(uri, method='GET', params=None, body=None, headers=None, default_error_handling: bool = True, use_fake_user_agent: Union[bool, str] = False, @@ -333,8 +340,8 @@ to automatically chose the charset from the returned header (defaults to latin-1) @type charset: CodecInfo, str, None - @kwarg disable_ssl_certificate_validation: diable SSL Verification - @type disable_ssl_certificate_validation: bool + @kwarg verify: verify the SSL certificate (default is True) + @type verify: bool or path to certificates @kwarg callbacks: Methods to call once data is fetched @type callbacks: list of callable @rtype: L{threadedhttp.HttpRequest} @@ -397,7 +404,13 @@ auth = requests_oauthlib.OAuth1(*auth)
timeout = config.socket_timeout - ignore_validation = kwargs.pop('disable_ssl_certificate_validation', False) + old_validation = kwargs.pop('disable_ssl_certificate_validation', None) + if old_validation is not None: + issue_deprecation_warning('disable_ssl_certificate_validation', + instead='verify', + warning_class=FutureWarning, + since='20201220') + kwargs.update(verify=not old_validation)
try: # Note that the connections are pooled which mean that a future @@ -405,7 +418,6 @@ # verify=True, when a request with verify=False happened before response = session.request(method, uri, params=params, data=body, headers=headers, auth=auth, timeout=timeout, - verify=not ignore_validation, **kwargs) except Exception as e: request.data = e diff --git a/pywikibot/family.py b/pywikibot/family.py index 7d79448..7060c73 100644 --- a/pywikibot/family.py +++ b/pywikibot/family.py @@ -836,6 +836,18 @@ """ return 'http'
+ def verify_SSL_certificate(self, code: str) -> bool: + """ + Return whether a HTTPS certificate should be verified. + + @param code: language code + @return: flag to verify the SSL certificate; + set it to False to allow access if certificate has an error. + """ + return True + + @deprecated('verify_SSL_certificate', since='20201013', + future_warning=True) def ignore_certificate_error(self, code: str) -> bool: """ Return whether a HTTPS certificate error should be ignored. @@ -843,7 +855,7 @@ @param code: language code @return: flag to allow access if certificate has an error. """ - return False + return not self.verify_SSL_certificate
def hostname(self, code): """The hostname to use for standard http connections.""" diff --git a/tests/http_tests.py b/tests/http_tests.py index 346a283..b5f2fab 100644 --- a/tests/http_tests.py +++ b/tests/http_tests.py @@ -106,7 +106,7 @@ with warnings.catch_warnings(record=True) as warning_log: response = http.fetch( uri='https://testssl-expire-r2i2.disig.sk/index.en.html', - disable_ssl_certificate_validation=True) + verify=False) r = response.text self.assertIsInstance(r, str) self.assertTrue(re.search(r'<title>.*</title>', r))