Permalink
Browse files

Merge branch 'release/0.2.3'

  • Loading branch information...
2 parents ed909ea + d525680 commit 53e785f6561ea61e11338f62fc6815c292ae9780 @kennethreitz committed Feb 15, 2011
Showing with 62 additions and 16 deletions.
  1. +2 −1 AUTHORS
  2. +10 −0 HISTORY.rst
  3. +22 −13 requests/core.py
  4. +1 −1 setup.py
  5. +27 −1 test_requests.py
View
3 AUTHORS
@@ -10,4 +10,5 @@ Development Lead
Patches and Suggestions
```````````````````````
-- Various Pocoo Members
+- Various Pocoo Members
+- Chris Adams
View
10 HISTORY.rst
@@ -1,6 +1,16 @@
History
-------
+0.2.3 (2011-02-15)
+++++++++++++++++++
+
+* New HTTPHandling Methods
+ - Reponse.__nonzero__ (false if bad HTTP Status)
+ - Response.ok (True if expected HTTP Status)
+ - Response.error (Logged HTTPError if bad HTTP Status)
+ - Reponse.raise_for_status() (Raises stored HTTPError)
+
+
0.2.2 (2011-02-14)
++++++++++++++++++
View
35 requests/core.py
@@ -13,6 +13,7 @@
import urllib
import urllib2
+from urllib2 import HTTPError
try:
import eventlet
@@ -33,8 +34,8 @@
__title__ = 'requests'
-__version__ = '0.2.2'
-__build__ = 0x000202
+__version__ = '0.2.3'
+__build__ = 0x000203
__author__ = 'Kenneth Reitz'
__license__ = 'ISC'
__copyright__ = 'Copyright 2011 Kenneth Reitz'
@@ -159,7 +160,6 @@ def send(self, anyway=False):
if isinstance(self.params, dict):
params = urllib.urlencode(self.params)
else:
-
params = self.params
req = _Request(("%s?%s" % (self.url, params)), method=self.method)
@@ -172,11 +172,11 @@ def send(self, anyway=False):
try:
resp = opener(req)
self._build_response(resp)
- success = True
+ self.response.ok = True
except urllib2.HTTPError as why:
self._build_response(why)
- success = False
+ self.response.error = why
elif self.method == 'PUT':
@@ -204,11 +204,11 @@ def send(self, anyway=False):
resp = opener(req)
self._build_response(resp)
- success = True
+ self.response.ok = True
except urllib2.HTTPError as why:
self._build_response(why)
- success = False
+ self.response.error = why
elif self.method == 'POST':
@@ -233,21 +233,19 @@ def send(self, anyway=False):
req.data = self.data
try:
-
opener = self._get_opener()
resp = opener(req)
self._build_response(resp)
- success = True
+ self.response.ok = True
except urllib2.HTTPError as why:
self._build_response(why)
- success = False
-
+ self.response.error = why
- self.sent = True if success else False
+ self.sent = self.response.ok
- return success
+ return self.sent
class Response(object):
@@ -261,9 +259,20 @@ def __init__(self):
self.status_code = None
self.headers = dict()
self.url = None
+ self.ok = False
+ self.error = None
def __repr__(self):
return '<Response [%s]>' % (self.status_code)
+
+ def __nonzero__(self):
+ """Returns true if status_code is 'OK'."""
+ return not self.error
+
+ def raise_for_status(self):
+ """Raises stored HTTPError if one exists."""
+ if self.error:
+ raise self.error
View
2 setup.py
@@ -22,7 +22,7 @@
setup(
name='requests',
- version='0.2.2',
+ version='0.2.3',
description='Awesome Python HTTP Library that\'s actually usable.',
long_description=open('README.rst').read() + '\n\n' +
open('HISTORY.rst').read(),
View
28 test_requests.py
@@ -1,7 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-
import unittest
from cStringIO import StringIO
@@ -21,29 +20,35 @@ def tearDown(self):
def test_invalid_url(self):
self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')
+
def test_HTTP_200_OK_GET(self):
r = requests.get('http://google.com')
self.assertEqual(r.status_code, 200)
+
def test_HTTPS_200_OK_GET(self):
r = requests.get('https://google.com')
self.assertEqual(r.status_code, 200)
+
def test_HTTP_200_OK_HEAD(self):
r = requests.head('http://google.com')
self.assertEqual(r.status_code, 200)
+
def test_HTTPS_200_OK_HEAD(self):
r = requests.head('https://google.com')
self.assertEqual(r.status_code, 200)
+
def test_AUTH_HTTPS_200_OK_GET(self):
auth = requests.AuthObject('requeststest', 'requeststest')
url = 'https://convore.com/api/account/verify.json'
r = requests.get(url, auth=auth)
self.assertEqual(r.status_code, 200)
+
def test_POSTBIN_GET_POST_FILES(self):
bin = requests.post('http://www.postbin.org/')
@@ -56,6 +61,27 @@ def test_POSTBIN_GET_POST_FILES(self):
self.assertEqual(post2.status_code, 201)
+ def test_nonzero_evaluation(self):
+ r = requests.get('http://google.com/some-404-url')
+ self.assertEqual(bool(r), False)
+
+ r = requests.get('http://google.com/')
+ self.assertEqual(bool(r), True)
+
+
+ def test_request_ok_set(self):
+ r = requests.get('http://google.com/some-404-url')
+ self.assertEqual(r.ok, False)
+
+
+ def test_status_raising(self):
+ r = requests.get('http://google.com/some-404-url')
+ self.assertRaises(requests.HTTPError, r.raise_for_status)
+
+ r = requests.get('http://google.com/')
+ self.assertFalse(r.error)
+ r.raise_for_status()
+
if __name__ == '__main__':
unittest.main()

0 comments on commit 53e785f

Please sign in to comment.