Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'feature/primary-sessions' into develop
  • Loading branch information
Kenneth Reitz committed Oct 22, 2011
2 parents 4d97cc7 + 2b3bd78 commit e418631
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 128 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Expand Up @@ -4,6 +4,8 @@ History
0.6.7
+++++

* Sessions are now the primary interface.
* Deprecated InvalidMethodException.
* PATCH fix.


Expand Down
6 changes: 3 additions & 3 deletions requests/__init__.py
Expand Up @@ -15,8 +15,8 @@
"""

__title__ = 'requests'
__version__ = '0.6.6'
__build__ = 0x000606
__version__ = '0.6.7'
__build__ = 0x000607
__author__ = 'Kenneth Reitz'
__license__ = 'ISC'
__copyright__ = 'Copyright 2011 Kenneth Reitz'
Expand All @@ -30,5 +30,5 @@
from .config import settings
from .exceptions import (
RequestException, AuthenticationError, Timeout, URLRequired,
InvalidMethod, TooManyRedirects
TooManyRedirects
)
78 changes: 6 additions & 72 deletions requests/api.py
Expand Up @@ -11,96 +11,30 @@
"""

from . import config
from .models import Request, Response, AuthObject
from .status_codes import codes
from .hooks import dispatch_hook
from .utils import cookiejar_from_dict, header_expand

from .sessions import session

__all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete')


def request(method, url,
params=None, data=None, headers=None, cookies=None, files=None, auth=None,
timeout=None, allow_redirects=False, proxies=None, hooks=None, return_response=True):

"""Constructs and sends a :class:`Request <Request>`.
Returns :class:`Response <Response>` object.
:param method: method for the new :class:`Request` object.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or 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.
:param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param return_response: (optional) If False, an un-sent Request object will returned.
"""

method = str(method).upper()

if cookies is None:
cookies = {}

cookies = cookiejar_from_dict(cookies)

# Expand header values
if headers:
for k, v in headers.items() or {}:
headers[k] = header_expand(v)

args = dict(
method = method,
url = url,
data = data,
params = params,
headers = headers,
cookiejar = cookies,
files = files,
auth = auth,
hooks = hooks,
timeout = timeout or config.settings.timeout,
allow_redirects = allow_redirects,
proxies = proxies or config.settings.proxies,
s = session()
return s.request(
method, url, params, data, headers, cookies, files, auth,
timeout, allow_redirects, proxies, hooks, return_response
)

# Arguments manipulation hook.
args = dispatch_hook('args', hooks, args)

r = Request(**args)

# Pre-request hook.
r = dispatch_hook('pre_request', hooks, r)

# Don't send if asked nicely.
if not return_response:
return r

# Send the HTTP Request.
r.send()

# Post-request hook.
r = dispatch_hook('post_request', hooks, r)

# Response manipulation hook.
r.response = dispatch_hook('response', hooks, r.response)

return r.response


def get(url, **kwargs):

"""Sends a GET request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object.
:param **kwargs: Optional arguments that ``request`` takes.
"""


kwargs.setdefault('allow_redirects', True)
return request('GET', url, **kwargs)

Expand Down
5 changes: 1 addition & 4 deletions requests/exceptions.py
Expand Up @@ -12,15 +12,12 @@ class RequestException(Exception):

class AuthenticationError(RequestException):
"""The authentication credentials provided were invalid."""

class Timeout(RequestException):
"""The request timed out."""

class URLRequired(RequestException):
"""A valid URL is required to make a request."""

class InvalidMethod(RequestException):
"""An inappropriate method was attempted."""

class TooManyRedirects(RequestException):
"""Too many redirects."""
20 changes: 10 additions & 10 deletions requests/models.py
Expand Up @@ -24,7 +24,7 @@
from .packages.poster.streaminghttp import register_openers, get_handlers
from .utils import dict_from_cookiejar, get_unicode_from_response, stream_decode_response_unicode, decode_gzip, stream_decode_gzip
from .status_codes import codes
from .exceptions import RequestException, AuthenticationError, Timeout, URLRequired, InvalidMethod, TooManyRedirects
from .exceptions import Timeout, URLRequired, TooManyRedirects


REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)
Expand All @@ -38,7 +38,7 @@ class Request(object):

def __init__(self,
url=None, headers=dict(), files=None, method=None, data=dict(),
params=dict(), auth=None, cookiejar=None, timeout=None, redirect=False,
params=dict(), auth=None, cookies=None, timeout=None, redirect=False,
allow_redirects=False, proxies=None, hooks=None):

#: Float describes the timeout of the request.
Expand Down Expand Up @@ -91,7 +91,7 @@ def __init__(self,
self.auth = auth

#: CookieJar to attach to :class:`Request <Request>`.
self.cookiejar = cookiejar
self.cookies = cookies

#: True if Request has been sent.
self.sent = False
Expand Down Expand Up @@ -132,8 +132,8 @@ def _get_opener(self):

_handlers = []

if self.cookiejar is not None:
_handlers.append(urllib2.HTTPCookieProcessor(self.cookiejar))
if self.cookies is not None:
_handlers.append(urllib2.HTTPCookieProcessor(self.cookies))

if self.auth:
if not isinstance(self.auth.handler,
Expand Down Expand Up @@ -191,8 +191,8 @@ def build(resp):
response.headers = CaseInsensitiveDict(getattr(resp.info(), 'dict', None))
response.raw = resp

if self.cookiejar:
response.cookies = dict_from_cookiejar(self.cookiejar)
if self.cookies:
response.cookies = dict_from_cookiejar(self.cookies)


except AttributeError:
Expand Down Expand Up @@ -244,7 +244,7 @@ def build(resp):

request = Request(
url, self.headers, self.files, method,
self.data, self.params, self.auth, self.cookiejar,
self.data, self.params, self.auth, self.cookies,
redirect=True
)
request.send()
Expand Down Expand Up @@ -364,8 +364,8 @@ def send(self, anyway=False):
# restore global timeout
socket.setdefaulttimeout(old_timeout)

if self.cookiejar is not None:
self.cookiejar.extract_cookies(resp, req)
if self.cookies is not None:
self.cookies.extract_cookies(resp, req)

except (urllib2.HTTPError, urllib2.URLError), why:
if hasattr(why, 'reason'):
Expand Down

0 comments on commit e418631

Please sign in to comment.