Skip to content

Commit

Permalink
Merge branch 'release/0.2.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Reitz committed Feb 15, 2011
2 parents ed909ea + d525680 commit 53e785f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Development Lead
Patches and Suggestions
```````````````````````

- Various Pocoo Members
- Various Pocoo Members
- Chris Adams
10 changes: 10 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -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)
++++++++++++++++++

Expand Down
35 changes: 22 additions & 13 deletions requests/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import urllib
import urllib2

from urllib2 import HTTPError

try:
import eventlet
Expand All @@ -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'
Expand Down Expand Up @@ -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)
Expand All @@ -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':
Expand Down Expand Up @@ -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':
Expand All @@ -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):
Expand All @@ -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



Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
28 changes: 27 additions & 1 deletion test_requests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import unittest
from cStringIO import StringIO

Expand All @@ -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/')
Expand All @@ -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.