Big API update
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent f9f3b46 · Browse files · 5 files·+96 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+96 added−16 removed
- other:+96 added−16 removed
Provenance
- Commit
6e13e73- Tree
99e5bd2- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
f9f3b46
5 files changed+96 added−16 removed
modified:docs/api.rst+47 added-1 removed
1+.. _api:2+3 API4 ===5 6+.. module:: requests7 8+This part of the documentation covers all the interfaces of Requests. For9+parts where Requests depends on external libraries, we document the most10+important right here and provide links to the canonical documentation.11+12+13 Main Interface14 --------------15 16+.. autofunction:: get17+.. autofunction:: post18+.. autofunction:: put19+.. autofunction:: delete20+.. autofunction:: head21 22+.. autoclass:: requests.models.Response23+ :inherited-members:24+25+26 Exceptions27 ----------28 29+.. autoexception:: HTTPError30 31+.. autoexception:: RequestException32+33+.. autoexception:: requests.models.AuthenticationError34+.. autoexception:: requests.models.URLRequired35+.. autoexception:: requests.models.InvalidMethod36+37+38 Internals14----------39+---------40+41+These items are an internal component to Requests, and should never be42+seen by the end user (developer). This part of the API documentation43+exists for those who are extending the functionality of Requests.44+45+Functions46+~~~~~~~~~47+48+.. autofunction:: request49+50+Classes51+~~~~~~~52+53+.. autoclass:: requests.models.Request54+ :inherited-members:55+56+Structures57+~~~~~~~~~~58+59+.. autoclass:: requests.structures.CaseInsensitiveDict60+ :inherited-members:modified:docs/index.rst+1 added-1 removed
9 unmodified lines10 11 Requests is an :ref:`ISC Licensed <isc>` HTTP library, written in Python, for human beings.12 13 Most existing Python modules for sending HTTP requests are extremely verbose14-and cumbersome. Python's builtin :py:class:`urllib2 <library.urllib2>` module provides most of14+and cumbersome. Python's builtin :py:module::urllib2 module provides most of15 the HTTP capabilities you should need, but the api is thoroughly **broken**.16 It requires an *enormous* amount of work (even method overrides) to perform the simplest of tasks.17 18 Things shouldn’t be this way. Not in Python.71 unmodified linesmodified:requests/api.py+1 added-1 removed
18 unmodified lines19 20 21 22 def request(method, url, **kwargs):23- """Sends a `method` request. Returns :class:`Response` object.23+ """Sends a Constructs and sends a :class:`Request <models.Request>`. Returns :class:`Response <models.Response>` object.24 25 :param method: method for the new :class:`Request` object.26 :param url: URL for the new :class:`Request` object.27 :param params: (optional) Dictionary of GET/HEAD/DELETE Parameters to send with the :class:`Request`.88 unmodified linesmodified:requests/models.py+43 added-12 removed
21 unmodified lines22 23 24 25 class Request(object):26- """The :class:`Request` object. It carries out all functionality of26+ """The :class:`Request <models.Request>` object. It carries out all functionality of27 Requests. Recommended interface is with the Requests functions.28 """29 30 _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE')2 unmodified lines33 data=dict(), auth=None, cookiejar=None, timeout=None, redirect=True):34 35 socket.setdefaulttimeout(timeout)36 37+ #: Request URL.38 self.url = url39+ #: Dictonary of HTTP Headers to attach to the :class:`Request <models.Request>`.40 self.headers = headers41+ #: Dictionary of files to multipart upload (``{filename: content}``).42 self.files = files43+ #: HTTP Method to use. Available: GET, HEAD, PUT, POST, DELETE.44 self.method = method45+ #: Form or Byte data to attach to the :class:`Request <models.Request>`.46 self.data = dict()47+ #: True if :class:`Request <models.Request>` is part of a redirect chain (disables history48+ #: and HTTPError storage).49 self.redirect = redirect50 51 # self.data = {}52 if hasattr(data, 'items'):8 unmodified lines61 self._enc_data = urllib.urlencode(self.data)62 else:63 self._enc_data = data64 58-65+ #: :class:`Response <models.Response>` instance, containing66+ #: content and metadata of HTTP Response, once :attr:`sent <send>`.67 self.response = Response()68 69 if isinstance(auth, (list, tuple)):70 auth = AuthObject(*auth)71 if not auth:72 auth = auth_manager.get_auth(self.url)73+ #: :class:`AuthObject` to attach to :class:`Request <models.Request>`.74 self.auth = auth75+ #: CookieJar to attach to :class:`Request <models.Request>`.76 self.cookiejar = cookiejar77+ #: True if Request has been sent.78 self.sent = False79 80 81 def __repr__(self):32 unmodified lines114 _handlers.append(self.auth.handler)115 116 117 _handlers.append(HTTPRedirectHandler)107- # print _handlers108- # print '^^'109- # print '!'118 119 if not _handlers:120 return urllib2.urlopen121 13 unmodified lines135 136 return opener.open137 138 def _build_response(self, resp):131- """Build internal Response object from given response."""139+ """Build internal :class:`Response <models.Response>` object from given response."""140 141 def build(resp):142 143 response = Response()116 unmodified lines260 261 262 263 class Response(object):256- """The :class:`Request` object. All :class:`Request` objects contain a257- :class:`Request.response <response>` attribute, which is an instance of258- this class.264+ """The core :class:`Response <models.Response>` object. All265+ :class:`Request <models.Request>` objects contain a266+ :class:`response <models.Response>` attribute, which is an instance267+ of this class.268 """269 270 def __init__(self):271+ #: Raw content of the response, in bytes.272+ #: If ``content-encoding`` of response was set to ``gzip``, the273+ #: response data will be automatically deflated.274 self.content = None275+ #: Integer Code of responded HTTP Status.276 self.status_code = None277+ #: Case-insensitive Dictionary of Response Headers.278+ #: For example, ``headers['content-encoding']`` will return the279+ #: value of a ``'Content-Encoding'`` response header.280 self.headers = CaseInsensitiveDict()281+ #: Final URL location of Response.282 self.url = None283+ #: True if no :attr:`error` occured.284 self.ok = False285+ #: Resulting :class:`HTTPError` of request, if one occured.286 self.error = None287+ #: True, if the response :attr:`content` is cached locally.288 self.cached = False289+ #: A list of :class:`Response <models.Response>` objects from290+ #: the history of the Request. Any redirect responses will end291+ #: up here.292 self.history = []293 294 295 def __repr__(self):296 return '<Response [%s]>' % (self.status_code)297 298 299 def __nonzero__(self):277- """Returns true if status_code is 'OK'."""300+ """Returns true if :attr:`status_code` is 'OK'."""301 return not self.error302 303 304 def raise_for_status(self):282- """Raises stored HTTPError if one exists."""305+ """Raises stored :class:`HTTPError`, if one occured."""306 if self.error:307 raise self.error308 309 def read(self, *args):310+ """Returns :attr:`content`. Used for file-like object compatiblity."""311+312 return self.content313 314 315 316 class AuthManager(object):292- """Authentication Manager."""317+ """Requests Authentication Manager."""318 319 def __new__(cls):320 singleton = cls.__dict__.get('__singleton__')321 if singleton is not None:160 unmodified lines482 self.handler = self._handlers.get(handler.lower(), urllib2.HTTPBasicAuthHandler)483 else:484 self.handler = handler485 486+487+# ----------488+# Exceptions489+# ----------490+491+492 class RequestException(Exception):493 """There was an ambiguous exception that occured while handling your494 request."""495 8 unmodified linesmodified:requests/structures.py+4 added-1 removed
10 unmodified lines11 from UserDict import DictMixin12 13 14 class CaseInsensitiveDict(DictMixin):15- """docstring for CaseInsensitiveDict"""15+ """Case-insensitive Dictionary for :class:`Response <models.Response>` Headers.16 17+ For example, ``headers['content-encoding']`` will return the18+ value of a ``'Content-Encoding'`` response header."""19+20 def __init__(self, *args, **kwargs):21 # super(CaseInsensitiveDict, self).__init__()22 self.data = dict(*args, **kwargs)23 41 unmodified lines