Development of an internal social media platform with personalised dashboards for students
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

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