Merge branch 'release/0.2.3'
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 ed909ea, d525680 · Browse files · 5 files·+62 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+62 added−16 removed
- other:+62 added−16 removed
Provenance
- Commit
53e785f- Tree
e8e9c6a- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
ed909ea- Parent 2
d525680
5 files changed+62 added−16 removed
modified:requests/core.py+22 added-13 removed
11 unmodified lines12 13 import urllib14 import urllib215 16+from urllib2 import HTTPError17 18 try:19 import eventlet20 eventlet.monkey_patch()12 unmodified lines33 from .packages.poster.streaminghttp import register_openers34 35 36 __title__ = 'requests'36-__version__ = '0.2.2'37-__build__ = 0x00020237+__version__ = '0.2.3'38+__build__ = 0x00020339 __author__ = 'Kenneth Reitz'40 __license__ = 'ISC'41 __copyright__ = 'Copyright 2011 Kenneth Reitz'42 116 unmodified lines159 # url encode GET params if it's a dict160 if isinstance(self.params, dict):161 params = urllib.urlencode(self.params)162 else:162-163 params = self.params164 165 req = _Request(("%s?%s" % (self.url, params)), method=self.method)166 4 unmodified lines171 172 try:173 resp = opener(req)174 self._build_response(resp)175- success = True175+ self.response.ok = True176 177 except urllib2.HTTPError as why:178 self._build_response(why)179- success = False179+ self.response.error = why180 181 182 elif self.method == 'PUT':183 if (not self.sent) or anyway:19 unmodified lines203 opener = self._get_opener()204 resp = opener(req)205 206 self._build_response(resp)207- success = True207+ self.response.ok = True208 209 except urllib2.HTTPError as why:210 self._build_response(why)211- success = False211+ self.response.error = why212 213 214 elif self.method == 'POST':215 if (not self.sent) or anyway:16 unmodified lines232 else:233 req.data = self.data234 235 try:236-236 opener = self._get_opener()237 resp = opener(req)238 239 self._build_response(resp)241- success = True240+ self.response.ok = True241 242 except urllib2.HTTPError as why:243 self._build_response(why)245- success = False246-244+ self.response.error = why245 248- self.sent = True if success else False246+ self.sent = self.response.ok247 250- return success248+ return self.sent249 250 251 class Response(object):252 """The :class:`Request` object. All :class:`Request` objects contain a5 unmodified lines258 self.content = None259 self.status_code = None260 self.headers = dict()261 self.url = None262+ self.ok = False263+ self.error = None264 265 def __repr__(self):266 return '<Response [%s]>' % (self.status_code)267+ 268+ def __nonzero__(self):269+ """Returns true if status_code is 'OK'."""270+ return not self.error271+ 272+ def raise_for_status(self):273+ """Raises stored HTTPError if one exists."""274+ if self.error:275+ raise self.error276 277 278 279 class AuthObject(object):193 unmodified linesmodified:AUTHORS+2 added-1 removed
8 unmodified lines9 10 Patches and Suggestions11 ```````````````````````12 13-- Various Pocoo Members13+- Various Pocoo Members14+- Chris Adamsmodified:HISTORY.rst+10 added
1 History2 -------3 4+0.2.3 (2011-02-15)5+++++++++++++++++++6+7+* New HTTPHandling Methods8+ - Reponse.__nonzero__ (false if bad HTTP Status)9+ - Response.ok (True if expected HTTP Status)10+ - Response.error (Logged HTTPError if bad HTTP Status)11+ - Reponse.raise_for_status() (Raises stored HTTPError)12+13+14 0.2.2 (2011-02-14)15 ++++++++++++++++++16 17 * Still handles request in the event of an HTTPError. (Issue #2)23 unmodified linesmodified:setup.py+1 added-1 removed
20 unmodified lines21 # if python > 2.6, require simplejson22 23 setup(24 name='requests',25- version='0.2.2',25+ version='0.2.3',26 description='Awesome Python HTTP Library that\'s actually usable.',27 long_description=open('README.rst').read() + '\n\n' +28 open('HISTORY.rst').read(),29 author='Kenneth Reitz',22 unmodified linesmodified:test_requests.py+27 added-1 removed
1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 4-4 import unittest5 from cStringIO import StringIO6 7 import requests11 unmodified lines19 20 def test_invalid_url(self):21 self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')22 23+24 def test_HTTP_200_OK_GET(self):25 r = requests.get('http://google.com')26 self.assertEqual(r.status_code, 200)27 28+29 def test_HTTPS_200_OK_GET(self):30 r = requests.get('https://google.com')31 self.assertEqual(r.status_code, 200)32 33+34 def test_HTTP_200_OK_HEAD(self):35 r = requests.head('http://google.com')36 self.assertEqual(r.status_code, 200)37 38+39 def test_HTTPS_200_OK_HEAD(self):40 r = requests.head('https://google.com')41 self.assertEqual(r.status_code, 200)42 43+44 def test_AUTH_HTTPS_200_OK_GET(self):45 auth = requests.AuthObject('requeststest', 'requeststest')46 url = 'https://convore.com/api/account/verify.json'47 r = requests.get(url, auth=auth)48 49 self.assertEqual(r.status_code, 200)50 51+52 def test_POSTBIN_GET_POST_FILES(self):53 54 bin = requests.post('http://www.postbin.org/')55 self.assertEqual(bin.status_code, 200)4 unmodified lines60 post2 = requests.post(bin.url, files={'some': StringIO('data')})61 self.assertEqual(post2.status_code, 201)62 63 64+ def test_nonzero_evaluation(self):65+ r = requests.get('http://google.com/some-404-url')66+ self.assertEqual(bool(r), False)67+ 68+ r = requests.get('http://google.com/')69+ self.assertEqual(bool(r), True)70 71+72+ def test_request_ok_set(self):73+ r = requests.get('http://google.com/some-404-url')74+ self.assertEqual(r.ok, False)75+76+77+ def test_status_raising(self):78+ r = requests.get('http://google.com/some-404-url')79+ self.assertRaises(requests.HTTPError, r.raise_for_status)80+81+ r = requests.get('http://google.com/')82+ self.assertFalse(r.error)83+ r.raise_for_status()84+85+86 if __name__ == '__main__':87 unittest.main()