From 8ac29b40e91910660afaa5d90529a1c6861e5014 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Tue, 15 Feb 2011 09:51:46 -0500 Subject: [PATCH 01/11] new documentation on Request changes. --- README.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.rst b/README.rst index 83bd3c44..fab341dd 100644 --- a/README.rst +++ b/README.rst @@ -96,6 +96,15 @@ If CookieJar object is is passed in (cookies=...), the cookies will be sent with Request.url (String) URL of response. Useful for detecting redirects. + + Request.ok + True if no errors occurred during the request, and the status_code is kosher. + + Request.error + (HTTPError) If an HTTPError occurred (e.g. status of 404), Otherwise this is None. + + Request.raise_for_status() + Raises HTTPError if a request is not kosher. **HTTP Authentication Registry:** From fa42c12e5b3a9bad83594d9831ddb00be094e249 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Tue, 15 Feb 2011 09:58:02 -0500 Subject: [PATCH 02/11] I need autodoc. --- README.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index fab341dd..325c69ca 100644 --- a/README.rst +++ b/README.rst @@ -85,25 +85,25 @@ If CookieJar object is is passed in (cookies=...), the cookies will be sent with **Responses:** - Request.status_code: + Response.status_code: (Integer) Received HTTP Status Code Response - Request.headers: + Response.headers: (Dictionary) Received HTTP Response Headers - Request.content: + Response.content: (Bytes) Received Content - Request.url + Response.url (String) URL of response. Useful for detecting redirects. - Request.ok - True if no errors occurred during the request, and the status_code is kosher. + Response.ok + (Bool) True if no errors occurred during the request, and the status_code is kosher. - Request.error + Response.error (HTTPError) If an HTTPError occurred (e.g. status of 404), Otherwise this is None. - Request.raise_for_status() + Response.raise_for_status() Raises HTTPError if a request is not kosher. From a5517960ea63728d3d8cb5c761a13b749575b3c9 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Tue, 15 Feb 2011 10:02:39 -0500 Subject: [PATCH 03/11] history indent fix for rest/pypi --- HISTORY.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 6c630039..0b60fb1b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -5,10 +5,10 @@ History ++++++++++++++++++ * 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) + - 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) From 34ca692c9cee4b9d146b8cd276d00ca4a7df8cb1 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 16 Feb 2011 12:47:58 -0500 Subject: [PATCH 04/11] from __future__ import absolute_imports --- requests/core.py | 3 ++- requests/packages/__init__.py | 2 ++ requests/packages/poster/__init__.py | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/requests/core.py b/requests/core.py index 05b7619d..59d778b9 100644 --- a/requests/core.py +++ b/requests/core.py @@ -9,10 +9,11 @@ :copyright: (c) 2011 by Kenneth Reitz. :license: ISC, see LICENSE for more details. """ - +from __future__ import absolute_import import urllib import urllib2 + from urllib2 import HTTPError try: diff --git a/requests/packages/__init__.py b/requests/packages/__init__.py index 5d44dd4d..ab2669e8 100644 --- a/requests/packages/__init__.py +++ b/requests/packages/__init__.py @@ -1 +1,3 @@ +from __future__ import absolute_import + from . import poster diff --git a/requests/packages/poster/__init__.py b/requests/packages/poster/__init__.py index 8f26770b..6e216fce 100644 --- a/requests/packages/poster/__init__.py +++ b/requests/packages/poster/__init__.py @@ -26,6 +26,8 @@ New releases of poster will always have a version number that compares greater than an older version of poster. New in version 0.6.""" +from __future__ import absolute_import + from . import streaminghttp from . import encode From f4e03a5d32366dc874a60eef64ea36502e3e23a9 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 17 Feb 2011 13:13:52 -0500 Subject: [PATCH 05/11] gitignore update for jenkins --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1ff8381a..e5cfd99e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .idea -MANIFEST \ No newline at end of file +MANIFEST +coverage.xml +nosetests.xml +pylint.txt \ No newline at end of file From efedbd9775b5c086b57b32c62cf45dd952c8f5b6 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 17 Feb 2011 13:14:03 -0500 Subject: [PATCH 06/11] added test_suite.sh for CI --- test_suite.sh | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 test_suite.sh diff --git a/test_suite.sh b/test_suite.sh new file mode 100755 index 00000000..bcea4c2c --- /dev/null +++ b/test_suite.sh @@ -0,0 +1,4 @@ +nosetests test_requests.py --with-xunit --with-coverage +coverage xml +rm -fr pylint.txt +pylint -d W0312 -d W0212 -d E1101 -d E0202 -d W0102 -d E0102 -f parseable ./requests > pylint.txt || true From f4c4bf3d0b8d8a09ec645f4064522852d0c5f9ee Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 17 Feb 2011 16:17:46 -0500 Subject: [PATCH 07/11] Much nicer helper functions and constructors --- requests/core.py | 79 ++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 63 deletions(-) diff --git a/requests/core.py b/requests/core.py index 59d778b9..f6d34447 100644 --- a/requests/core.py +++ b/requests/core.py @@ -9,11 +9,11 @@ :copyright: (c) 2011 by Kenneth Reitz. :license: ISC, see LICENSE for more details. """ + from __future__ import absolute_import import urllib import urllib2 - from urllib2 import HTTPError try: @@ -29,7 +29,6 @@ if not 'eventlet' in locals(): except ImportError: pass - from .packages.poster.encode import multipart_encode from .packages.poster.streaminghttp import register_openers @@ -45,6 +44,7 @@ __copyright__ = 'Copyright 2011 Kenneth Reitz' AUTOAUTHS = [] + class _Request(urllib2.Request): """Hidden wrapper around the urllib2.Request object. Allows for manual setting of HTTP methods. @@ -68,17 +68,17 @@ class Request(object): _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE') - def __init__(self): - self.url = None - self.headers = dict() - self.files = None - self.method = None - self.params = {} - self.data = {} + def __init__(self, url=None, headers=dict(), files=None, method=None, params=dict(), data=dict(), auth=None, cookiejar=None): + self.url = url + self.headers = headers + self.files = files + self.method = method + self.params = params + self.data = data self.response = Response() - self.auth = None - self.cookiejar = None + self.auth = auth + self.cookiejar = cookiejar self.sent = False @@ -303,15 +303,7 @@ def get(url, params={}, headers={}, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - r = Request() - - r.method = 'GET' - r.url = url - r.params = params - r.headers = headers - r.cookiejar = cookies - r.auth = _detect_auth(url, auth) - + r = Request(method='GET', url=url, params=params, headers=headers, cookiejar=cookies, auth=_detect_auth(url, auth)) r.send() return r.response @@ -326,17 +318,7 @@ def head(url, params={}, headers={}, cookies=None, auth=None): :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - - r = Request() - - r.method = 'HEAD' - r.url = url - # return response object - r.params = params - r.headers = headers - r.cookiejar = cookies - r.auth = _detect_auth(url, auth) - + r = Request(method='HEAD', url=url, params=params, headers=headers, cookiejar=cookies, auth=_detect_auth(url, auth)) r.send() return r.response @@ -353,19 +335,7 @@ def post(url, data={}, headers={}, files=None, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - r = Request() - - r.url = url - r.method = 'POST' - r.data = data - - if files: - r.files = files - - r.headers = headers - r.cookiejar = cookies - r.auth = _detect_auth(url, auth) - + r = Request(method='POST', url=url, data=data, headers=headers, files=files, cookiejar=cookies, auth=_detect_auth(url, auth)) r.send() return r.response @@ -381,17 +351,8 @@ def put(url, data='', headers={}, files={}, cookies=None, auth=None): :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - - r = Request() - r.url = url - r.method = 'PUT' - r.data = data - r.files = files - r.headers = headers - r.cookiejar = cookies - r.auth = _detect_auth(url, auth) - + r = Request(method='PUT', url=url, data=data, headers=headers, files=files, cookiejar=cookies, auth=_detect_auth(url, auth)) r.send() return r.response @@ -407,15 +368,7 @@ def delete(url, params={}, headers={}, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - r = Request() - - r.url = url - r.method = 'DELETE' - - r.headers = headers - r.cookiejar = cookies - r.auth = _detect_auth(url, auth) - + r = Request(method='DELETE', url=url, params=params, headers=headers, cookiejar=cookies, auth=_detect_auth(url, auth)) r.send() return r.response From 015d6b78d5035a6a19402b21cd963cd13ec82f83 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 17 Feb 2011 16:20:53 -0500 Subject: [PATCH 08/11] cleaning up line lengths --- requests/core.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/requests/core.py b/requests/core.py index f6d34447..17bd4247 100644 --- a/requests/core.py +++ b/requests/core.py @@ -50,8 +50,9 @@ class _Request(urllib2.Request): setting of HTTP methods. """ - def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None): - urllib2.Request.__init__( self, url, data, headers, origin_req_host, unverifiable) + def __init__(self, url, data=None, headers={}, origin_req_host=None, + unverifiable=False, method=None): + urllib2.Request.__init__(self, url, data, headers, origin_req_host, unverifiable) self.method = method def get_method(self): @@ -68,7 +69,8 @@ class Request(object): _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE') - def __init__(self, url=None, headers=dict(), files=None, method=None, params=dict(), data=dict(), auth=None, cookiejar=None): + def __init__(self, url=None, headers=dict(), files=None, method=None, + params=dict(), data=dict(), auth=None, cookiejar=None): self.url = url self.headers = headers self.files = files @@ -303,7 +305,8 @@ def get(url, params={}, headers={}, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - r = Request(method='GET', url=url, params=params, headers=headers, cookiejar=cookies, auth=_detect_auth(url, auth)) + r = Request(method='GET', url=url, params=params, headers=headers, + cookiejar=cookies, auth=_detect_auth(url, auth)) r.send() return r.response @@ -318,7 +321,8 @@ def head(url, params={}, headers={}, cookies=None, auth=None): :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - r = Request(method='HEAD', url=url, params=params, headers=headers, cookiejar=cookies, auth=_detect_auth(url, auth)) + r = Request(method='HEAD', url=url, params=params, headers=headers, + cookiejar=cookies, auth=_detect_auth(url, auth)) r.send() return r.response @@ -335,7 +339,8 @@ def post(url, data={}, headers={}, files=None, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - r = Request(method='POST', url=url, data=data, headers=headers, files=files, cookiejar=cookies, auth=_detect_auth(url, auth)) + r = Request(method='POST', url=url, data=data, headers=headers, + files=files, cookiejar=cookies, auth=_detect_auth(url, auth)) r.send() return r.response @@ -352,7 +357,8 @@ def put(url, data='', headers={}, files={}, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - r = Request(method='PUT', url=url, data=data, headers=headers, files=files, cookiejar=cookies, auth=_detect_auth(url, auth)) + r = Request(method='PUT', url=url, data=data, headers=headers, files=files, + cookiejar=cookies, auth=_detect_auth(url, auth)) r.send() return r.response @@ -368,7 +374,8 @@ def delete(url, params={}, headers={}, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - r = Request(method='DELETE', url=url, params=params, headers=headers, cookiejar=cookies, auth=_detect_auth(url, auth)) + r = Request(method='DELETE', url=url, params=params, headers=headers, + cookiejar=cookies, auth=_detect_auth(url, auth)) r.send() return r.response @@ -414,7 +421,8 @@ def _get_autoauth(url): class RequestException(Exception): - """There was an ambiguous exception that occured while handling your request.""" + """There was an ambiguous exception that occured while handling your + request.""" class AuthenticationError(RequestException): """The authentication credentials provided were invalid.""" From 7229dbe0331492b16ed57745b0b448e8f26160ca Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 17 Feb 2011 16:22:03 -0500 Subject: [PATCH 09/11] Making everyone else happy. --- requests/core.py | 466 +++++++++++++++++++++++------------------------ setup.py | 62 +++---- test_requests.py | 100 +++++----- 3 files changed, 314 insertions(+), 314 deletions(-) diff --git a/requests/core.py b/requests/core.py index 17bd4247..cd2e764c 100644 --- a/requests/core.py +++ b/requests/core.py @@ -17,17 +17,17 @@ import urllib2 from urllib2 import HTTPError try: - import eventlet - eventlet.monkey_patch() + import eventlet + eventlet.monkey_patch() except ImportError: - pass + pass if not 'eventlet' in locals(): - try: - from gevent import monkey - monkey.patch_all() - except ImportError: - pass + try: + from gevent import monkey + monkey.patch_all() + except ImportError: + pass from .packages.poster.encode import multipart_encode from .packages.poster.streaminghttp import register_openers @@ -46,102 +46,102 @@ AUTOAUTHS = [] class _Request(urllib2.Request): - """Hidden wrapper around the urllib2.Request object. Allows for manual + """Hidden wrapper around the urllib2.Request object. Allows for manual setting of HTTP methods. """ - - def __init__(self, url, data=None, headers={}, origin_req_host=None, - unverifiable=False, method=None): - urllib2.Request.__init__(self, url, data, headers, origin_req_host, unverifiable) - self.method = method + + def __init__(self, url, data=None, headers={}, origin_req_host=None, + unverifiable=False, method=None): + urllib2.Request.__init__(self, url, data, headers, origin_req_host, unverifiable) + self.method = method - def get_method(self): - if self.method: - return self.method + def get_method(self): + if self.method: + return self.method - return urllib2.Request.get_method(self) + return urllib2.Request.get_method(self) class Request(object): - """The :class:`Request` object. It carries out all functionality of + """The :class:`Request` object. It carries out all functionality of Requests. Recommended interface is with the Requests functions. """ - - _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE') - - def __init__(self, url=None, headers=dict(), files=None, method=None, - params=dict(), data=dict(), auth=None, cookiejar=None): - self.url = url - self.headers = headers - self.files = files - self.method = method - self.params = params - self.data = data - self.response = Response() - - self.auth = auth - self.cookiejar = cookiejar - self.sent = False - - - def __repr__(self): - return '' % (self.method) - - - def __setattr__(self, name, value): - if (name == 'method') and (value): - if not value in self._METHODS: - raise InvalidMethod() - - object.__setattr__(self, name, value) - - - def _checks(self): - """Deterministic checks for consistency.""" + + _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE') + + def __init__(self, url=None, headers=dict(), files=None, method=None, + params=dict(), data=dict(), auth=None, cookiejar=None): + self.url = url + self.headers = headers + self.files = files + self.method = method + self.params = params + self.data = data + self.response = Response() + + self.auth = auth + self.cookiejar = cookiejar + self.sent = False + + + def __repr__(self): + return '' % (self.method) + + + def __setattr__(self, name, value): + if (name == 'method') and (value): + if not value in self._METHODS: + raise InvalidMethod() + + object.__setattr__(self, name, value) + + + def _checks(self): + """Deterministic checks for consistency.""" - if not self.url: - raise URLRequired + if not self.url: + raise URLRequired - - def _get_opener(self): - """Creates appropriate opener object for urllib2.""" + + def _get_opener(self): + """Creates appropriate opener object for urllib2.""" - _handlers = [] + _handlers = [] - if self.auth or self.cookiejar: + if self.auth or self.cookiejar: - if self.auth: + if self.auth: - authr = urllib2.HTTPPasswordMgrWithDefaultRealm() + authr = urllib2.HTTPPasswordMgrWithDefaultRealm() - authr.add_password(None, self.url, self.auth.username, self.auth.password) - auth_handler = urllib2.HTTPBasicAuthHandler(authr) + authr.add_password(None, self.url, self.auth.username, self.auth.password) + auth_handler = urllib2.HTTPBasicAuthHandler(authr) - _handlers.append(auth_handler) + _handlers.append(auth_handler) - if self.cookiejar: + if self.cookiejar: - cookie_handler = urllib2.HTTPCookieProcessor(cookiejar) - _handlers.append(cookie_handler) + cookie_handler = urllib2.HTTPCookieProcessor(cookiejar) + _handlers.append(cookie_handler) - opener = urllib2.build_opener(*_handlers) - return opener.open + opener = urllib2.build_opener(*_handlers) + return opener.open - else: - return urllib2.urlopen + else: + return urllib2.urlopen - def _build_response(self, resp): - """Build internal Response object from given response.""" - - self.response.status_code = resp.code - self.response.headers = resp.info().dict - self.response.content = resp.read() - self.response.url = resp.url + def _build_response(self, resp): + """Build internal Response object from given response.""" + + self.response.status_code = resp.code + self.response.headers = resp.info().dict + self.response.content = resp.read() + self.response.url = resp.url - - def send(self, anyway=False): - """Sends the request. Returns True of successful, false if not. + + def send(self, anyway=False): + """Sends the request. Returns True of successful, false if not. If there was an HTTPError during transmission, self.response.status_code will contain the HTTPError code. @@ -151,136 +151,136 @@ class Request(object): already been sent. """ - self._checks() + self._checks() - success = False + success = False - if self.method in ('GET', 'HEAD', 'DELETE'): - if (not self.sent) or anyway: + if self.method in ('GET', 'HEAD', 'DELETE'): + if (not self.sent) or anyway: - # url encode GET params if it's a dict - if isinstance(self.params, dict): - params = urllib.urlencode(self.params) - else: - params = self.params + # url encode GET params if it's a dict + if isinstance(self.params, dict): + params = urllib.urlencode(self.params) + else: + params = self.params - req = _Request(("%s?%s" % (self.url, params)), method=self.method) + req = _Request(("%s?%s" % (self.url, params)), method=self.method) - if self.headers: - req.headers = self.headers + if self.headers: + req.headers = self.headers - opener = self._get_opener() + opener = self._get_opener() - try: - resp = opener(req) - self._build_response(resp) - self.response.ok = True + try: + resp = opener(req) + self._build_response(resp) + self.response.ok = True - except urllib2.HTTPError as why: - self._build_response(why) - self.response.error = why + except urllib2.HTTPError as why: + self._build_response(why) + self.response.error = why - elif self.method == 'PUT': - if (not self.sent) or anyway: + elif self.method == 'PUT': + if (not self.sent) or anyway: - if self.files: - register_openers() - datagen, headers = multipart_encode(self.files) - req = _Request(self.url, data=datagen, headers=headers, method='PUT') + if self.files: + register_openers() + datagen, headers = multipart_encode(self.files) + req = _Request(self.url, data=datagen, headers=headers, method='PUT') - if self.headers: - req.headers.update(self.headers) + if self.headers: + req.headers.update(self.headers) - else: + else: - req = _Request(self.url, method='PUT') + req = _Request(self.url, method='PUT') - if self.headers: - req.headers = self.headers + if self.headers: + req.headers = self.headers - req.data = self.data + req.data = self.data - try: - opener = self._get_opener() - resp = opener(req) + try: + opener = self._get_opener() + resp = opener(req) - self._build_response(resp) - self.response.ok = True + self._build_response(resp) + self.response.ok = True - except urllib2.HTTPError as why: - self._build_response(why) - self.response.error = why + except urllib2.HTTPError as why: + self._build_response(why) + self.response.error = why - elif self.method == 'POST': - if (not self.sent) or anyway: + elif self.method == 'POST': + if (not self.sent) or anyway: - if self.files: - register_openers() - datagen, headers = multipart_encode(self.files) - req = _Request(self.url, data=datagen, headers=headers, method='POST') + if self.files: + register_openers() + datagen, headers = multipart_encode(self.files) + req = _Request(self.url, data=datagen, headers=headers, method='POST') - if self.headers: - req.headers.update(self.headers) - - else: - req = _Request(self.url, method='POST') - req.headers = self.headers + if self.headers: + req.headers.update(self.headers) + + else: + req = _Request(self.url, method='POST') + req.headers = self.headers - # url encode form data if it's a dict - if isinstance(self.data, dict): - req.data = urllib.urlencode(self.data) - else: - req.data = self.data + # url encode form data if it's a dict + if isinstance(self.data, dict): + req.data = urllib.urlencode(self.data) + else: + req.data = self.data - try: - opener = self._get_opener() - resp = opener(req) + try: + opener = self._get_opener() + resp = opener(req) - self._build_response(resp) - self.response.ok = True + self._build_response(resp) + self.response.ok = True - except urllib2.HTTPError as why: - self._build_response(why) - self.response.error = why - - self.sent = self.response.ok - - return self.sent - + except urllib2.HTTPError as why: + self._build_response(why) + self.response.error = why + + self.sent = self.response.ok + + return self.sent + class Response(object): - """The :class:`Request` object. All :class:`Request` objects contain a + """The :class:`Request` object. All :class:`Request` objects contain a :class:`Request.response ` attribute, which is an instance of this class. """ - def __init__(self): - self.content = None - self.status_code = None - self.headers = dict() - self.url = None - self.ok = False - self.error = None - - def __repr__(self): - return '' % (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 + def __init__(self): + self.content = None + self.status_code = None + self.headers = dict() + self.url = None + self.ok = False + self.error = None + + def __repr__(self): + return '' % (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 - + class AuthObject(object): - """The :class:`AuthObject` is a simple HTTP Authentication token. When + """The :class:`AuthObject` is a simple HTTP Authentication token. When given to a Requests function, it enables Basic HTTP Authentication for that Request. You can also enable Authorization for domain realms with AutoAuth. See AutoAuth for more details. @@ -288,15 +288,15 @@ class AuthObject(object): :param username: Username to authenticate with. :param password: Password for given username. """ - - def __init__(self, username, password): - self.username = username - self.password = password + + def __init__(self, username, password): + self.username = username + self.password = password def get(url, params={}, headers={}, cookies=None, auth=None): - """Sends a GET request. Returns :class:`Response` object. + """Sends a GET request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`. @@ -304,16 +304,16 @@ def get(url, params={}, headers={}, cookies=None, auth=None): :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - - r = Request(method='GET', url=url, params=params, headers=headers, - cookiejar=cookies, auth=_detect_auth(url, auth)) - r.send() - - return r.response + + r = Request(method='GET', url=url, params=params, headers=headers, + cookiejar=cookies, auth=_detect_auth(url, auth)) + r.send() + + return r.response def head(url, params={}, headers={}, cookies=None, auth=None): - """Sends a HEAD request. Returns :class:`Response` object. + """Sends a HEAD request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`. @@ -321,15 +321,15 @@ def head(url, params={}, headers={}, cookies=None, auth=None): :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - r = Request(method='HEAD', url=url, params=params, headers=headers, - cookiejar=cookies, auth=_detect_auth(url, auth)) - r.send() - - return r.response + r = Request(method='HEAD', url=url, params=params, headers=headers, + cookiejar=cookies, auth=_detect_auth(url, auth)) + r.send() + + return r.response def post(url, data={}, headers={}, files=None, cookies=None, auth=None): - """Sends a POST request. Returns :class:`Response` object. + """Sends a POST request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary of POST Data to send with the :class:`Request`. @@ -338,16 +338,16 @@ def post(url, data={}, headers={}, files=None, cookies=None, auth=None): :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - - r = Request(method='POST', url=url, data=data, headers=headers, - files=files, cookiejar=cookies, auth=_detect_auth(url, auth)) - r.send() - - return r.response - - + + r = Request(method='POST', url=url, data=data, headers=headers, + files=files, cookiejar=cookies, auth=_detect_auth(url, auth)) + r.send() + + return r.response + + def put(url, data='', headers={}, files={}, cookies=None, auth=None): - """Sends a PUT request. Returns :class:`Response` object. + """Sends a PUT request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Bytes of PUT Data to send with the :class:`Request`. @@ -357,15 +357,15 @@ def put(url, data='', headers={}, files={}, cookies=None, auth=None): :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - r = Request(method='PUT', url=url, data=data, headers=headers, files=files, - cookiejar=cookies, auth=_detect_auth(url, auth)) - r.send() - - return r.response + r = Request(method='PUT', url=url, data=data, headers=headers, files=files, + cookiejar=cookies, auth=_detect_auth(url, auth)) + r.send() + + return r.response - + def delete(url, params={}, headers={}, cookies=None, auth=None): - """Sends a DELETE request. Returns :class:`Response` object. + """Sends a DELETE request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary of GET Parameters to send with the :class:`Request`. @@ -373,16 +373,16 @@ def delete(url, params={}, headers={}, cookies=None, auth=None): :param cookies: (optional) CookieJar object to send with the :class:`Request`. :param auth: (optional) AuthObject to enable Basic HTTP Auth. """ - - r = Request(method='DELETE', url=url, params=params, headers=headers, - cookiejar=cookies, auth=_detect_auth(url, auth)) - r.send() - - return r.response + + r = Request(method='DELETE', url=url, params=params, headers=headers, + cookiejar=cookies, auth=_detect_auth(url, auth)) + r.send() + + return r.response def add_autoauth(url, authobject): - """Registers given AuthObject to given URL domain. for auto-activation. + """Registers given AuthObject to given URL domain. for auto-activation. Once a URL is registered with an AuthObject, the configured HTTP Authentication will be used for all requests with URLS containing the given URL string. @@ -397,38 +397,38 @@ def add_autoauth(url, authobject): :param authobject: AuthObject to auto-activate. """ - global AUTOAUTHS - - AUTOAUTHS.append((url, authobject)) + global AUTOAUTHS + + AUTOAUTHS.append((url, authobject)) def _detect_auth(url, auth): - """Returns registered AuthObject for given url if available, defaulting to + """Returns registered AuthObject for given url if available, defaulting to given AuthObject. """ - return _get_autoauth(url) if not auth else auth + return _get_autoauth(url) if not auth else auth - + def _get_autoauth(url): - """Returns registered AuthObject for given url if available.""" - - for (autoauth_url, auth) in AUTOAUTHS: - if autoauth_url in url: - return auth - - return None + """Returns registered AuthObject for given url if available.""" + + for (autoauth_url, auth) in AUTOAUTHS: + if autoauth_url in url: + return auth + + return None class RequestException(Exception): - """There was an ambiguous exception that occured while handling your + """There was an ambiguous exception that occured while handling your request.""" class AuthenticationError(RequestException): - """The authentication credentials provided were invalid.""" - + """The authentication credentials provided were invalid.""" + class URLRequired(RequestException): - """A valid URL is required to make a request.""" - + """A valid URL is required to make a request.""" + class InvalidMethod(RequestException): - """An inappropriate method was attempted.""" + """An inappropriate method was attempted.""" diff --git a/setup.py b/setup.py index 58edb452..4df796ea 100644 --- a/setup.py +++ b/setup.py @@ -7,45 +7,45 @@ import sys from distutils.core import setup - + if sys.argv[-1] == "publish": - os.system("python setup.py sdist upload") - sys.exit() + os.system("python setup.py sdist upload") + sys.exit() if sys.argv[-1] == "test": - os.system("python test_requests.py") - sys.exit() - + os.system("python test_requests.py") + sys.exit() + required = [] # if python > 2.6, require simplejson setup( - name='requests', - 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(), - author='Kenneth Reitz', - author_email='me@kennethreitz.com', - url='https://github.com/kennethreitz/requests', - packages= [ - 'requests', - 'requests.packages', - 'requests.packages.poster' - ], - install_requires=required, - license='ISC', - classifiers=( - # 'Development Status :: 5 - Production/Stable', - 'Intended Audience :: Developers', - 'Natural Language :: English', - 'License :: OSI Approved :: ISC License (ISCL)', - 'Programming Language :: Python', + name='requests', + 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(), + author='Kenneth Reitz', + author_email='me@kennethreitz.com', + url='https://github.com/kennethreitz/requests', + packages= [ + 'requests', + 'requests.packages', + 'requests.packages.poster' + ], + install_requires=required, + license='ISC', + classifiers=( + # 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'Natural Language :: English', + 'License :: OSI Approved :: ISC License (ISCL)', + 'Programming Language :: Python', # 'Programming Language :: Python :: 2.5', 'Programming Language :: Python :: 2.6', - 'Programming Language :: Python :: 2.7', - # 'Programming Language :: Python :: 3.0', - # 'Programming Language :: Python :: 3.1', - ), + 'Programming Language :: Python :: 2.7', + # 'Programming Language :: Python :: 3.0', + # 'Programming Language :: Python :: 3.1', + ), ) diff --git a/test_requests.py b/test_requests.py index 2fda7ee8..7ea35343 100644 --- a/test_requests.py +++ b/test_requests.py @@ -8,80 +8,80 @@ import requests class RequestsTestSuite(unittest.TestCase): - """Requests test cases.""" - - def setUp(self): - pass + """Requests test cases.""" + + def setUp(self): + pass - def tearDown(self): - """Teardown.""" - pass - - def test_invalid_url(self): - self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw') + def tearDown(self): + """Teardown.""" + pass + + 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_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_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_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_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) + 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) + self.assertEqual(r.status_code, 200) - def test_POSTBIN_GET_POST_FILES(self): + def test_POSTBIN_GET_POST_FILES(self): - bin = requests.post('http://www.postbin.org/') - self.assertEqual(bin.status_code, 200) + bin = requests.post('http://www.postbin.org/') + self.assertEqual(bin.status_code, 200) - post = requests.post(bin.url, data={'some': 'data'}) - self.assertEqual(post.status_code, 201) + post = requests.post(bin.url, data={'some': 'data'}) + self.assertEqual(post.status_code, 201) - post2 = requests.post(bin.url, files={'some': StringIO('data')}) - self.assertEqual(post2.status_code, 201) + post2 = requests.post(bin.url, files={'some': StringIO('data')}) + 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_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_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) + 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() + r = requests.get('http://google.com/') + self.assertFalse(r.error) + r.raise_for_status() if __name__ == '__main__': - unittest.main() + unittest.main() From 7b6ed48a86bc8bb4619445b5da859b7d603cd558 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 17 Feb 2011 19:38:40 -0500 Subject: [PATCH 10/11] tox setup for 2.4-2.7 --- test_suite.sh | 2 +- tox.ini | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 tox.ini diff --git a/test_suite.sh b/test_suite.sh index bcea4c2c..86f8cd30 100755 --- a/test_suite.sh +++ b/test_suite.sh @@ -1,4 +1,4 @@ -nosetests test_requests.py --with-xunit --with-coverage +tox coverage xml rm -fr pylint.txt pylint -d W0312 -d W0212 -d E1101 -d E0202 -d W0102 -d E0102 -f parseable ./requests > pylint.txt || true diff --git a/tox.ini b/tox.ini new file mode 100644 index 00000000..9a7083fb --- /dev/null +++ b/tox.ini @@ -0,0 +1,4 @@ +[tox] +envlist = py24,py25,py26,py27 +[testenv] +commands=nosetests --with-xunit --xunit-file=nosetests.xml \ No newline at end of file From d3911da65316730a3b0ebfeb3f21ceb3be3461ef Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Fri, 18 Feb 2011 03:32:10 -0500 Subject: [PATCH 11/11] tox update (usable now?) --- tox.ini | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 9a7083fb..7c7d8552 100644 --- a/tox.ini +++ b/tox.ini @@ -1,4 +1,7 @@ [tox] envlist = py24,py25,py26,py27 + [testenv] -commands=nosetests --with-xunit --xunit-file=nosetests.xml \ No newline at end of file +commands=py.test --junitxml=junit-{envname}.xml +deps = + pytest \ No newline at end of file