Permalink
Browse files

Merge branch 'release/0.3.3'

  • Loading branch information...
2 parents fec0d1a + 01b22a7 commit 7215452da57775982b1d50db868b857c530839c2 @kennethreitz committed May 12, 2011
Showing with 452 additions and 31 deletions.
  1. +1 −0 AUTHORS
  2. +8 −0 HISTORY.rst
  3. +10 −10 README.rst
  4. +2 −2 docs/conf.py
  5. +25 −0 requests/__init__.py
  6. +40 −19 requests/core.py
  7. +360 −0 requests/structures.py
  8. +6 −0 test_requests.py
View
@@ -17,3 +17,4 @@ Patches and Suggestions
- Justin Murphy
- Rob Madole
- Aram Dulyan
+- Johannes Gorset
View
@@ -1,6 +1,14 @@
History
-------
+0.3.3 (2011-05-12)
+++++++++++++++++++
+
+* Request timeouts
+* Unicode url-encoded data
+* Settings context manager and module
+
+
0.3.2 (2011-04-15)
++++++++++++++++++
View
@@ -63,24 +63,24 @@ If a {filename: fileobject} dictionary is passed in (files=...), a multipart_enc
If CookieJar object is is passed in (cookies=...), the cookies will be sent with the request.
GET Requests
- >>> request.get(url, params={}, headers={}, cookies=None, auth=None)
- <request object>
+ >>> requests.get(url, params={}, headers={}, cookies=None, auth=None)
+ <Response [200]>
HEAD Requests
- >>> request.head(url, params={}, headers={}, cookies=None, auth=None)
- <request object>
+ >>> requests.head(url, params={}, headers={}, cookies=None, auth=None)
+ <Response [200]>
PUT Requests
- >>> request.put(url, data='', headers={}, files={}, cookies=None, auth=None)
- <request object>
+ >>> requests.put(url, data='', headers={}, files={}, cookies=None, auth=None)
+ <Response [200]>
POST Requests
- >>> request.post(url, data={}, headers={}, files={}, cookies=None, auth=None)
- <request object>
+ >>> requests.post(url, data={}, headers={}, files={}, cookies=None, auth=None)
+ <Response [200]>
DELETE Requests
- >>> request.delete(url, params={}, headers={}, cookies=None, auth=None)
- <request object>
+ >>> requests.delete(url, params={}, headers={}, cookies=None, auth=None)
+ <Response [200]>
**Responses:**
View
@@ -25,7 +25,7 @@
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
-extensions = ['sphinx.ext.autodoc', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.pngmath', 'sphinx.ext.jsmath', 'sphinx.ext.viewcode']
+extensions = ['sphinx.ext.autodoc']
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
@@ -48,7 +48,7 @@
# built documents.
#
# The short X.Y version.
-version = '0.3.2'
+version = '0.3.3'
# The full version, including alpha/beta/rc tags.
release = version
View
@@ -1,6 +1,31 @@
# -*- coding: utf-8 -*-
+import inspect
+
import packages
from core import *
from core import __version__
+
+timeout = None
+
+class settings:
+ """Context manager for settings."""
+
+ cache = {}
+
+ def __init__(self, timeout):
+ self.module = inspect.getmodule(self)
+
+ # Cache settings
+ self.cache['timeout'] = self.module.timeout
+
+ self.module.timeout = timeout
+
+ def __enter__(self):
+ pass
+
+ def __exit__(self, type, value, traceback):
+ # Restore settings
+ for key in self.cache:
+ setattr(self.module, key, self.cache[key])
View
@@ -12,8 +12,10 @@
from __future__ import absolute_import
+import requests
import urllib
import urllib2
+import socket
import zlib
from urllib2 import HTTPError
@@ -23,10 +25,9 @@
from .packages.poster.streaminghttp import register_openers, get_handlers
-
__title__ = 'requests'
-__version__ = '0.3.2'
-__build__ = 0x000302
+__version__ = '0.3.3'
+__build__ = 0x000303
__author__ = 'Kenneth Reitz'
__license__ = 'ISC'
__copyright__ = 'Copyright 2011 Kenneth Reitz'
@@ -38,7 +39,6 @@
]
-
class _Request(urllib2.Request):
"""Hidden wrapper around the urllib2.Request object. Allows for manual
setting of HTTP methods.
@@ -63,19 +63,24 @@ class Request(object):
_METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE')
def __init__(self, url=None, headers=dict(), files=None, method=None,
- data=dict(), auth=None, cookiejar=None):
+ data=dict(), auth=None, cookiejar=None, timeout=None):
self.url = url
self.headers = headers
self.files = files
self.method = method
self.data = data
+ socket.setdefaulttimeout(timeout)
+
+ for (k, v) in self.data.iteritems():
+ self.data[k] = v.encode('utf-8')
+
# url encode data if it's a dict
if hasattr(data, 'items'):
- self._enc_data = urllib.urlencode(data)
+ self._enc_data = urllib.urlencode(self.data)
else:
- self._enc_data = data
+ self._enc_data = self.data
self.response = Response()
@@ -155,8 +160,9 @@ def _build_response(self, resp):
self.response.url = getattr(resp, 'url', None)
+
@staticmethod
- def _build_url(url, data):
+ def _build_url(url, data=None):
"""Build URLs."""
if urlparse(url).query:
@@ -167,6 +173,7 @@ def _build_url(url, data):
else:
return url
+
def send(self, anyway=False):
"""Sends the request. Returns True of successful, false if not.
If there was an HTTPError during transmission,
@@ -200,6 +207,9 @@ def send(self, anyway=False):
req.headers.update(self.headers)
if not self.sent or anyway:
+
+
+
try:
opener = self._get_opener()
resp = opener(req)
@@ -226,6 +236,8 @@ def send(self, anyway=False):
def read(self, *args):
return self.response.read()
+
+
class Response(object):
"""The :class:`Request` object. All :class:`Request` objects contain a
:class:`Request.response <response>` attribute, which is an instance of
@@ -260,6 +272,7 @@ def read(self, *args):
return self.content
+
class AuthManager(object):
"""Authentication Manager."""
@@ -296,6 +309,7 @@ def add_auth(self, uri, auth):
self._auth[uri] = auth
+
def add_password(self, realm, uri, user, passwd):
"""Adds password to AuthManager."""
# uri could be a single URI or a sequence
@@ -443,44 +457,48 @@ def request(method, url, **kwargs):
:param cookies: (optional) CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
+ :param timeout: (optional) Float describing the timeout of the request.
"""
data = kwargs.pop('data', dict()) or kwargs.pop('params', dict())
r = Request(method=method, url=url, data=data, headers=kwargs.pop('headers', {}),
cookiejar=kwargs.pop('cookies', None), files=kwargs.pop('files', None),
- auth=kwargs.pop('auth', auth_manager.get_auth(url)))
+ auth=kwargs.pop('auth', auth_manager.get_auth(url)),
+ timeout=kwargs.pop('timeout', requests.timeout))
r.send()
return r.response
-def get(url, params={}, headers={}, cookies=None, auth=None):
+def get(url, params={}, headers={}, cookies=None, auth=None, **kwargs):
"""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`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) CookieJar object to send with the :class:`Request`.
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
+ :param timeout: (optional) Float describing the timeout of the request.
"""
- return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth)
+ return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs)
-def head(url, params={}, headers={}, cookies=None, auth=None):
+def head(url, params={}, headers={}, cookies=None, auth=None, **kwargs):
"""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`.
:param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.
:param cookies: (optional) CookieJar object to send with the :class:`Request`.
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
+ :param timeout: (optional) Float describing the timeout of the request.
"""
- return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth)
+ return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs)
-def post(url, data={}, headers={}, files=None, cookies=None, auth=None):
+def post(url, data={}, headers={}, files=None, cookies=None, auth=None, **kwargs):
"""Sends a POST request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object.
@@ -489,12 +507,13 @@ def post(url, data={}, headers={}, files=None, cookies=None, auth=None):
:param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
:param cookies: (optional) CookieJar object to send with the :class:`Request`.
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
+ :param timeout: (optional) Float describing the timeout of the request.
"""
- return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth)
+ return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, **kwargs)
-def put(url, data='', headers={}, files={}, cookies=None, auth=None):
+def put(url, data='', headers={}, files={}, cookies=None, auth=None, **kwargs):
"""Sends a PUT request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object.
@@ -503,22 +522,24 @@ def put(url, data='', headers={}, files={}, cookies=None, auth=None):
:param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
:param cookies: (optional) CookieJar object to send with the :class:`Request`.
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
+ :param timeout: (optional) Float describing the timeout of the request.
"""
- return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth)
+ return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth, **kwargs)
-def delete(url, params={}, headers={}, cookies=None, auth=None):
+def delete(url, params={}, headers={}, cookies=None, auth=None, **kwargs):
"""Sends a DELETE request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary of DELETE Parameters to send with the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.
:param cookies: (optional) CookieJar object to send with the :class:`Request`.
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
+ :param timeout: (optional) Float describing the timeout of the request.
"""
- return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth)
+ return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth, **kwargs)
Oops, something went wrong.

0 comments on commit 7215452

Please sign in to comment.