Add Session method to merge environment settings with per-request settings
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ian Cordasco · committed by Ian Cordasco · parent 7a09e72 · Browse files · 1 file·+30 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+30 added−24 removed
- other:+30 added−24 removed
Provenance
- Commit
b4a7906- Tree
a956dac- Author
Ian Cordasco <graffatcolmingov@gmail.com>- Committer
Ian Cordasco <graffatcolmingov@gmail.com>- Parent
7a09e72
1 file changed+30 added−24 removed
modified:requests/sessions.py+30 added-24 removed
433 unmodified lines434 prep = self.prepare_request(req)435 436 proxies = proxies or {}437 438- # Gather clues from the surrounding environment.439- if self.trust_env:440- # Set environment's proxies.441- env_proxies = get_environ_proxies(url) or {}442- for (k, v) in env_proxies.items():443- proxies.setdefault(k, v)438+ settings = self.merge_environment_settings(439+ prep.url, proxies, stream, verify, cert440+ )441 445- # Look for configuration.446- if verify is True or verify is None:447- verify = os.environ.get('REQUESTS_CA_BUNDLE')448-449- # Curl compatibility.450- if verify is True or verify is None:451- verify = os.environ.get('CURL_CA_BUNDLE')452-453- # Merge all the kwargs.454- proxies = merge_setting(proxies, self.proxies)455- stream = merge_setting(stream, self.stream)456- verify = merge_setting(verify, self.verify)457- cert = merge_setting(cert, self.cert)458-442 # Send the request.443 send_kwargs = {461- 'stream': stream,444 'timeout': timeout,463- 'verify': verify,464- 'cert': cert,465- 'proxies': proxies,445 'allow_redirects': allow_redirects,446 }447+ send_kwargs.update(settings)448 resp = self.send(prep, **send_kwargs)449 450 return resp451 139 unmodified lines591 r.content592 593 return r594 595+ def merge_environment_settings(self, url, proxies, stream, verify, cert):596+ """Checks the environment and merges it with some settings."""597+ # Gather clues from the surrounding environment.598+ if self.trust_env:599+ # Set environment's proxies.600+ env_proxies = get_environ_proxies(url) or {}601+ for (k, v) in env_proxies.items():602+ proxies.setdefault(k, v)603+604+ # Look for configuration.605+ if verify is True or verify is None:606+ verify = os.environ.get('REQUESTS_CA_BUNDLE')607+608+ # Curl compatibility.609+ if verify is True or verify is None:610+ verify = os.environ.get('CURL_CA_BUNDLE')611+612+ # Merge all the kwargs.613+ proxies = merge_setting(proxies, self.proxies)614+ stream = merge_setting(stream, self.stream)615+ verify = merge_setting(verify, self.verify)616+ cert = merge_setting(cert, self.cert)617+618+ return {'verify': verify, 'proxies': proxies, 'stream': stream,619+ 'cert': cert}620+621 def get_adapter(self, url):622 """Returns the appropriate connnection adapter for the given URL."""623 for (prefix, adapter) in self.adapters.items():624 34 unmodified lines