Don't add params to redirect / new configuration
authorKenneth Reitz <me@kennethreitz.com>
Mon, 26 Sep 2011 00:14:32 +0000 (20:14 -0400)
committerKenneth Reitz <me@kennethreitz.com>
Mon, 26 Sep 2011 00:14:32 +0000 (20:14 -0400)
requests/config.py
requests/models.py
requests/sessions.py

index 55c1b88c382d0c8e3ad4eda741e2347ef5024e3d..5bf49b4572e466e63f1b8a8edeb205ef996195cc 100644 (file)
@@ -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
index 50da2f68b02ee2651794b98545dddf0f93730884..9ebd18d1e9fee10073c09660f1f62af4a140cd3a 100644 (file)
@@ -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)
index 7145d278c0d815f280560359ac4179dc56aa8ad7..910b1af87e6953e8361803ab8aa13a321a81c591 100644 (file)
@@ -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):