Skip to content

Commit

Permalink
prettier imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Reitz committed Feb 25, 2011
1 parent ccb7fdd commit 14ba99f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions requests/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@


from __future__ import absolute_import

import urllib
import urllib2

from urllib2 import HTTPError


Expand Down
26 changes: 25 additions & 1 deletion requests/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +36,7 @@
]



class _Request(urllib2.Request):
"""Hidden wrapper around the urllib2.Request object. Allows for manual
setting of HTTP methods.
Expand Down Expand Up @@ -84,19 +88,22 @@ def __init__(self, url=None, headers=dict(), files=None, method=None,
def __repr__(self):
return '<Request [%s]>' % (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


def _get_opener(self):
"""Creates appropriate opener object for urllib2."""

Expand Down Expand Up @@ -176,6 +183,7 @@ def send(self, anyway=False):
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
Expand All @@ -191,13 +199,16 @@ def __init__(self):
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:
Expand All @@ -217,13 +228,16 @@ def __new__(cls):

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."""

Expand All @@ -242,6 +256,7 @@ def add_password(self, realm, uri, user, passwd):
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)
Expand All @@ -251,10 +266,12 @@ def find_user_password(self, realm, authuri):

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
Expand All @@ -278,6 +295,7 @@ def reduce_uri(self, uri, default_port=True):
authority = "%s:%d" % (host, dport)
return authority, path


def is_suburi(self, base, test):
"""Check if test is below base in a URI tree
Expand All @@ -292,9 +310,11 @@ def is_suburi(self, base, test):
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):
Expand All @@ -304,6 +324,7 @@ def remove(self, uri, realm=None):
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):
Expand All @@ -319,6 +340,7 @@ def __contains__(self, uri):
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
Expand Down Expand Up @@ -349,6 +371,8 @@ def __init__(self, username, password, handler='basic', realm=None):
self.handler = handler




def request(method, url, **kwargs):
"""Sends a `method` request. Returns :class:`Response` object.
Expand Down

0 comments on commit 14ba99f

Please sign in to comment.