api.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 or bytes to be sent in the query string for the :class:`Request`.
  15. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
  16. :param json: (optional) json data to send in the body of the :class:`Request`.
  17. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
  18. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
  19. :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': ('filename', fileobj)}``) for multipart encoding upload.
  20. :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
  21. :param timeout: (optional) How long to wait for the server to send data
  22. before giving up, as a float, or a (`connect timeout, read timeout
  23. <user/advanced.html#timeouts>`_) tuple.
  24. :type timeout: float or tuple
  25. :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
  26. :type allow_redirects: bool
  27. :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
  28. :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
  29. :param stream: (optional) if ``False``, the response content will be immediately downloaded.
  30. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
  31. :return: :class:`Response <Response>` object
  32. :rtype: requests.Response
  33. Usage::
  34. >>> import requests
  35. >>> req = requests.request('GET', 'http://httpbin.org/get')
  36. <Response [200]>
  37. """
  38. session = sessions.Session()
  39. response = session.request(method=method, url=url, **kwargs)
  40. # By explicitly closing the session, we avoid leaving sockets open which
  41. # can trigger a ResourceWarning in some cases, and look like a memory leak
  42. # in others.
  43. session.close()
  44. return response
  45. def get(url, **kwargs):
  46. """Sends a GET request.
  47. :param url: URL for the new :class:`Request` object.
  48. :param \*\*kwargs: Optional arguments that ``request`` takes.
  49. :return: :class:`Response <Response>` object
  50. :rtype: requests.Response
  51. """
  52. kwargs.setdefault('allow_redirects', True)
  53. return request('get', url, **kwargs)
  54. def options(url, **kwargs):
  55. """Sends a OPTIONS request.
  56. :param url: URL for the new :class:`Request` object.
  57. :param \*\*kwargs: Optional arguments that ``request`` takes.
  58. :return: :class:`Response <Response>` object
  59. :rtype: requests.Response
  60. """
  61. kwargs.setdefault('allow_redirects', True)
  62. return request('options', url, **kwargs)
  63. def head(url, **kwargs):
  64. """Sends a HEAD request.
  65. :param url: URL for the new :class:`Request` object.
  66. :param \*\*kwargs: Optional arguments that ``request`` takes.
  67. :return: :class:`Response <Response>` object
  68. :rtype: requests.Response
  69. """
  70. kwargs.setdefault('allow_redirects', False)
  71. return request('head', url, **kwargs)
  72. def post(url, data=None, json=None, **kwargs):
  73. """Sends a POST request.
  74. :param url: URL for the new :class:`Request` object.
  75. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
  76. :param json: (optional) json data to send in the body of the :class:`Request`.
  77. :param \*\*kwargs: Optional arguments that ``request`` takes.
  78. :return: :class:`Response <Response>` object
  79. :rtype: requests.Response
  80. """
  81. return request('post', url, data=data, json=json, **kwargs)
  82. def put(url, data=None, **kwargs):
  83. """Sends a PUT request.
  84. :param url: URL for the new :class:`Request` object.
  85. :param data: (optional) Dictionary, bytes, or file-like object 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('put', url, data=data, **kwargs)
  91. def patch(url, data=None, **kwargs):
  92. """Sends a PATCH request.
  93. :param url: URL for the new :class:`Request` object.
  94. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
  95. :param \*\*kwargs: Optional arguments that ``request`` takes.
  96. :return: :class:`Response <Response>` object
  97. :rtype: requests.Response
  98. """
  99. return request('patch', url, data=data, **kwargs)
  100. def delete(url, **kwargs):
  101. """Sends a DELETE request.
  102. :param url: URL for the new :class:`Request` object.
  103. :param \*\*kwargs: Optional arguments that ``request`` takes.
  104. :return: :class:`Response <Response>` object
  105. :rtype: requests.Response
  106. """
  107. return request('delete', url, **kwargs)