You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.
  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 body of 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. <Response [200]>
  45. """
  46. # By using the 'with' statement we are sure the session is closed, thus we
  47. # avoid leaving sockets open which can trigger a ResourceWarning in some
  48. # cases, and look like a memory leak in others.
  49. with sessions.Session() as session:
  50. return session.request(method=method, url=url, **kwargs)
  51. def get(url, params=None, **kwargs):
  52. r"""Sends a GET request.
  53. :param url: URL for the new :class:`Request` object.
  54. :param params: (optional) Dictionary, list of tuples or bytes to send
  55. in the body of the :class:`Request`.
  56. :param \*\*kwargs: Optional arguments that ``request`` takes.
  57. :return: :class:`Response <Response>` object
  58. :rtype: requests.Response
  59. """
  60. kwargs.setdefault('allow_redirects', True)
  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. kwargs.setdefault('allow_redirects', True)
  70. return request('options', url, **kwargs)
  71. def head(url, **kwargs):
  72. r"""Sends a HEAD request.
  73. :param url: URL for the new :class:`Request` object.
  74. :param \*\*kwargs: Optional arguments that ``request`` takes.
  75. :return: :class:`Response <Response>` object
  76. :rtype: requests.Response
  77. """
  78. kwargs.setdefault('allow_redirects', False)
  79. return request('head', url, **kwargs)
  80. def post(url, data=None, json=None, **kwargs):
  81. r"""Sends a POST request.
  82. :param url: URL for the new :class:`Request` object.
  83. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  84. object to send in the body of the :class:`Request`.
  85. :param json: (optional) json data to send in the body of the :class:`Request`.
  86. :param \*\*kwargs: Optional arguments that ``request`` takes.
  87. :return: :class:`Response <Response>` object
  88. :rtype: requests.Response
  89. """
  90. return request('post', url, data=data, json=json, **kwargs)
  91. def put(url, data=None, **kwargs):
  92. r"""Sends a PUT request.
  93. :param url: URL for the new :class:`Request` object.
  94. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  95. object to send in the body of the :class:`Request`.
  96. :param json: (optional) json data to send in the body of the :class:`Request`.
  97. :param \*\*kwargs: Optional arguments that ``request`` takes.
  98. :return: :class:`Response <Response>` object
  99. :rtype: requests.Response
  100. """
  101. return request('put', url, data=data, **kwargs)
  102. def patch(url, data=None, **kwargs):
  103. r"""Sends a PATCH request.
  104. :param url: URL for the new :class:`Request` object.
  105. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  106. object to send in the body of the :class:`Request`.
  107. :param json: (optional) json data to send in the body of the :class:`Request`.
  108. :param \*\*kwargs: Optional arguments that ``request`` takes.
  109. :return: :class:`Response <Response>` object
  110. :rtype: requests.Response
  111. """
  112. return request('patch', url, data=data, **kwargs)
  113. def delete(url, **kwargs):
  114. r"""Sends a DELETE request.
  115. :param url: URL for the new :class:`Request` object.
  116. :param \*\*kwargs: Optional arguments that ``request`` takes.
  117. :return: :class:`Response <Response>` object
  118. :rtype: requests.Response
  119. """
  120. return request('delete', url, **kwargs)