configuration rework

This commit is contained in:
Kenneth Reitz
2011-09-25 20:57:29 -04:00
parent 351a711ce9
commit 802bcc9ba3
4 changed files with 36 additions and 16 deletions
+5 -4
View File
@@ -11,7 +11,7 @@ This module impliments the Requests API.
"""
import config
from ._config import get_config
from .models import Request, Response
from .status_codes import codes
from .hooks import dispatch_hook
@@ -24,7 +24,7 @@ __all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete')
def request(method, url,
params=None, data=None, headers=None, cookies=None, files=None, auth=None,
timeout=None, allow_redirects=False, proxies=None, hooks=None,
_connection=None):
config=None, _connection=None):
"""Constructs and sends a :class:`Request <Request>`.
Returns :class:`Response <Response>` object.
@@ -44,6 +44,7 @@ def request(method, url,
"""
method = str(method).upper()
config = get_config(config)
if cookies is None:
cookies = {}
@@ -64,9 +65,9 @@ def request(method, url,
cookies=cookies,
files=files,
auth=auth,
timeout=timeout or config.settings.timeout,
timeout=timeout or config.get('timeout'),
allow_redirects=allow_redirects,
proxies=proxies or config.settings.proxies,
proxies=proxies or config.get('proxies'),
)
# Arguments manipulation hook.
+4 -4
View File
@@ -15,7 +15,7 @@ from urlparse import urlparse, urlunparse, urljoin
from .packages import urllib3
# print dir(urllib3)
from .config import get_config
from ._config import get_config
from .structures import CaseInsensitiveDict
from .utils import *
from .status_codes import codes
@@ -91,15 +91,15 @@ class Request(object):
# Header manipulation and defaults.
if settings.accept_gzip:
settings.base_headers.update({'Accept-Encoding': 'gzip'})
if self.config.get('accept_gzip'):
self.headers.update({'Accept-Encoding': 'gzip'})
if headers:
headers = CaseInsensitiveDict(self.headers)
else:
headers = CaseInsensitiveDict()
for (k, v) in settings.base_headers.items():
for (k, v) in self.config.get('base_headers').items():
if k not in headers:
headers[k] = v
+27 -8
View File
@@ -12,23 +12,31 @@ requests (cookies, auth, proxies).
import cookielib
from . import api
from .config import get_config
from ._config import get_config
from .utils import add_dict_to_cookiejar
def merge_kwargs(local_kwargs, default_kwargs):
def merge_kwargs(local_kwarg, default_kwarg):
"""Merges kwarg dictionaries.
If a key in the dictionary is set to None, i
"""
# Bypass if not a dictionary (e.g. timeout)
if not hasattr(local_kwargs, 'items'):
return local_kwargs
if not hasattr(local_kwarg, 'items'):
return local_kwarg
kwargs = default_kwargs.copy()
kwargs.update(local_kwargs)
kwargs = default_kwarg.copy()
kwargs.update(local_kwarg)
# from clint.textui import colored
# print colored.red(default_kwarg)
# print colored.red(local_kwarg)
# Remove keys that are set to None.
for (k,v) in local_kwargs.items():
for (k,v) in local_kwarg.items():
if v is None:
del kwargs[k]
@@ -74,8 +82,19 @@ class Session(object):
self._map_api_methods()
def get(url, **kwargs):
def get(self, url, **kwargs):
_kwargs = {}
for attr in self.__attrs__:
default_attr = getattr(self, attr)
local_attr = kwargs.get(attr)
new_attr = merge_kwargs(local_attr, default_attr)
if new_attr is not None:
_kwargs[attr] = new_attr
return api.get(url, **_kwargs)
def __repr__(self):
return '<requests-client at 0x%x>' % (id(self))