Path: blob/master/venv/Lib/site-packages/requests/api.py
811 views
# -*- coding: utf-8 -*-12"""3requests.api4~~~~~~~~~~~~56This module implements the Requests API.78:copyright: (c) 2012 by Kenneth Reitz.9:license: Apache2, see LICENSE for more details.10"""1112from . import sessions131415def request(method, url, **kwargs):16"""Constructs and sends a :class:`Request <Request>`.1718:param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.19:param url: URL for the new :class:`Request` object.20:param params: (optional) Dictionary, list of tuples or bytes to send21in the query string for the :class:`Request`.22:param data: (optional) Dictionary, list of tuples, bytes, or file-like23object to send in the body of the :class:`Request`.24:param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.25:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.26:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.27:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.28``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``29or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string30defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers31to add for the file.32:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.33:param timeout: (optional) How many seconds to wait for the server to send data34before giving up, as a float, or a :ref:`(connect timeout, read35timeout) <timeouts>` tuple.36:type timeout: float or tuple37:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.38:type allow_redirects: bool39:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.40:param verify: (optional) Either a boolean, in which case it controls whether we verify41the server's TLS certificate, or a string, in which case it must be a path42to a CA bundle to use. Defaults to ``True``.43:param stream: (optional) if ``False``, the response content will be immediately downloaded.44:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.45:return: :class:`Response <Response>` object46:rtype: requests.Response4748Usage::4950>>> import requests51>>> req = requests.request('GET', 'https://httpbin.org/get')52>>> req53<Response [200]>54"""5556# By using the 'with' statement we are sure the session is closed, thus we57# avoid leaving sockets open which can trigger a ResourceWarning in some58# cases, and look like a memory leak in others.59with sessions.Session() as session:60return session.request(method=method, url=url, **kwargs)616263def get(url, params=None, **kwargs):64r"""Sends a GET request.6566:param url: URL for the new :class:`Request` object.67:param params: (optional) Dictionary, list of tuples or bytes to send68in the query string for the :class:`Request`.69:param \*\*kwargs: Optional arguments that ``request`` takes.70:return: :class:`Response <Response>` object71:rtype: requests.Response72"""7374kwargs.setdefault('allow_redirects', True)75return request('get', url, params=params, **kwargs)767778def options(url, **kwargs):79r"""Sends an OPTIONS request.8081:param url: URL for the new :class:`Request` object.82:param \*\*kwargs: Optional arguments that ``request`` takes.83:return: :class:`Response <Response>` object84:rtype: requests.Response85"""8687kwargs.setdefault('allow_redirects', True)88return request('options', url, **kwargs)899091def head(url, **kwargs):92r"""Sends a HEAD request.9394:param url: URL for the new :class:`Request` object.95:param \*\*kwargs: Optional arguments that ``request`` takes. If96`allow_redirects` is not provided, it will be set to `False` (as97opposed to the default :meth:`request` behavior).98:return: :class:`Response <Response>` object99:rtype: requests.Response100"""101102kwargs.setdefault('allow_redirects', False)103return request('head', url, **kwargs)104105106def post(url, data=None, json=None, **kwargs):107r"""Sends a POST request.108109:param url: URL for the new :class:`Request` object.110:param data: (optional) Dictionary, list of tuples, bytes, or file-like111object to send in the body of the :class:`Request`.112:param json: (optional) json data to send in the body of the :class:`Request`.113:param \*\*kwargs: Optional arguments that ``request`` takes.114:return: :class:`Response <Response>` object115:rtype: requests.Response116"""117118return request('post', url, data=data, json=json, **kwargs)119120121def put(url, data=None, **kwargs):122r"""Sends a PUT request.123124:param url: URL for the new :class:`Request` object.125:param data: (optional) Dictionary, list of tuples, bytes, or file-like126object to send in the body of the :class:`Request`.127:param json: (optional) json data to send in the body of the :class:`Request`.128:param \*\*kwargs: Optional arguments that ``request`` takes.129:return: :class:`Response <Response>` object130:rtype: requests.Response131"""132133return request('put', url, data=data, **kwargs)134135136def patch(url, data=None, **kwargs):137r"""Sends a PATCH request.138139:param url: URL for the new :class:`Request` object.140:param data: (optional) Dictionary, list of tuples, bytes, or file-like141object to send in the body of the :class:`Request`.142:param json: (optional) json data to send in the body of the :class:`Request`.143:param \*\*kwargs: Optional arguments that ``request`` takes.144:return: :class:`Response <Response>` object145:rtype: requests.Response146"""147148return request('patch', url, data=data, **kwargs)149150151def delete(url, **kwargs):152r"""Sends a DELETE request.153154:param url: URL for the new :class:`Request` object.155:param \*\*kwargs: Optional arguments that ``request`` takes.156:return: :class:`Response <Response>` object157:rtype: requests.Response158"""159160return request('delete', url, **kwargs)161162163