url builder, cleanups
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 9a8b25d · Browse files · 1 file·+23 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+23 added−9 removed
- other:+23 added−9 removed
Provenance
- Commit
32f7558- Tree
b75a6d1- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
9a8b25d
1 file changed+23 added−9 removed
modified:requests/core.py+23 added-9 removed
14 unmodified lines15 import urllib16 import urllib217 18 from urllib2 import HTTPError19+from urlparse import urlparse20 21 from .packages.poster.encode import multipart_encode22 from .packages.poster.streaminghttp import register_openers, get_handlers23 18 unmodified lines42 """Hidden wrapper around the urllib2.Request object. Allows for manual43 setting of HTTP methods.44 """45 45- def __init__(self, url, data=None, headers={}, origin_req_host=None,46- unverifiable=False, method=None):46+ def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None):47 urllib2.Request.__init__(self, url, data, headers, origin_req_host, unverifiable)48 self.method = method49 50 def get_method(self):11 unmodified lines62 _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE')63 64 def __init__(self, url=None, headers=dict(), files=None, method=None,65 data=dict(), auth=None, cookiejar=None):66+ 67 self.url = url68 self.headers = headers69 self.files = files70 self.method = method71 72 # url encode data if it's a dict72- if isinstance(data, dict):73+ if hasattr(data, 'items'):74 self.data = urllib.urlencode(data)75 else:76 self.data = data77 55 unmodified lines133 self.response.url = getattr(resp, 'url', None)134 self.response.content = resp.read()135 136 137+ @staticmethod138+ def _build_url(url, data):139+ """Build URLs."""140+ 141+ if urlparse(url).query:142+ return '%s&%s' % (url, data)143+ else:144+ if data:145+ return '%s?%s' % (url, data)146+ else:147+ return url148+149+150 def send(self, anyway=False):151 """Sends the request. Returns True of successful, false if not.152 If there was an HTTPError during transmission,153 self.response.status_code will contain the HTTPError code.6 unmodified lines160 self._checks()161 success = False162 163 if self.method in ('GET', 'HEAD', 'DELETE'):150- req = _Request(("%s?%s" % (self.url, self.data)), method=self.method)164+ req = _Request(self._build_url(self.url, self.data), method=self.method)165 else:166 if self.files:167 register_openers()168 datagen, headers = multipart_encode(self.files)229 unmodified lines398 :param cookies: (optional) CookieJar object to send with the :class:`Request`.399 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.400 :param auth: (optional) AuthObject to enable Basic HTTP Auth.401 """388- data = kwargs.pop('data', {}) or kwargs.pop('params', {})402+ data = kwargs.get('data', None) or kwargs.get('params', None)403 390- r = Request(method=method, url=url, data=data, headers=kwargs.pop('headers', {}),391- cookiejar=kwargs.pop('cookies', None), files=kwargs.pop('files', None),392- auth=kwargs.pop('auth', auth_manager.get_auth(url)))404+ r = Request(method=method, url=url, data=data, headers=kwargs.get('headers', {}),405+ cookiejar=kwargs.get('cookies', None), files=kwargs.get('files', None),406+ auth=kwargs.get('auth', auth_manager.get_auth(url)))407 r.send()408 409 return r.response410 6 unmodified lines417 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.418 :param cookies: (optional) CookieJar object to send with the :class:`Request`.419 :param auth: (optional) AuthObject to enable Basic HTTP Auth.420 """407-421+ 422 return request('GET', url, params=params, headers=headers, cookiejar=cookies, auth=auth)423 424 425 def head(url, params={}, headers={}, cookies=None, auth=None):66 unmodified lines