Skip to content
Myr

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

5 files+96 added−16 removed

modified:docs/api.rst+47 added-1 removed
1.. _api:23API4===56.. module:: requests78This part of the documentation covers all the interfaces of Requests. For9parts where Requests depends on external libraries, we document the most10important right here and provide links to the canonical documentation.111213Main Interface14--------------1516.. autofunction:: get17.. autofunction:: post18.. autofunction:: put19.. autofunction:: delete20.. autofunction:: head2122.. autoclass:: requests.models.Response23 :inherited-members:242526Exceptions27----------2829.. autoexception:: HTTPError3031.. autoexception:: RequestException3233.. autoexception:: requests.models.AuthenticationError34.. autoexception:: requests.models.URLRequired35.. autoexception:: requests.models.InvalidMethod363738Internals14---------39---------4041These items are an internal component to Requests, and should never be42seen by the end user (developer). This part of the API documentation43exists for those who are extending the functionality of Requests.4445Functions46~~~~~~~~~4748.. autofunction:: request4950Classes51~~~~~~~5253.. autoclass:: requests.models.Request54 :inherited-members:5556Structures57~~~~~~~~~~5859.. autoclass:: requests.structures.CaseInsensitiveDict60 :inherited-members:
modified:docs/index.rst+1 added-1 removed
9 unmodified lines
1011Requests is an :ref:`ISC Licensed <isc>` HTTP library, written in Python, for human beings.1213Most existing Python modules for sending HTTP requests are extremely verbose14and cumbersome. Python's builtin :py:class:`urllib2 <library.urllib2>` module provides most of14and cumbersome. Python's builtin :py:module::urllib2 module provides most of15the HTTP capabilities you should need, but the api is thoroughly **broken**.16It requires an *enormous* amount of work (even method overrides) to perform the simplest of tasks.1718Things shouldn’t be this way. Not in Python.
71 unmodified lines
modified:requests/api.py+1 added-1 removed
18 unmodified lines
19202122def 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.2425 :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 lines
modified:requests/models.py+43 added-12 removed
21 unmodified lines
22232425class 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 """2930 _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE')
2 unmodified lines
33 data=dict(), auth=None, cookiejar=None, timeout=None, redirect=True):3435 socket.setdefaulttimeout(timeout)3637 #: 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 = redirect5051 # self.data = {}52 if hasattr(data, 'items'):
8 unmodified lines
61 self._enc_data = urllib.urlencode(self.data)62 else:63 self._enc_data = data645865 #: :class:`Response <models.Response>` instance, containing66 #: content and metadata of HTTP Response, once :attr:`sent <send>`.67 self.response = Response()6869 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 = False798081 def __repr__(self):
32 unmodified lines
114 _handlers.append(self.auth.handler)115116117 _handlers.append(HTTPRedirectHandler)107 # print _handlers108 # print '^^'109 # print '!'118119 if not _handlers:120 return urllib2.urlopen121
13 unmodified lines
135136 return opener.open137138 def _build_response(self, resp):131 """Build internal Response object from given response."""139 """Build internal :class:`Response <models.Response>` object from given response."""140141 def build(resp):142143 response = Response()
116 unmodified lines
260261262263class 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 """269270 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 = []293294295 def __repr__(self):296 return '<Response [%s]>' % (self.status_code)297298299 def __nonzero__(self):277 """Returns true if status_code is 'OK'."""300 """Returns true if :attr:`status_code` is 'OK'."""301 return not self.error302303304 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.error308309 def read(self, *args):310 """Returns :attr:`content`. Used for file-like object compatiblity."""311312 return self.content313314315316class AuthManager(object):292 """Authentication Manager."""317 """Requests Authentication Manager."""318319 def __new__(cls):320 singleton = cls.__dict__.get('__singleton__')321 if singleton is not None:
160 unmodified lines
482 self.handler = self._handlers.get(handler.lower(), urllib2.HTTPBasicAuthHandler)483 else:484 self.handler = handler485486487# ----------488# Exceptions489# ----------490491492class RequestException(Exception):493 """There was an ambiguous exception that occured while handling your494 request."""495
8 unmodified lines
modified:requests/structures.py+4 added-1 removed
10 unmodified lines
11from UserDict import DictMixin121314class CaseInsensitiveDict(DictMixin):15 """docstring for CaseInsensitiveDict"""15 """Case-insensitive Dictionary for :class:`Response <models.Response>` Headers.1617 For example, ``headers['content-encoding']`` will return the18 value of a ``'Content-Encoding'`` response header."""1920 def __init__(self, *args, **kwargs):21 # super(CaseInsensitiveDict, self).__init__()22 self.data = dict(*args, **kwargs)23
41 unmodified lines