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
Provenance
- Commit
642c667- Tree
2fff2b1- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
cc16073- Parent 2
b8b87d4
4 files changed+71 added−38 removed
modified:docs/conf.py+2 added-2 removed
46 unmodified lines47 # |version| and |release|, also used in various other places throughout the48 # built documents.49 #50 # The short X.Y version.51-version = '0.2.0'51+version = '0.3.2'52 # The full version, including alpha/beta/rc tags.53-release = '0.2.0'53+release = version54 55 # The language for content autogenerated by Sphinx. Refer to documentation56 # for a list of supported languages.57 #language = None175 unmodified linesmodified:requests/core.py+44 added-22 removed
12 unmodified lines13 from __future__ import absolute_import14 15 import urllib16 import urllib217+import zlib18 19 from urllib2 import HTTPError20 from urlparse import urlparse21 2 unmodified lines24 25 26 27 __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'33 34 __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 ]39 40 22 unmodified lines63 _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE')64 65 def __init__(self, url=None, headers=dict(), files=None, method=None,66 data=dict(), auth=None, cookiejar=None):66- 67+68 self.url = url69 self.headers = headers70 self.files = files71 self.method = method69 unmodified lines141 return opener.open142 143 def _build_response(self, resp):144 """Build internal Response object from given response."""144- 145+146 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()149 150+ 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+ pass155 156+ self.response.url = getattr(resp, 'url', None)157+158 @staticmethod159 def _build_url(url, data):160 """Build URLs."""154- 161+162 if urlparse(url).query:163 return '%s&%s' % (url, data)164 else:165 if data:22 unmodified lines188 register_openers()189 190 if self.data:191 self.files.update(self.data)185- 192+193 datagen, headers = multipart_encode(self.files)194 req = _Request(self.url, data=datagen, headers=headers, method=self.method)188- 195+196 else:197 req = _Request(self.url, data=self._enc_data, method=self.method)198 199 if self.headers:5 unmodified lines205 resp = opener(req)206 207 if self.cookiejar is not None:208 self.cookiejar.extract_cookies(resp, req)202- 209+210 except urllib2.HTTPError, why:211 self._build_response(why)212 self.response.error = why213 else:8 unmodified lines222 223 return self.sent224 225 219- def read(self):226+ def read(self, *args):227 return self.response.read()221- 228+229 class 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 lines255 """Raises stored HTTPError if one exists."""256 if self.error:257 raise self.error258 252- def read(self):259+ def read(self, *args):260 return self.content261 262 263 class AuthManager(object):264 """Authentication Manager."""258- 265+266 def __new__(cls):267 singleton = cls.__dict__.get('__singleton__')268 if singleton is not None:269 return singleton13 unmodified lines283 284 285 def add_auth(self, uri, auth):286 """Registers AuthObject to AuthManager."""280- 287+288 uri = self.reduce_uri(uri, False)289+290+ # try to make it an AuthObject291+ if not isinstance(auth, AuthObject):292+ try:293+ auth = AuthObject(*auth)294+ except TypeError:295+ pass296+297 self._auth[uri] = auth298 299 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- 304+305 reduced_uri = tuple([self.reduce_uri(u, False) for u in uri])291- 306+307 if reduced_uri not in self.passwd:308 self.passwd[reduced_uri] = {}309 self.passwd[reduced_uri] = (user, passwd)310 8 unmodified lines319 return (None, None)320 321 322 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)324 325+ 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 authority331 332+333 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 lines350 "https": 443,351 }.get(scheme)352 if dport is not None:353 authority = "%s:%d" % (host, dport)354+355 return authority, path356 335- 357+358 def is_suburi(self, base, test):359 """Check if test is below base in a URI tree360 361 Both args must be URIs in reduced form.100 unmodified lines462 :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- 466+467 return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth)468 469 470 def head(url, params={}, headers={}, cookies=None, auth=None):66 unmodified linesmodified:setup.py+4 added-3 removed
6 unmodified lines7 8 from distutils.core import setup9 10 11- 11+12 if sys.argv[-1] == "publish":13 os.system("python setup.py sdist upload")14 sys.exit()15 16 if sys.argv[-1] == "test":17 os.system("python test_requests.py")18 sys.exit()19- 19+20 required = []21 22-# if python > 2.6, require simplejson22+if sys.version_info[:2] < (2,6):23+ required.append('simplejson')24 25 setup(26 name='requests',27 version=requests.__version__,26 unmodified linesmodified:test_requests.py+21 added-11 removed
28 unmodified lines29 self.assertEqual(r.status_code, 200)30 31 def test_HTTP_200_OK_GET_WITH_PARAMS(self):32 heads = {'User-agent': 'Mozilla/5.0'}33- 33+34 r = requests.get('http://www.google.com/search', params={'q': 'test'}, headers=heads)35 self.assertEqual(r.status_code, 200)36 37 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):8 unmodified lines46 'Mozilla/5.0 (github.com/kennethreitz/requests)'}47 48 r = requests.get('http://whatsmyua.com', headers=heads);49 self.assertTrue(heads['User-agent'] in r.content)50- 50+51 def test_HTTP_200_OK_HEAD(self):52 r = requests.head('http://google.com')53 self.assertEqual(r.status_code, 200)54 15 unmodified lines70 requests.auth_manager.empty()71 72 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- 75+76 post = requests.post(bin.url, data={'some': 'data'})77 self.assertEqual(post.status_code, 201)78 79 post2 = requests.post(bin.url, files={'some': open('test_requests.py')})80 self.assertEqual(post2.status_code, 201)81 82 def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):83 bin = requests.post('http://www.postbin.org/')85- 84+85 self.assertEqual(bin.status_code, 200)86 87 post2 = requests.post(bin.url, files={'some': open('test_requests.py')}, data={'some': 'data'})88 self.assertEqual(post2.status_code, 201)6 unmodified lines95 post2 = requests.post(bin.url, files={'some': open('test_requests.py')},96 headers={'User-Agent': 'requests-tests'})97 98 self.assertEqual(post2.status_code, 201)100- 99+100 def test_nonzero_evaluation(self):101 r = requests.get('http://google.com/some-404-url')102 self.assertEqual(bool(r), False)104- 103+104 r = requests.get('http://google.com/')105 self.assertEqual(bool(r), True)107- 106+107 def test_request_ok_set(self):108 r = requests.get('http://google.com/some-404-url')109 self.assertEqual(r.ok, False)111- 110+111 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- 114+115 r = requests.get('http://google.com/')116 self.assertFalse(r.error)117 r.raise_for_status()119- 118+119 def test_cookie_jar(self):120 """121 .. todo:: This really doesn't test to make sure the cookie is working122 """2 unmodified lines125 126 requests.get('http://google.com', cookies=jar)127 self.assertTrue(jar)128 129+ def test_decompress_gzip(self):130 131+ r = requests.get('http://api.stackoverflow.com/1.1/users/495995/top-answer-tags')132+ r.content.decode('ascii')133 134+ def test_autoauth(self):135+136+ conv_auth = ('requeststest', 'requeststest')137+ requests.auth_manager.add_auth('convore.com', conv_auth)138+139+ r = requests.get('https://convore.com/api/account/verify.json')140+ self.assertEquals(r.status_code, 200)141+142 if __name__ == '__main__':143 unittest.main()