From dc7b4e7d32e45494d43d37c3d7c61032211b83ca Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 25 Sep 2011 20:14:32 -0400 Subject: [PATCH] Don't add params to redirect / new configuration --- requests/config.py | 36 +++++++++++++++++------------------- requests/models.py | 16 ++++++++++++++-- requests/sessions.py | 14 ++++++++------ 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/requests/config.py b/requests/config.py index 55c1b88..5bf49b4 100644 --- a/requests/config.py +++ b/requests/config.py @@ -8,8 +8,6 @@ This module provides the Requests settings feature set. settings parameters: -TODO: Verify!!! -TODO: Make sure format is acceptabl/cool - :base_headers: - Sets default User-Agent to `python-requests.org` - :accept_gzip: - Whether or not to accept gzip-compressed data - :proxies: - http proxies? @@ -18,29 +16,29 @@ TODO: Make sure format is acceptabl/cool - :max_redirects: - maximum number of allowed redirects? - :decode_unicode: - whether or not to accept unicode? -Used globally - """ +def merge_configs(config, default_config=None): + """Merge two given configurations.""" -class Settings(object): + # Use the module-level defaults, if none is given. + if default_config is None: + default_config = config.copy() - def __init__(self, **kwargs): - super(Settings, self).__init__() + d = default_config.copy() + d.update(config) - def __getattribute__(self, key): - return object.__getattribute__(self, key) + return d +# Module-level defaults. +config = dict() -settings = Settings() +config['base_headers'] = {'User-Agent': 'python-requests.org'} +config['accept_gzip'] = True +config['proxies'] = {} +config['verbose'] = None +config['timeout'] = None +config['max_redirects'] = 30 +config['decode_unicode'] = True -settings.base_headers = {'User-Agent': 'python-requests.org'} -settings.accept_gzip = True -settings.proxies = None -settings.verbose = None -settings.timeout = None -settings.max_redirects = 30 -settings.decode_unicode = True -#: Use socket.setdefaulttimeout() as fallback? -settings.timeout_fallback = True diff --git a/requests/models.py b/requests/models.py index 50da2f6..9ebd18d 100644 --- a/requests/models.py +++ b/requests/models.py @@ -160,6 +160,10 @@ class Request(object): (self.allow_redirects)) ): + # print r.headers['location'] + # print dir(r.raw._original_response.fp) + # print '--' + # We already redirected. Don't keep it alive. # r.raw.close() @@ -195,8 +199,15 @@ class Request(object): # Create the new Request. request = Request( - url, self.headers, self.files, method, - self.data, self.params, self.auth, self.cookies, + url=url, + headers=self.headers, + files=self.files, + method=method, + data=self.data, + # params=self.params, + params=None, + auth=self.auth, + cookies=self.cookies, # Flag as part of a redirect loop. redirect=True @@ -272,6 +283,7 @@ class Request(object): # except (urllib2.HTTPError, urllib2.URLError), why: except Exception, why: + print why.__dict__ # if hasattr(why, 'reason'): # if isinstance(why.reason, socket.timeout): # why = Timeout(why) diff --git a/requests/sessions.py b/requests/sessions.py index 7145d27..910b1af 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -18,12 +18,16 @@ from .utils import add_dict_to_cookiejar class Session(object): """A Requests session.""" - __attrs__ = ['headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks'] + __attrs__ = [ + 'headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks', + 'config' + ] def __init__(self, **kwargs): # Set up a CookieJar to be used by default self.cookies = cookielib.FileCookieJar() + self.config = kwargs.get('config') or dict() # Map args from kwargs to instance-local variables map(lambda k, v: (k in self.__attrs__) and setattr(self, k, v), @@ -39,7 +43,6 @@ class Session(object): return self def __exit__(self, *args): - # print args pass def _map_api_methods(self): @@ -50,8 +53,8 @@ class Session(object): def pass_args(func): def wrapper_func(*args, **kwargs): - inst_attrs = dict((k, v) for k, v in self.__dict__.iteritems() - if k in self.__attrs__) + inst_attrs = dict((k, v) for k, v in self.__dict__.iteritems() if k in self.__attrs__) + # Combine instance-local values with kwargs values, with # priority to values in kwargs kwargs = dict(inst_attrs.items() + kwargs.items()) @@ -70,8 +73,7 @@ class Session(object): return wrapper_func # Map and decorate each function available in requests.api - map(lambda fn: setattr(self, fn, pass_args(getattr(api, fn))), - api.__all__) + map(lambda fn: setattr(self, fn, pass_args(getattr(api, fn))), api.__all__) def session(**kwargs): -- 2.34.1