Much nicer helper functions and constructors
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 efedbd9 · Browse files · 1 file·+16 added−63 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+16 added−63 removed
- other:+16 added−63 removed
Provenance
- Commit
f4c4bf3- Tree
f56e2fa- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
efedbd9
1 file changed+16 added−63 removed
modified:requests/core.py+16 added-63 removed
7 unmodified lines8 9 :copyright: (c) 2011 by Kenneth Reitz.10 :license: ISC, see LICENSE for more details.11 """12+13 from __future__ import absolute_import14 import urllib15 import urllib216 16-17 from urllib2 import HTTPError18 19 try:20 import eventlet7 unmodified lines28 monkey.patch_all()29 except ImportError:30 pass31 32-32 from .packages.poster.encode import multipart_encode33 from .packages.poster.streaminghttp import register_openers34 35 7 unmodified lines43 44 AUTOAUTHS = []45 46 47+48 class _Request(urllib2.Request):49 """Hidden wrapper around the urllib2.Request object. Allows for manual50 setting of HTTP methods.51 """15 unmodified lines67 """68 69 _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE')70 71- def __init__(self):72- self.url = None73- self.headers = dict()74- self.files = None75- self.method = None76- self.params = {}77- self.data = {}71+ def __init__(self, url=None, headers=dict(), files=None, method=None, params=dict(), data=dict(), auth=None, cookiejar=None):72+ self.url = url73+ self.headers = headers74+ self.files = files75+ self.method = method76+ self.params = params77+ self.data = data78 self.response = Response()79 80- self.auth = None81- self.cookiejar = None80+ self.auth = auth81+ self.cookiejar = cookiejar82 self.sent = False83 84 85 def __repr__(self):216 unmodified lines302 :param cookies: (optional) CookieJar object to send with the :class:`Request`.303 :param auth: (optional) AuthObject to enable Basic HTTP Auth.304 """305 306- r = Request()307- 308- r.method = 'GET'309- r.url = url310- r.params = params311- r.headers = headers312- r.cookiejar = cookies313- r.auth = _detect_auth(url, auth)314- 306+ r = Request(method='GET', url=url, params=params, headers=headers, cookiejar=cookies, auth=_detect_auth(url, auth))307 r.send()308 309 return r.response310 6 unmodified lines317 :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.318 :param cookies: (optional) CookieJar object to send with the :class:`Request`.319 :param auth: (optional) AuthObject to enable Basic HTTP Auth.320 """329- 330- r = Request()331- 332- r.method = 'HEAD'333- r.url = url334- # return response object335- r.params = params336- r.headers = headers337- r.cookiejar = cookies338- r.auth = _detect_auth(url, auth)339- 321+ r = Request(method='HEAD', url=url, params=params, headers=headers, cookiejar=cookies, auth=_detect_auth(url, auth))322 r.send()323 324 return r.response325 8 unmodified lines334 :param cookies: (optional) CookieJar object to send with the :class:`Request`.335 :param auth: (optional) AuthObject to enable Basic HTTP Auth.336 """337 356- r = Request()357- 358- r.url = url359- r.method = 'POST'360- r.data = data361- 362- if files:363- r.files = files364- 365- r.headers = headers366- r.cookiejar = cookies367- r.auth = _detect_auth(url, auth)368- 338+ r = Request(method='POST', url=url, data=data, headers=headers, files=files, cookiejar=cookies, auth=_detect_auth(url, auth))339 r.send()340 341 return r.response342 7 unmodified lines350 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.351 :param cookies: (optional) CookieJar object to send with the :class:`Request`.352 :param auth: (optional) AuthObject to enable Basic HTTP Auth.353 """384- 385- r = Request()354 387- r.url = url388- r.method = 'PUT'389- r.data = data390- r.files = files391- r.headers = headers392- r.cookiejar = cookies393- r.auth = _detect_auth(url, auth)394- 355+ r = Request(method='PUT', url=url, data=data, headers=headers, files=files, cookiejar=cookies, auth=_detect_auth(url, auth)) 356 r.send()357 358 return r.response359 7 unmodified lines367 :param cookies: (optional) CookieJar object to send with the :class:`Request`.368 :param auth: (optional) AuthObject to enable Basic HTTP Auth.369 """370 410- r = Request()411-412- r.url = url413- r.method = 'DELETE'414- 415- r.headers = headers416- r.cookiejar = cookies417- r.auth = _detect_auth(url, auth)418- 371+ r = Request(method='DELETE', url=url, params=params, headers=headers, cookiejar=cookies, auth=_detect_auth(url, auth))372 r.send()373 374 return r.response375 51 unmodified lines