Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """
  2. requests.api
  3. ~~~~~~~~~~~~
  4. This module implements the Requests API.
  5. :copyright: (c) 2012 by Kenneth Reitz.
  6. :license: Apache2, see LICENSE for more details.
  7. """
  8. from . import sessions
  9. def request(method, url, **kwargs):
  10. """Constructs and sends a :class:`Request <Request>`.
  11. :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
  12. :param url: URL for the new :class:`Request` object.
  13. :param params: (optional) Dictionary, list of tuples or bytes to send
  14. in the query string for the :class:`Request`.
  15. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  16. object to send in the body of the :class:`Request`.
  17. :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
  18. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
  19. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
  20. :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
  21. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
  22. or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
  23. defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
  24. to add for the file.
  25. :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
  26. :param timeout: (optional) How many seconds to wait for the server to send data
  27. before giving up, as a float, or a :ref:`(connect timeout, read
  28. timeout) <timeouts>` tuple.
  29. :type timeout: float or tuple
  30. :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
  31. :type allow_redirects: bool
  32. :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
  33. :param verify: (optional) Either a boolean, in which case it controls whether we verify
  34. the server's TLS certificate, or a string, in which case it must be a path
  35. to a CA bundle to use. Defaults to ``True``.
  36. :param stream: (optional) if ``False``, the response content will be immediately downloaded.
  37. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
  38. :return: :class:`Response <Response>` object
  39. :rtype: requests.Response
  40. Usage::
  41. >>> import requests
  42. >>> req = requests.request('GET', 'https://httpbin.org/get')
  43. >>> req
  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 query string for the :class:`Request`.
  56. :param \*\*kwargs: Optional arguments that ``request`` takes.
  57. :return: :class:`Response <Response>` object
  58. :rtype: requests.Response
  59. """
  60. return request("get", url, params=params, **kwargs)
  61. def options(url, **kwargs):
  62. r"""Sends an OPTIONS request.
  63. :param url: URL for the new :class:`Request` object.
  64. :param \*\*kwargs: Optional arguments that ``request`` takes.
  65. :return: :class:`Response <Response>` object
  66. :rtype: requests.Response
  67. """
  68. return request("options", url, **kwargs)
  69. def head(url, **kwargs):
  70. r"""Sends a HEAD request.
  71. :param url: URL for the new :class:`Request` object.
  72. :param \*\*kwargs: Optional arguments that ``request`` takes. If
  73. `allow_redirects` is not provided, it will be set to `False` (as
  74. opposed to the default :meth:`request` behavior).
  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) A JSON serializable Python 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("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) A JSON serializable Python object 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) A JSON serializable Python object 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)