Don't add params to redirect / new configuration

This commit is contained in:
Kenneth Reitz
2011-09-25 20:14:32 -04:00
parent 9488af5a42
commit dc7b4e7d32
3 changed files with 41 additions and 29 deletions
+19 -21
View 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()
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 = Settings()
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
+14 -2
View 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)
+8 -6
View 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):