"""
from __future__ import absolute_import
+
import urllib
import urllib2
+
from urllib2 import HTTPError
from .packages.poster.encode import multipart_encode
from .packages.poster.streaminghttp import register_openers, get_handlers
+
__title__ = 'requests'
__version__ = '0.3.0'
__build__ = 0x000300
]
+
class _Request(urllib2.Request):
"""Hidden wrapper around the urllib2.Request object. Allows for manual
setting of HTTP methods.
def __repr__(self):
return '<Request [%s]>' % (self.method)
+
def __setattr__(self, name, value):
if (name == 'method') and (value):
if not value in self._METHODS:
object.__setattr__(self, name, value)
+
def _checks(self):
"""Deterministic checks for consistency."""
if not self.url:
raise URLRequired
+
def _get_opener(self):
"""Creates appropriate opener object for urllib2."""
return self.sent
+
class Response(object):
"""The :class:`Request` object. All :class:`Request` objects contain a
:class:`Request.response <response>` attribute, which is an instance of
self.error = None
self.cached = False
+
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:
return singleton
+
def __init__(self):
self.passwd = {}
self._auth = {}
-
+
+
def __repr__(self):
return '<AuthManager [%s]>' % (self.method)
+
def add_auth(self, uri, auth):
"""Registers AuthObject to AuthManager."""
self.passwd[reduced_uri] = {}
self.passwd[reduced_uri] = (user, passwd)
+
def find_user_password(self, realm, authuri):
for uris, authinfo in self.passwd.iteritems():
reduced_authuri = self.reduce_uri(authuri, False)
return (None, None)
+
def get_auth(self, uri):
uri = self.reduce_uri(uri, False)
return self._auth.get(uri, None)
+
def reduce_uri(self, uri, default_port=True):
"""Accept authority or URI and extract only the authority and path."""
# note HTTP URLs do not have a userinfo component
authority = "%s:%d" % (host, dport)
return authority, path
+
def is_suburi(self, base, test):
"""Check if test is below base in a URI tree
return True
return False
+
def empty(self):
self.passwd = {}
+
def remove(self, uri, realm=None):
# uri could be a single URI or a sequence
if isinstance(uri, basestring):
reduced_uri = tuple([self.reduce_uri(u, default_port) for u in uri])
del self.passwd[reduced_uri][realm]
+
def __contains__(self, uri):
# uri could be a single URI or a sequence
if isinstance(uri, basestring):
auth_manager = AuthManager()
+
class AuthObject(object):
"""The :class:`AuthObject` is a simple HTTP Authentication token. When
given to a Requests function, it enables Basic HTTP Authentication for that
self.handler = handler
+
+
def request(method, url, **kwargs):
"""Sends a `method` request. Returns :class:`Response` object.