summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AUTHORS3
-rw-r--r--HISTORY.rst10
-rw-r--r--requests/core.py35
-rw-r--r--setup.py2
-rw-r--r--test_requests.py28
5 files changed, 62 insertions, 16 deletions
diff --git a/AUTHORS b/AUTHORS
index 3fc077d3..0eb7b2f3 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -10,4 +10,5 @@ Development Lead
Patches and Suggestions
```````````````````````
-- Various Pocoo Members \ No newline at end of file
+- Various Pocoo Members
+- Chris Adams
diff --git a/HISTORY.rst b/HISTORY.rst
index f48abe0f..6c630039 100644
--- a/HISTORY.rst
+++ b/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)
++++++++++++++++++
diff --git a/requests/core.py b/requests/core.py
index 6fcfca03..05b7619d 100644
--- a/requests/core.py
+++ b/requests/core.py
@@ -13,6 +13,7 @@
import urllib
import urllib2
+from urllib2 import HTTPError
try:
import eventlet
@@ -33,8 +34,8 @@ from .packages.poster.streaminghttp import register_openers
__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 @@ class Request(object):
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 @@ class Request(object):
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 @@ class Request(object):
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 @@ class Request(object):
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 @@ class Response(object):
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
diff --git a/setup.py b/setup.py
index 727834c5..58edb452 100644
--- a/setup.py
+++ b/setup.py
@@ -22,7 +22,7 @@ required = []
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(),
diff --git a/test_requests.py b/test_requests.py
index 35bb9f65..2fda7ee8 100644
--- a/test_requests.py
+++ b/test_requests.py
@@ -1,7 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-
import unittest
from cStringIO import StringIO
@@ -21,22 +20,27 @@ class RequestsTestSuite(unittest.TestCase):
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'
@@ -44,6 +48,7 @@ class RequestsTestSuite(unittest.TestCase):
self.assertEqual(r.status_code, 200)
+
def test_POSTBIN_GET_POST_FILES(self):
bin = requests.post('http://www.postbin.org/')
@@ -56,6 +61,27 @@ class RequestsTestSuite(unittest.TestCase):
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()