Merge settings when creating PreparedRequest, don't copy Request.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Robert Estelle · committed by Robert Estelle · parent 0eccb54 · Browse files · 3 files·+53 added−86 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+53 added−86 removed
- other:+53 added−86 removed
Provenance
- Commit
0ab7a52- Tree
2edadc5- Author
Robert Estelle <robert@btbapp.com>- Committer
Robert Estelle <robert@btbapp.com>- Parent
0eccb54
3 files changed+53 added−86 removed
modified:requests/models.py+27 added-32 removed
26 unmodified lines27 from .compat import (28 cookielib, urlunparse, urlsplit, urlencode, str, bytes, StringIO,29 is_py2, chardet, json, builtin_str, basestring)30 31-from copy import copy as shallowcopy32-31 CONTENT_CHUNK_SIZE = 10 * 102432 ITER_CHUNK_SIZE = 51233 34 log = logging.getLogger(__name__)178 unmodified lines213 214 def __repr__(self):215 return '<Request [%s]>' % (self.method)216 219- def copy(self):220- return Request(221- method = self.method,222- url = self.url,223- auth = self.auth,224-225- # Copy mutable dict/list parameters so that altering one request226- # does not alter the copy.227- headers = shallowcopy(self.headers),228- params = shallowcopy(self.params),229- cookies = shallowcopy(self.cookies),230- hooks = shallowcopy(self.hooks),231-232- files = shallowcopy(self.files),233- data = shallowcopy(self.data),234- )235-217 def prepare(self):218 """Constructs a :class:`PreparedRequest <PreparedRequest>` for transmission and returns it."""219 p = PreparedRequest()239-240- p.prepare_method(self.method)241- p.prepare_url(self.url, self.params)242- p.prepare_headers(self.headers)243- p.prepare_cookies(self.cookies)244- p.prepare_body(self.data, self.files)245- p.prepare_auth(self.auth, self.url)246- # Note that prepare_auth must be last to enable authentication schemes247- # such as OAuth to work on a fully prepared request.248-249- # This MUST go after prepare_auth. Authenticators could add a hook250- p.prepare_hooks(self.hooks)251-220+ p.prepare(221+ method=self.method,222+ url=self.url,223+ headers=self.headers,224+ files=self.files,225+ data=self.data,226+ params=self.params,227+ auth=self.auth,228+ cookies=self.cookies,229+ hooks=self.hooks,230+ )231 return p232 233 234 class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):26 unmodified lines261 self.body = None262 #: dictionary of callback hooks, for internal usage.263 self.hooks = default_hooks()264 265+ def prepare(self, method=None, url=None, headers=None, files=None,266+ data=None, params=None, auth=None, cookies=None, hooks=None):267+ """Prepares the the entire request with the given parameters."""268+269+ self.prepare_method(method)270+ self.prepare_url(url, params)271+ self.prepare_headers(headers)272+ self.prepare_cookies(cookies)273+ self.prepare_body(data, files)274+ self.prepare_auth(auth, url)275+ # Note that prepare_auth must be last to enable authentication schemes276+ # such as OAuth to work on a fully prepared request.277+278+ # This MUST go after prepare_auth. Authenticators could add a hook279+ self.prepare_hooks(hooks)280+281 def __repr__(self):282 return '<PreparedRequest [%s]>' % (self.method)283 284 def copy(self):436 unmodified linesmodified:requests/sessions.py+26 added-23 removed
226 unmodified lines227 228 def __exit__(self, *args):229 self.close()230 231- def update_request(self, request):232- """Destructively updates/merges the settings of a :class:`Request`233- object from those of the :class:`Session`.231+ def prepare_request(self, request):232+ """Constructs a :class:`PreparedRequest <PreparedRequest>` for233+ transmission and returns it. The :class:`PreparedRequest` has settings234+ merged from the :class:`Request <Request>` instance and those of the235+ :class:`Session`.236 235- :param request: mutable :class:`Request` instance.237+ :param request: :class:`Request` instance to prepare with this238+ session's settings.239 """240 cookies = request.cookies or {}241 242 # Bootstrap CookieJar.3 unmodified lines246 # Merge with session cookies247 merged_cookies = RequestsCookieJar()248 merged_cookies.update(self.cookies)249 merged_cookies.update(cookies)247- request.cookies = merged_cookies250 249- # Gather clues from the surrounding environment.250- if self.trust_env:251- # Set environment's basic authentication if not explicitly set.252- if not request.auth and not self.auth:253- request.auth = get_netrc_auth(request.url)251 255- # Merge settings from the request and the session.256- request.params = merge_setting(request.params, self.params)257- request.headers = merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict)258- request.auth = merge_setting(request.auth, self.auth)259- request.hooks = merge_setting(request.hooks, self.hooks)260- request.method = request.method.upper()252+ # Set environment's basic authentication if not explicitly set.253+ auth = request.auth254+ if self.trust_env and not auth and not self.auth:255+ auth = get_netrc_auth(request.url)256 262- def prepare_request(self, request):263- req = request.copy()264- self.update_request(req)265- return req.prepare()257+ p = PreparedRequest()258+ p.prepare(259+ method=request.method.upper(),260+ url=request.url,261+ files=request.files,262+ data=request.data,263+ headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict),264+ params=merge_setting(request.params, self.params),265+ auth=merge_setting(auth, self.auth),266+ cookies=merged_cookies,267+ hooks=merge_setting(request.hooks, self.hooks),268+ )269+ return p270 271 def request(self, method, url,272 params=None,273 data=None,48 unmodified lines322 auth = auth,323 cookies = cookies,324 hooks = hooks,325 )322- self.update_request(req)323- prep = req.prepare()326+ prep = self.prepare_request(req)327 328 proxies = proxies or {}329 330 # Gather clues from the surrounding environment.109 unmodified lines440 kwargs.setdefault('proxies', self.proxies)441 442 # It's possible that users might accidentally send a Request object.443 # Guard against that specific failure case.441- if getattr(request, 'prepare', None):444+ if not isinstance(request, PreparedRequest):445 raise ValueError('You can only send PreparedRequests.')446 447 # Set up variables needed for resolve_redirects and dispatching of448 # hooks78 unmodified linesmodified:test_requests.py-31 removed
456 unmodified lines457 resp = s.send(prep)458 459 self.assertTrue(hasattr(resp, 'hook_working'))460 461- def test_request_copy(self):462- a = requests.Request('POST', httpbin('post'),463- headers={'oh': 'gee'},464- files={'some': 'thing'},465- data={'baz':'qux'},466- params={'foo': 'bar'},467- auth=object(),468- cookies={'Some-Cookie':'Some Value'},469- hooks={'response': lambda r: r })470- b = a.copy()471-472- self.assertNotEqual(a, b)473- self.assertEqual(a.method, b.method)474- self.assertEqual(a.url, b.url)475- self.assertEqual(a.headers, b.headers)476- self.assertEqual(a.files, b.files)477- self.assertEqual(a.data, b.data)478- self.assertEqual(a.params, b.params)479- self.assertEqual(a.auth, b.auth)480- self.assertEqual(a.cookies, b.cookies)481- self.assertEqual(a.hooks, b.hooks)482-483- a.headers['Foo'] = 'Bar'484- self.assertNotEqual(a.headers, b.headers)485-486- a.data['x'] = 'y'487- self.assertNotEqual(a.data, b.data)488-489- a.params['_'] = '?'490- self.assertNotEqual(a.params, b.params)491-461 def test_prepared_from_session(self):462 class DummyAuth(requests.auth.AuthBase):463 def __call__(self, r):464 r.headers['Dummy-Auth-Test'] = 'dummy-auth-test-ok'342 unmodified lines