Skip to content
Myr

Merge branch 'release/0.3.2'

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 · parents cc16073, b8b87d4 · Browse files · 4 files+71 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+71 added−38 removed

  • other:+71 added−38 removed

4 files+71 added−38 removed

modified:docs/conf.py+2 added-2 removed
46 unmodified lines
47# |version| and |release|, also used in various other places throughout the48# built documents.49#50# The short X.Y version.51version = '0.2.0'51version = '0.3.2'52# The full version, including alpha/beta/rc tags.53release = '0.2.0'53release = version5455# The language for content autogenerated by Sphinx. Refer to documentation56# for a list of supported languages.57#language = None
175 unmodified lines
modified:requests/core.py+44 added-22 removed
12 unmodified lines
13from __future__ import absolute_import1415import urllib16import urllib217import zlib1819from urllib2 import HTTPError20from urlparse import urlparse21
2 unmodified lines
24252627__title__ = 'requests'27__version__ = '0.3.1'28__build__ = 0x00030128__version__ = '0.3.2'29__build__ = 0x00030230__author__ = 'Kenneth Reitz'31__license__ = 'ISC'32__copyright__ = 'Copyright 2011 Kenneth Reitz'3334__all__ = [34 'Request', 'Response', 'request', 'get', 'head', 'post', 'put', 'delete', 35 'auth_manager', 'AuthObject','RequestException', 'AuthenticationError', 35 'Request', 'Response', 'request', 'get', 'head', 'post', 'put', 'delete',36 'auth_manager', 'AuthObject','RequestException', 'AuthenticationError',37 'URLRequired', 'InvalidMethod', 'HTTPError'38]3940
22 unmodified lines
63 _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE')6465 def __init__(self, url=None, headers=dict(), files=None, method=None,66 data=dict(), auth=None, cookiejar=None):66 6768 self.url = url69 self.headers = headers70 self.files = files71 self.method = method
69 unmodified lines
141 return opener.open142143 def _build_response(self, resp):144 """Build internal Response object from given response."""144 145146 self.response.status_code = getattr(resp, 'code', None)147 self.response.headers = getattr(resp.info(), 'dict', None)147 self.response.url = getattr(resp, 'url', None)148 self.response.content = resp.read()149150 if self.response.headers.get('content-encoding', None) == 'gzip':151 try:152 self.response.content = zlib.decompress(self.response.content, 16+zlib.MAX_WBITS)153 except zlib.error:154 pass155156 self.response.url = getattr(resp, 'url', None)157158 @staticmethod159 def _build_url(url, data):160 """Build URLs."""154 161162 if urlparse(url).query:163 return '%s&%s' % (url, data)164 else:165 if data:
22 unmodified lines
188 register_openers()189190 if self.data:191 self.files.update(self.data)185 192193 datagen, headers = multipart_encode(self.files)194 req = _Request(self.url, data=datagen, headers=headers, method=self.method)188 195196 else:197 req = _Request(self.url, data=self._enc_data, method=self.method)198199 if self.headers:
5 unmodified lines
205 resp = opener(req)206207 if self.cookiejar is not None:208 self.cookiejar.extract_cookies(resp, req)202 209210 except urllib2.HTTPError, why:211 self._build_response(why)212 self.response.error = why213 else:
8 unmodified lines
222223 return self.sent224225219 def read(self):226 def read(self, *args):227 return self.response.read()221 228229class Response(object):230 """The :class:`Request` object. All :class:`Request` objects contain a231 :class:`Request.response <response>` attribute, which is an instance of232 this class.
22 unmodified lines
255 """Raises stored HTTPError if one exists."""256 if self.error:257 raise self.error258252 def read(self):259 def read(self, *args):260 return self.content261262263class AuthManager(object):264 """Authentication Manager."""258 265266 def __new__(cls):267 singleton = cls.__dict__.get('__singleton__')268 if singleton is not None:269 return singleton
13 unmodified lines
283284285 def add_auth(self, uri, auth):286 """Registers AuthObject to AuthManager."""280 287288 uri = self.reduce_uri(uri, False)289290 # try to make it an AuthObject291 if not isinstance(auth, AuthObject):292 try:293 auth = AuthObject(*auth)294 except TypeError:295 pass296297 self._auth[uri] = auth298299 def add_password(self, realm, uri, user, passwd):300 """Adds password to AuthManager."""301 # uri could be a single URI or a sequence302 if isinstance(uri, basestring):303 uri = [uri]289 304305 reduced_uri = tuple([self.reduce_uri(u, False) for u in uri])291 306307 if reduced_uri not in self.passwd:308 self.passwd[reduced_uri] = {}309 self.passwd[reduced_uri] = (user, passwd)310
8 unmodified lines
319 return (None, None)320321322 def get_auth(self, uri):308 uri = self.reduce_uri(uri, False)309 return self._auth.get(uri, None)323 (in_domain, in_path) = self.reduce_uri(uri, False)324325 for domain, path, authority in (326 (i[0][0], i[0][1], i[1]) for i in self._auth.iteritems()327 ):328 if in_domain == domain:329 if path in in_path:330 return authority331332333 def reduce_uri(self, uri, default_port=True):334 """Accept authority or URI and extract only the authority and path."""335 # note HTTP URLs do not have a userinfo component336 parts = urllib2.urlparse.urlsplit(uri)
13 unmodified lines
350 "https": 443,351 }.get(scheme)352 if dport is not None:353 authority = "%s:%d" % (host, dport)354355 return authority, path356335 357358 def is_suburi(self, base, test):359 """Check if test is below base in a URI tree360361 Both args must be URIs in reduced form.
100 unmodified lines
462 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.463 :param cookies: (optional) CookieJar object to send with the :class:`Request`.464 :param auth: (optional) AuthObject to enable Basic HTTP Auth.465 """444 466467 return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth)468469470def head(url, params={}, headers={}, cookies=None, auth=None):
66 unmodified lines
modified:setup.py+4 added-3 removed
6 unmodified lines
78from distutils.core import setup91011 1112if sys.argv[-1] == "publish":13 os.system("python setup.py sdist upload")14 sys.exit()1516if sys.argv[-1] == "test":17 os.system("python test_requests.py")18 sys.exit()19 1920required = []2122# if python > 2.6, require simplejson22if sys.version_info[:2] < (2,6):23 required.append('simplejson')2425setup(26 name='requests',27 version=requests.__version__,
26 unmodified lines
modified:test_requests.py+21 added-11 removed
28 unmodified lines
29 self.assertEqual(r.status_code, 200)3031 def test_HTTP_200_OK_GET_WITH_PARAMS(self):32 heads = {'User-agent': 'Mozilla/5.0'}33 3334 r = requests.get('http://www.google.com/search', params={'q': 'test'}, headers=heads)35 self.assertEqual(r.status_code, 200)3637 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):
8 unmodified lines
46 'Mozilla/5.0 (github.com/kennethreitz/requests)'}4748 r = requests.get('http://whatsmyua.com', headers=heads);49 self.assertTrue(heads['User-agent'] in r.content)50 5051 def test_HTTP_200_OK_HEAD(self):52 r = requests.head('http://google.com')53 self.assertEqual(r.status_code, 200)54
15 unmodified lines
70 requests.auth_manager.empty()7172 def test_POSTBIN_GET_POST_FILES(self):73 bin = requests.post('http://www.postbin.org/')74 print bin.url74 self.assertEqual(bin.status_code, 200)76 7576 post = requests.post(bin.url, data={'some': 'data'})77 self.assertEqual(post.status_code, 201)7879 post2 = requests.post(bin.url, files={'some': open('test_requests.py')})80 self.assertEqual(post2.status_code, 201)8182 def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):83 bin = requests.post('http://www.postbin.org/')85 8485 self.assertEqual(bin.status_code, 200)8687 post2 = requests.post(bin.url, files={'some': open('test_requests.py')}, data={'some': 'data'})88 self.assertEqual(post2.status_code, 201)
6 unmodified lines
95 post2 = requests.post(bin.url, files={'some': open('test_requests.py')},96 headers={'User-Agent': 'requests-tests'})9798 self.assertEqual(post2.status_code, 201)100 99100 def test_nonzero_evaluation(self):101 r = requests.get('http://google.com/some-404-url')102 self.assertEqual(bool(r), False)104 103104 r = requests.get('http://google.com/')105 self.assertEqual(bool(r), True)107 106107 def test_request_ok_set(self):108 r = requests.get('http://google.com/some-404-url')109 self.assertEqual(r.ok, False)111 110111 def test_status_raising(self):112 r = requests.get('http://google.com/some-404-url')113 self.assertRaises(requests.HTTPError, r.raise_for_status)115 114115 r = requests.get('http://google.com/')116 self.assertFalse(r.error)117 r.raise_for_status()119 118119 def test_cookie_jar(self):120 """121 .. todo:: This really doesn't test to make sure the cookie is working122 """
2 unmodified lines
125126 requests.get('http://google.com', cookies=jar)127 self.assertTrue(jar)128129 def test_decompress_gzip(self):130131 r = requests.get('http://api.stackoverflow.com/1.1/users/495995/top-answer-tags')132 r.content.decode('ascii')133134 def test_autoauth(self):135136 conv_auth = ('requeststest', 'requeststest')137 requests.auth_manager.add_auth('convore.com', conv_auth)138139 r = requests.get('https://convore.com/api/account/verify.json')140 self.assertEquals(r.status_code, 200)141142if __name__ == '__main__':143 unittest.main()