From b4a7906bca61f94a5b7a0e177b0d85b3ee11604f Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 23 Aug 2014 15:50:15 -0500 Subject: [PATCH 1/2] Add Session method to merge environment settings with per-request settings --- requests/sessions.py | 54 ++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index 07b6a32c..dafa9567 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -435,36 +435,16 @@ class Session(SessionRedirectMixin): proxies = proxies or {} - # Gather clues from the surrounding environment. - if self.trust_env: - # Set environment's proxies. - env_proxies = get_environ_proxies(url) or {} - for (k, v) in env_proxies.items(): - proxies.setdefault(k, v) - - # Look for configuration. - if verify is True or verify is None: - verify = os.environ.get('REQUESTS_CA_BUNDLE') - - # Curl compatibility. - if verify is True or verify is None: - verify = os.environ.get('CURL_CA_BUNDLE') - - # Merge all the kwargs. - proxies = merge_setting(proxies, self.proxies) - stream = merge_setting(stream, self.stream) - verify = merge_setting(verify, self.verify) - cert = merge_setting(cert, self.cert) + settings = self.merge_environment_settings( + prep.url, proxies, stream, verify, cert + ) # Send the request. send_kwargs = { - 'stream': stream, 'timeout': timeout, - 'verify': verify, - 'cert': cert, - 'proxies': proxies, 'allow_redirects': allow_redirects, } + send_kwargs.update(settings) resp = self.send(prep, **send_kwargs) return resp @@ -612,6 +592,32 @@ class Session(SessionRedirectMixin): return r + def merge_environment_settings(self, url, proxies, stream, verify, cert): + """Checks the environment and merges it with some settings.""" + # Gather clues from the surrounding environment. + if self.trust_env: + # Set environment's proxies. + env_proxies = get_environ_proxies(url) or {} + for (k, v) in env_proxies.items(): + proxies.setdefault(k, v) + + # Look for configuration. + if verify is True or verify is None: + verify = os.environ.get('REQUESTS_CA_BUNDLE') + + # Curl compatibility. + if verify is True or verify is None: + verify = os.environ.get('CURL_CA_BUNDLE') + + # Merge all the kwargs. + proxies = merge_setting(proxies, self.proxies) + stream = merge_setting(stream, self.stream) + verify = merge_setting(verify, self.verify) + cert = merge_setting(cert, self.cert) + + return {'verify': verify, 'proxies': proxies, 'stream': stream, + 'cert': cert} + def get_adapter(self, url): """Returns the appropriate connnection adapter for the given URL.""" for (prefix, adapter) in self.adapters.items(): From 596ca83f0c66460deb82af07b13ce724b9f46474 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sun, 24 Aug 2014 09:54:56 -0500 Subject: [PATCH 2/2] Update verify check and doc-string --- requests/sessions.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index dafa9567..508b0ef2 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -593,7 +593,7 @@ class Session(SessionRedirectMixin): return r def merge_environment_settings(self, url, proxies, stream, verify, cert): - """Checks the environment and merges it with some settings.""" + """Check the environment and merge it with some settings.""" # Gather clues from the surrounding environment. if self.trust_env: # Set environment's proxies. @@ -601,13 +601,11 @@ class Session(SessionRedirectMixin): for (k, v) in env_proxies.items(): proxies.setdefault(k, v) - # Look for configuration. + # Look for requests environment configuration and be compatible + # with cURL. if verify is True or verify is None: - verify = os.environ.get('REQUESTS_CA_BUNDLE') - - # Curl compatibility. - if verify is True or verify is None: - verify = os.environ.get('CURL_CA_BUNDLE') + verify = (os.environ.get('REQUESTS_CA_BUNDLE') or + os.environ.get('CURL_CA_BUNDLE')) # Merge all the kwargs. proxies = merge_setting(proxies, self.proxies)