mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
prettier imports
This commit is contained in:
@@ -13,8 +13,10 @@
|
||||
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
import urllib
|
||||
import urllib2
|
||||
|
||||
from urllib2 import HTTPError
|
||||
|
||||
|
||||
|
||||
+25
-1
@@ -11,14 +11,17 @@
|
||||
"""
|
||||
|
||||
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
|
||||
@@ -33,6 +36,7 @@ __all__ = [
|
||||
]
|
||||
|
||||
|
||||
|
||||
class _Request(urllib2.Request):
|
||||
"""Hidden wrapper around the urllib2.Request object. Allows for manual
|
||||
setting of HTTP methods.
|
||||
@@ -84,6 +88,7 @@ class Request(object):
|
||||
def __repr__(self):
|
||||
return '<Request [%s]>' % (self.method)
|
||||
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if (name == 'method') and (value):
|
||||
if not value in self._METHODS:
|
||||
@@ -91,12 +96,14 @@ class Request(object):
|
||||
|
||||
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."""
|
||||
|
||||
@@ -176,6 +183,7 @@ class Request(object):
|
||||
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
|
||||
@@ -191,13 +199,16 @@ class Response(object):
|
||||
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:
|
||||
@@ -217,13 +228,16 @@ class AuthManager(object):
|
||||
|
||||
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."""
|
||||
|
||||
@@ -242,6 +256,7 @@ class AuthManager(object):
|
||||
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)
|
||||
@@ -251,10 +266,12 @@ class AuthManager(object):
|
||||
|
||||
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
|
||||
@@ -278,6 +295,7 @@ class AuthManager(object):
|
||||
authority = "%s:%d" % (host, dport)
|
||||
return authority, path
|
||||
|
||||
|
||||
def is_suburi(self, base, test):
|
||||
"""Check if test is below base in a URI tree
|
||||
|
||||
@@ -292,9 +310,11 @@ class AuthManager(object):
|
||||
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):
|
||||
@@ -304,6 +324,7 @@ class AuthManager(object):
|
||||
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):
|
||||
@@ -319,6 +340,7 @@ class AuthManager(object):
|
||||
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
|
||||
@@ -349,6 +371,8 @@ class AuthObject(object):
|
||||
self.handler = handler
|
||||
|
||||
|
||||
|
||||
|
||||
def request(method, url, **kwargs):
|
||||
"""Sends a `method` request. Returns :class:`Response` object.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user