Skip to content
Myr

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

1 file+23 added−9 removed

modified:requests/core.py+23 added-9 removed
14 unmodified lines
15import urllib16import urllib21718from urllib2 import HTTPError19from urlparse import urlparse2021from .packages.poster.encode import multipart_encode22from .packages.poster.streaminghttp import register_openers, get_handlers23
18 unmodified lines
42 """Hidden wrapper around the urllib2.Request object. Allows for manual43 setting of HTTP methods.44 """4545 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 = method4950 def get_method(self):
11 unmodified lines
62 _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE')6364 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 = method7172 # 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 lines
133 self.response.url = getattr(resp, 'url', None)134 self.response.content = resp.read()135136137 @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 url148149150 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 lines
160 self._checks()161 success = False162163 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 lines
398 :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)403390 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()408409 return r.response410
6 unmodified lines
417 :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 """407421 422 return request('GET', url, params=params, headers=headers, cookiejar=cookies, auth=auth)423424425def head(url, params={}, headers={}, cookies=None, auth=None):
66 unmodified lines