mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
added toa AUTHORS file
This commit is contained in:
@@ -43,5 +43,6 @@ Patches and Suggestions
|
||||
- Rick Mak
|
||||
- Johan Bergström
|
||||
- Josselin Jacquard
|
||||
- Michael Van Veen <michael@mvanveen.net>
|
||||
- Mike Waldner
|
||||
- Serge Domkowski
|
||||
- Serge Domkowski
|
||||
|
||||
+11
-11
@@ -54,17 +54,17 @@ def request(method, url,
|
||||
headers[k] = header_expand(v)
|
||||
|
||||
args = dict(
|
||||
method = method,
|
||||
url = url,
|
||||
data = data,
|
||||
params = params,
|
||||
headers = headers,
|
||||
cookiejar = cookies,
|
||||
files = files,
|
||||
auth = auth,
|
||||
timeout = timeout or config.settings.timeout,
|
||||
allow_redirects = allow_redirects,
|
||||
proxies = proxies or config.settings.proxies,
|
||||
method=method,
|
||||
url=url,
|
||||
data=data,
|
||||
params=params,
|
||||
headers=headers,
|
||||
cookiejar=cookies,
|
||||
files=files,
|
||||
auth=auth,
|
||||
timeout=timeout or config.settings.timeout,
|
||||
allow_redirects=allow_redirects,
|
||||
proxies=proxies or config.settings.proxies,
|
||||
)
|
||||
|
||||
# Arguments manipulation hook.
|
||||
|
||||
@@ -6,8 +6,23 @@ requests.config
|
||||
|
||||
This module provides the Requests settings feature set.
|
||||
|
||||
settings parameters:
|
||||
|
||||
TODO: Verify!!!
|
||||
TODO: Make sure format is acceptabl/cool
|
||||
- :base_headers: - Sets default User-Agent to `python-requests.org`
|
||||
- :accept_gzip: - Whether or not to accept gzip-compressed data
|
||||
- :proxies: - http proxies?
|
||||
- :verbose: - display verbose information?
|
||||
- :timeout: - timeout time until request terminates
|
||||
- :max_redirects: - maximum number of allowed redirects?
|
||||
- :decode_unicode: - whether or not to accept unicode?
|
||||
|
||||
Used globally
|
||||
|
||||
"""
|
||||
|
||||
|
||||
class Settings(object):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
|
||||
+1
-1
@@ -26,4 +26,4 @@ from sessions import session
|
||||
from status_codes import codes
|
||||
from config import settings
|
||||
|
||||
import utils
|
||||
import utils
|
||||
|
||||
@@ -6,21 +6,27 @@ requests.exceptions
|
||||
|
||||
"""
|
||||
|
||||
|
||||
class RequestException(Exception):
|
||||
"""There was an ambiguous exception that occured while handling your
|
||||
request."""
|
||||
|
||||
|
||||
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."""
|
||||
|
||||
+21
-42
@@ -30,7 +30,6 @@ from .exceptions import RequestException, AuthenticationError, Timeout, URLRequi
|
||||
REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)
|
||||
|
||||
|
||||
|
||||
class Request(object):
|
||||
"""The :class:`Request <Request>` object. It carries out all functionality of
|
||||
Requests. Recommended interface is with the Requests functions.
|
||||
@@ -96,7 +95,6 @@ class Request(object):
|
||||
#: True if Request has been sent.
|
||||
self.sent = False
|
||||
|
||||
|
||||
# Header manipulation and defaults.
|
||||
|
||||
if settings.accept_gzip:
|
||||
@@ -113,18 +111,15 @@ class Request(object):
|
||||
|
||||
self.headers = headers
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return '<Request [%s]>' % (self.method)
|
||||
|
||||
|
||||
def _checks(self):
|
||||
"""Deterministic checks for consistency."""
|
||||
|
||||
if not self.url:
|
||||
raise URLRequired
|
||||
|
||||
|
||||
def _get_opener(self):
|
||||
"""Creates appropriate opener object for urllib2."""
|
||||
|
||||
@@ -173,13 +168,11 @@ class Request(object):
|
||||
|
||||
return opener.open
|
||||
|
||||
|
||||
def _build_response(self, resp, is_error=False):
|
||||
"""Build internal :class:`Response <Response>` object
|
||||
from given response.
|
||||
"""
|
||||
|
||||
|
||||
def build(resp):
|
||||
|
||||
response = Response()
|
||||
@@ -193,7 +186,6 @@ class Request(object):
|
||||
|
||||
response.cookies = dict_from_cookiejar(self.cookiejar)
|
||||
|
||||
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
@@ -204,7 +196,6 @@ class Request(object):
|
||||
|
||||
return response
|
||||
|
||||
|
||||
history = []
|
||||
|
||||
r = build(resp)
|
||||
@@ -258,7 +249,6 @@ class Request(object):
|
||||
self.response = r
|
||||
self.response.request = self
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _encode_params(data):
|
||||
"""Encode parameters in a piece of data.
|
||||
@@ -276,22 +266,28 @@ class Request(object):
|
||||
for k, vs in data.items():
|
||||
for v in isinstance(vs, list) and vs or [vs]:
|
||||
result.append((k.encode('utf-8') if isinstance(k, unicode) else k,
|
||||
v.encode('utf-8') if isinstance(v, unicode) else v))
|
||||
return result, urllib.urlencode(result, doseq=True)
|
||||
v.encode('utf-8') if isinstance(v, unicode) else v)
|
||||
)
|
||||
return (result, urllib.urlencode(result, doseq=True))
|
||||
|
||||
else:
|
||||
return data, data
|
||||
|
||||
|
||||
def _build_url(self):
|
||||
"""Build the actual URL to use."""
|
||||
|
||||
# Support for unicode domain names and paths.
|
||||
scheme, netloc, path, params, query, fragment = urlparse(self.url)
|
||||
netloc = netloc.encode('idna')
|
||||
|
||||
if isinstance(path, unicode):
|
||||
path = path.encode('utf-8')
|
||||
|
||||
path = urllib.quote(path, safe="%/:=&?~#+!$,;'@()*[]")
|
||||
self.url = str(urlunparse([ scheme, netloc, path, params, query, fragment ]))
|
||||
|
||||
self.url = str(urlunparse(
|
||||
[scheme, netloc, path, params, query, fragment]
|
||||
))
|
||||
|
||||
if self._enc_params:
|
||||
if urlparse(self.url).query:
|
||||
@@ -301,7 +297,6 @@ class Request(object):
|
||||
else:
|
||||
return self.url
|
||||
|
||||
|
||||
def send(self, anyway=False):
|
||||
"""Sends the request. Returns True of successful, false if not.
|
||||
If there was an HTTPError during transmission,
|
||||
@@ -321,7 +316,6 @@ class Request(object):
|
||||
datetime.now().isoformat(), self.method, self.url
|
||||
))
|
||||
|
||||
|
||||
url = self._build_url()
|
||||
if self.method in ('GET', 'HEAD', 'DELETE'):
|
||||
req = _Request(url, method=self.method)
|
||||
@@ -340,7 +334,7 @@ class Request(object):
|
||||
req = _Request(url, data=self._enc_data, method=self.method)
|
||||
|
||||
if self.headers:
|
||||
for k,v in self.headers.iteritems():
|
||||
for k, v in self.headers.iteritems():
|
||||
req.add_header(k, v)
|
||||
|
||||
if not self.sent or anyway:
|
||||
@@ -381,17 +375,17 @@ class Request(object):
|
||||
self._build_response(resp)
|
||||
self.response.ok = True
|
||||
|
||||
|
||||
self.sent = self.response.ok
|
||||
|
||||
return self.sent
|
||||
|
||||
|
||||
class Response(object):
|
||||
"""The core :class:`Response <Response>` object. All
|
||||
:class:`Request <Request>` objects contain a
|
||||
:class:`response <Response>` attribute, which is an instance
|
||||
of this class.
|
||||
"""The core :class:`Response <Response>` object.
|
||||
|
||||
|
||||
All :class:`Request <Request>` objects contain a :class:`response
|
||||
<Response>` attribute, which is an instance of this class.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
@@ -430,11 +424,9 @@ class Response(object):
|
||||
#: A dictionary of Cookies the server sent back.
|
||||
self.cookies = None
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return '<Response [%s]>' % (self.status_code)
|
||||
|
||||
|
||||
def __nonzero__(self):
|
||||
"""Returns true if :attr:`status_code` is 'OK'."""
|
||||
|
||||
@@ -496,14 +488,12 @@ class Response(object):
|
||||
self._content_consumed = True
|
||||
return self._content
|
||||
|
||||
|
||||
def raise_for_status(self):
|
||||
"""Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""
|
||||
if self.error:
|
||||
raise self.error
|
||||
|
||||
|
||||
|
||||
class AuthManager(object):
|
||||
"""Requests Authentication Manager."""
|
||||
|
||||
@@ -516,16 +506,13 @@ 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."""
|
||||
|
||||
@@ -540,7 +527,6 @@ class AuthManager(object):
|
||||
|
||||
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
|
||||
@@ -553,7 +539,6 @@ 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)
|
||||
@@ -563,7 +548,6 @@ class AuthManager(object):
|
||||
|
||||
return (None, None)
|
||||
|
||||
|
||||
def get_auth(self, uri):
|
||||
(in_domain, in_path) = self.reduce_uri(uri, False)
|
||||
|
||||
@@ -574,7 +558,6 @@ class AuthManager(object):
|
||||
if path in in_path:
|
||||
return authority
|
||||
|
||||
|
||||
def reduce_uri(self, uri, default_port=True):
|
||||
"""Accept authority or URI and extract only the authority and path."""
|
||||
|
||||
@@ -603,7 +586,6 @@ class AuthManager(object):
|
||||
|
||||
return authority, path
|
||||
|
||||
|
||||
def is_suburi(self, base, test):
|
||||
"""Check if test is below base in a URI tree
|
||||
|
||||
@@ -618,11 +600,9 @@ 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):
|
||||
@@ -632,7 +612,6 @@ 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):
|
||||
@@ -648,12 +627,12 @@ 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
|
||||
Request. You can also enable Authorization for domain realms with AutoAuth.
|
||||
See AutoAuth for more details.
|
||||
"""The :class:`AuthObject` is a simple HTTP Authentication token.
|
||||
|
||||
When given to a Requests function, it enables Basic HTTP Authentication
|
||||
for that Request. You can also enable Authorization for domain realms
|
||||
with AutoAuth. See AutoAuth for more details.
|
||||
|
||||
:param username: Username to authenticate with.
|
||||
:param password: Password for given username.
|
||||
|
||||
+4
-10
@@ -8,8 +8,9 @@ Urllib2 Monkey patches.
|
||||
|
||||
"""
|
||||
|
||||
import urllib2
|
||||
import re
|
||||
import urllib2
|
||||
|
||||
|
||||
class Request(urllib2.Request):
|
||||
"""Hidden wrapper around the urllib2.Request object. Allows for manual
|
||||
@@ -35,7 +36,6 @@ class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):
|
||||
http_error_302 = http_error_303 = http_error_307 = http_error_301
|
||||
|
||||
|
||||
|
||||
class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):
|
||||
"""HTTP Basic Auth Handler with authentication loop fixes."""
|
||||
|
||||
@@ -44,14 +44,12 @@ class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):
|
||||
self.retried_req = None
|
||||
self.retried = 0
|
||||
|
||||
|
||||
def reset_retry_count(self):
|
||||
# Python 2.6.5 will call this on 401 or 407 errors and thus loop
|
||||
# forever. We disable reset_retry_count completely and reset in
|
||||
# http_error_auth_reqed instead.
|
||||
pass
|
||||
|
||||
|
||||
def http_error_auth_reqed(self, auth_header, host, req, headers):
|
||||
# Reset the retry counter once for each request.
|
||||
if req is not self.retried_req:
|
||||
@@ -63,7 +61,6 @@ class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):
|
||||
)
|
||||
|
||||
|
||||
|
||||
class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):
|
||||
"""HTTP Basic Auth Handler with forced Authentication."""
|
||||
|
||||
@@ -71,10 +68,9 @@ class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):
|
||||
rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'
|
||||
'realm=(["\'])(.*?)\\2', re.I)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
def __init__(self, *args, **kwargs):
|
||||
HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
|
||||
|
||||
|
||||
def http_error_401(self, req, fp, code, msg, headers):
|
||||
url = req.get_full_url()
|
||||
response = self._http_error_auth_reqed('www-authenticate', url, req, headers)
|
||||
@@ -83,7 +79,6 @@ class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):
|
||||
|
||||
http_error_404 = http_error_401
|
||||
|
||||
|
||||
def _http_error_auth_reqed(self, authreq, host, req, headers):
|
||||
|
||||
authreq = headers.get(authreq, None)
|
||||
@@ -116,7 +111,6 @@ class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):
|
||||
return response
|
||||
|
||||
|
||||
|
||||
class HTTPDigestAuthHandler(urllib2.HTTPDigestAuthHandler):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
@@ -145,4 +139,4 @@ class HTTPDigestAuthHandler(urllib2.HTTPDigestAuthHandler):
|
||||
arg = inst.args[0]
|
||||
if arg.startswith("AbstractDigestAuthHandler doesn't know "):
|
||||
return
|
||||
raise
|
||||
raise
|
||||
|
||||
@@ -15,13 +15,11 @@ from . import api
|
||||
from .utils import add_dict_to_cookiejar
|
||||
|
||||
|
||||
|
||||
class Session(object):
|
||||
"""A Requests session."""
|
||||
|
||||
__attrs__ = ['headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks']
|
||||
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
|
||||
# Set up a CookieJar to be used by default
|
||||
@@ -34,7 +32,6 @@ class Session(object):
|
||||
# Map and wrap requests.api methods
|
||||
self._map_api_methods()
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return '<requests-client at 0x%x>' % (id(self))
|
||||
|
||||
@@ -45,7 +42,6 @@ class Session(object):
|
||||
# print args
|
||||
pass
|
||||
|
||||
|
||||
def _map_api_methods(self):
|
||||
"""Reads each available method from requests.api and decorates
|
||||
them with a wrapper, which inserts any instance-local attributes
|
||||
@@ -81,4 +77,4 @@ class Session(object):
|
||||
def session(**kwargs):
|
||||
"""Returns a :class:`Session` for context-managment."""
|
||||
|
||||
return Session(**kwargs)
|
||||
return Session(**kwargs)
|
||||
|
||||
@@ -80,4 +80,4 @@ for (code, titles) in _codes.items():
|
||||
for title in titles:
|
||||
setattr(codes, title, code)
|
||||
if not title.startswith('\\'):
|
||||
setattr(codes, title.upper(), code)
|
||||
setattr(codes, title.upper(), code)
|
||||
|
||||
@@ -8,6 +8,7 @@ Datastructures that power Requests.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
class CaseInsensitiveDict(dict):
|
||||
"""Case-insensitive Dictionary
|
||||
|
||||
@@ -46,6 +47,7 @@ class CaseInsensitiveDict(dict):
|
||||
else:
|
||||
return default
|
||||
|
||||
|
||||
class LookupDict(dict):
|
||||
"""Dictionary lookup object."""
|
||||
|
||||
@@ -62,4 +64,4 @@ class LookupDict(dict):
|
||||
return self.__dict__.get(key, None)
|
||||
|
||||
def get(self, key, default=None):
|
||||
return self.__dict__.get(key, default)
|
||||
return self.__dict__.get(key, default)
|
||||
|
||||
+8
-7
@@ -51,10 +51,9 @@ def header_expand(headers):
|
||||
|
||||
collector.append('; '.join(_params))
|
||||
|
||||
if not len(headers) == i+1:
|
||||
if not len(headers) == i + 1:
|
||||
collector.append(', ')
|
||||
|
||||
|
||||
# Remove trailing seperators.
|
||||
if collector[-1] in (', ', '; '):
|
||||
del collector[-1]
|
||||
@@ -62,7 +61,6 @@ def header_expand(headers):
|
||||
return ''.join(collector)
|
||||
|
||||
|
||||
|
||||
def dict_from_cookiejar(cj):
|
||||
"""Returns a key/value dictionary from a CookieJar.
|
||||
|
||||
@@ -235,8 +233,8 @@ def decode_gzip(content):
|
||||
:param content: bytestring to gzip-decode.
|
||||
"""
|
||||
|
||||
return zlib.decompress(content, 16+zlib.MAX_WBITS)
|
||||
return zlib.decompress(content, 16+zlib.MAX_WBITS)
|
||||
return zlib.decompress(content, 16 + zlib.MAX_WBITS)
|
||||
return zlib.decompress(content, 16 + zlib.MAX_WBITS)
|
||||
return zlib.decompress(content, 16 + zlib.MAX_WBITS)
|
||||
|
||||
|
||||
@@ -255,6 +253,7 @@ def stream_decode_gzip(iterator):
|
||||
except zlib.error:
|
||||
pass
|
||||
|
||||
|
||||
def curl_from_request(request):
|
||||
"""Returns a curl command from the request.
|
||||
|
||||
@@ -276,10 +275,13 @@ def curl_from_request(request):
|
||||
#: -u/--user - Specify the user name and password to use for server auth.
|
||||
#: Basic Auth only for now
|
||||
auth = ''
|
||||
|
||||
if request.auth is not None:
|
||||
auth = '-u "%s:%s" ' % (request.auth.username, request.auth.password)
|
||||
|
||||
auth = '-u "%s:%s" ' % (request.auth.username, request.auth.password)
|
||||
|
||||
method = ''
|
||||
|
||||
if request.method.upper() == 'HEAD':
|
||||
#: -I/--head - fetch headers only.
|
||||
method = '-I '
|
||||
@@ -321,4 +323,3 @@ def curl_from_request(request):
|
||||
|
||||
#: Params handled in _build_url
|
||||
return curl + auth + method + header + cookies + form + '"' + request._build_url() + '"'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user