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?
- :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
(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()
# 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
# 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)
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),
return self
def __exit__(self, *args):
- # print args
pass
def _map_api_methods(self):
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())
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):