api.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding: utf-8 -*-
  2. """
  3. requests.api
  4. ~~~~~~~~~~~~
  5. This module implements the Requests API.
  6. :copyright: (c) 2012 by Kenneth Reitz.
  7. :license: Apache2, see LICENSE for more details.
  8. """
  9. from . import sessions
  10. def request(method, url, **kwargs):
  11. """Constructs and sends a :class:`Request <Request>`.
  12. :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
  13. :param url: URL for the new :class:`Request` object.
  14. :param params: (optional) Dictionary, list of tuples or bytes to send
  15. in the query string for the :class:`Request`.
  16. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  17. object to send in the body of the :class:`Request`.
  18. :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
  19. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
  20. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
  21. :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
  22. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
  23. or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
  24. defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
  25. to add for the file.
  26. :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
  27. :param timeout: (optional) How many seconds to wait for the server to send data
  28. before giving up, as a float, or a :ref:`(connect timeout, read
  29. timeout) <timeouts>` tuple.
  30. :type timeout: float or tuple
  31. :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
  32. :type allow_redirects: bool
  33. :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
  34. :param verify: (optional) Either a boolean, in which case it controls whether we verify
  35. the server's TLS certificate, or a string, in which case it must be a path
  36. to a CA bundle to use. Defaults to ``True``.
  37. :param stream: (optional) if ``False``, the response content will be immediately downloaded.
  38. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
  39. :return: :class:`Response <Response>` object
  40. :rtype: requests.Response
  41. Usage::
  42. >>> import requests
  43. >>> req = requests.request('GET', 'https://httpbin.org/get')
  44. >>> req
  45. <Response [200]>
  46. """
  47. # By using the 'with' statement we are sure the session is closed, thus we
  48. # avoid leaving sockets open which can trigger a ResourceWarning in some
  49. # cases, and look like a memory leak in others.
  50. with sessions.Session() as session:
  51. return session.request(method=method, url=url, **kwargs)
  52. def get(url, params=None, **kwargs):
  53. r"""Sends a GET request.
  54. :param url: URL for the new :class:`Request` object.
  55. :param params: (optional) Dictionary, list of tuples or bytes to send
  56. in the query string for the :class:`Request`.
  57. :param \*\*kwargs: Optional arguments that ``request`` takes.
  58. :return: :class:`Response <Response>` object
  59. :rtype: requests.Response
  60. """
  61. return request('get', url, params=params, **kwargs)
  62. def options(url, **kwargs):
  63. r"""Sends an OPTIONS request.
  64. :param url: URL for the new :class:`Request` object.
  65. :param \*\*kwargs: Optional arguments that ``request`` takes.
  66. :return: :class:`Response <Response>` object
  67. :rtype: requests.Response
  68. """
  69. return request('options', url, **kwargs)
  70. def head(url, **kwargs):
  71. r"""Sends a HEAD request.
  72. :param url: URL for the new :class:`Request` object.
  73. :param \*\*kwargs: Optional arguments that ``request`` takes. If
  74. `allow_redirects` is not provided, it will be set to `False` (as
  75. opposed to the default :meth:`request` behavior).
  76. :return: :class:`Response <Response>` object
  77. :rtype: requests.Response
  78. """
  79. kwargs.setdefault('allow_redirects', False)
  80. return request('head', url, **kwargs)
  81. def post(url, data=None, json=None, **kwargs):
  82. r"""Sends a POST request.
  83. :param url: URL for the new :class:`Request` object.
  84. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  85. object to send in the body of the :class:`Request`.
  86. :param json: (optional) json data to send in the body of the :class:`Request`.
  87. :param \*\*kwargs: Optional arguments that ``request`` takes.
  88. :return: :class:`Response <Response>` object
  89. :rtype: requests.Response
  90. """
  91. return request('post', url, data=data, json=json, **kwargs)
  92. def put(url, data=None, **kwargs):
  93. r"""Sends a PUT request.
  94. :param url: URL for the new :class:`Request` object.
  95. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  96. object to send in the body of the :class:`Request`.
  97. :param json: (optional) json data to send in the body of the :class:`Request`.
  98. :param \*\*kwargs: Optional arguments that ``request`` takes.
  99. :return: :class:`Response <Response>` object
  100. :rtype: requests.Response
  101. """
  102. return request('put', url, data=data, **kwargs)
  103. def patch(url, data=None, **kwargs):
  104. r"""Sends a PATCH request.
  105. :param url: URL for the new :class:`Request` object.
  106. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  107. object to send in the body of the :class:`Request`.
  108. :param json: (optional) json data to send in the body of the :class:`Request`.
  109. :param \*\*kwargs: Optional arguments that ``request`` takes.
  110. :return: :class:`Response <Response>` object
  111. :rtype: requests.Response
  112. """
  113. return request('patch', url, data=data, **kwargs)
  114. def delete(url, **kwargs):
  115. r"""Sends a DELETE request.
  116. :param url: URL for the new :class:`Request` object.
  117. :param \*\*kwargs: Optional arguments that ``request`` takes.
  118. :return: :class:`Response <Response>` object
  119. :rtype: requests.Response
  120. """
  121. return request('delete', url, **kwargs)