api.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, params=None, **kwargs):
  46. """Sends a GET request.
  47. :param url: URL for the new :class:`Request` object.
  48. :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
  49. :param \*\*kwargs: Optional arguments that ``request`` takes.
  50. :return: :class:`Response <Response>` object
  51. :rtype: requests.Response
  52. """
  53. kwargs.setdefault('allow_redirects', True)
  54. return request('get', url, params=params, **kwargs)
  55. def options(url, **kwargs):
  56. """Sends a OPTIONS request.
  57. :param url: URL for the new :class:`Request` object.
  58. :param \*\*kwargs: Optional arguments that ``request`` takes.
  59. :return: :class:`Response <Response>` object
  60. :rtype: requests.Response
  61. """
  62. kwargs.setdefault('allow_redirects', True)
  63. return request('options', url, **kwargs)
  64. def head(url, **kwargs):
  65. """Sends a HEAD request.
  66. :param url: URL for the new :class:`Request` object.
  67. :param \*\*kwargs: Optional arguments that ``request`` takes.
  68. :return: :class:`Response <Response>` object
  69. :rtype: requests.Response
  70. """
  71. kwargs.setdefault('allow_redirects', False)
  72. return request('head', url, **kwargs)
  73. def post(url, data=None, json=None, **kwargs):
  74. """Sends a POST request.
  75. :param url: URL for the new :class:`Request` object.
  76. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
  77. :param json: (optional) json data to send in the body of the :class:`Request`.
  78. :param \*\*kwargs: Optional arguments that ``request`` takes.
  79. :return: :class:`Response <Response>` object
  80. :rtype: requests.Response
  81. """
  82. return request('post', url, data=data, json=json, **kwargs)
  83. def put(url, data=None, **kwargs):
  84. """Sends a PUT request.
  85. :param url: URL for the new :class:`Request` object.
  86. :param data: (optional) Dictionary, bytes, or file-like object 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('put', url, data=data, **kwargs)
  92. def patch(url, data=None, **kwargs):
  93. """Sends a PATCH request.
  94. :param url: URL for the new :class:`Request` object.
  95. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
  96. :param \*\*kwargs: Optional arguments that ``request`` takes.
  97. :return: :class:`Response <Response>` object
  98. :rtype: requests.Response
  99. """
  100. return request('patch', url, data=data, **kwargs)
  101. def delete(url, **kwargs):
  102. """Sends a DELETE request.
  103. :param url: URL for the new :class:`Request` object.
  104. :param \*\*kwargs: Optional arguments that ``request`` takes.
  105. :return: :class:`Response <Response>` object
  106. :rtype: requests.Response
  107. """
  108. return request('delete', url, **kwargs)