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.

164 lines
6.5 KiB

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