__init__.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. # pylint: disable=missing-module-docstring, global-statement
  4. import asyncio
  5. import threading
  6. import concurrent.futures
  7. from types import MethodType
  8. from timeit import default_timer
  9. import httpx
  10. import anyio
  11. import h2.exceptions
  12. from .network import get_network, initialize, check_network_configuration
  13. from .client import get_loop
  14. from .raise_for_httperror import raise_for_httperror
  15. # queue.SimpleQueue: Support Python 3.6
  16. try:
  17. from queue import SimpleQueue
  18. except ImportError:
  19. from queue import Empty
  20. from collections import deque
  21. class SimpleQueue:
  22. """Minimal backport of queue.SimpleQueue"""
  23. def __init__(self):
  24. self._queue = deque()
  25. self._count = threading.Semaphore(0)
  26. def put(self, item):
  27. self._queue.append(item)
  28. self._count.release()
  29. def get(self):
  30. if not self._count.acquire(True): #pylint: disable=consider-using-with
  31. raise Empty
  32. return self._queue.popleft()
  33. THREADLOCAL = threading.local()
  34. """Thread-local data is data for thread specific values."""
  35. def reset_time_for_thread():
  36. THREADLOCAL.total_time = 0
  37. def get_time_for_thread():
  38. """returns thread's total time or None"""
  39. return THREADLOCAL.__dict__.get('total_time')
  40. def set_timeout_for_thread(timeout, start_time=None):
  41. THREADLOCAL.timeout = timeout
  42. THREADLOCAL.start_time = start_time
  43. def set_context_network_name(network_name):
  44. THREADLOCAL.network = get_network(network_name)
  45. def get_context_network():
  46. """If set return thread's network.
  47. If unset, return value from :py:obj:`get_network`.
  48. """
  49. return THREADLOCAL.__dict__.get('network') or get_network()
  50. def request(method, url, **kwargs):
  51. """same as requests/requests/api.py request(...)"""
  52. time_before_request = default_timer()
  53. # timeout (httpx)
  54. if 'timeout' in kwargs:
  55. timeout = kwargs['timeout']
  56. else:
  57. timeout = getattr(THREADLOCAL, 'timeout', None)
  58. if timeout is not None:
  59. kwargs['timeout'] = timeout
  60. # 2 minutes timeout for the requests without timeout
  61. timeout = timeout or 120
  62. # ajdust actual timeout
  63. timeout += 0.2 # overhead
  64. start_time = getattr(THREADLOCAL, 'start_time', time_before_request)
  65. if start_time:
  66. timeout -= default_timer() - start_time
  67. # raise_for_error
  68. check_for_httperror = True
  69. if 'raise_for_httperror' in kwargs:
  70. check_for_httperror = kwargs['raise_for_httperror']
  71. del kwargs['raise_for_httperror']
  72. # requests compatibility
  73. if isinstance(url, bytes):
  74. url = url.decode()
  75. # network
  76. network = get_context_network()
  77. # do request
  78. future = asyncio.run_coroutine_threadsafe(network.request(method, url, **kwargs), get_loop())
  79. try:
  80. response = future.result(timeout)
  81. except concurrent.futures.TimeoutError as e:
  82. raise httpx.TimeoutException('Timeout', request=None) from e
  83. # requests compatibility
  84. # see also https://www.python-httpx.org/compatibility/#checking-for-4xx5xx-responses
  85. response.ok = not response.is_error
  86. # update total_time.
  87. # See get_time_for_thread() and reset_time_for_thread()
  88. if hasattr(THREADLOCAL, 'total_time'):
  89. time_after_request = default_timer()
  90. THREADLOCAL.total_time += time_after_request - time_before_request
  91. # raise an exception
  92. if check_for_httperror:
  93. raise_for_httperror(response)
  94. return response
  95. def get(url, **kwargs):
  96. kwargs.setdefault('allow_redirects', True)
  97. return request('get', url, **kwargs)
  98. def options(url, **kwargs):
  99. kwargs.setdefault('allow_redirects', True)
  100. return request('options', url, **kwargs)
  101. def head(url, **kwargs):
  102. kwargs.setdefault('allow_redirects', False)
  103. return request('head', url, **kwargs)
  104. def post(url, data=None, **kwargs):
  105. return request('post', url, data=data, **kwargs)
  106. def put(url, data=None, **kwargs):
  107. return request('put', url, data=data, **kwargs)
  108. def patch(url, data=None, **kwargs):
  109. return request('patch', url, data=data, **kwargs)
  110. def delete(url, **kwargs):
  111. return request('delete', url, **kwargs)
  112. async def stream_chunk_to_queue(network, queue, method, url, **kwargs):
  113. try:
  114. async with await network.stream(method, url, **kwargs) as response:
  115. queue.put(response)
  116. # aiter_raw: access the raw bytes on the response without applying any HTTP content decoding
  117. # https://www.python-httpx.org/quickstart/#streaming-responses
  118. async for chunk in response.aiter_raw(65536):
  119. if len(chunk) > 0:
  120. queue.put(chunk)
  121. except (httpx.StreamClosed, anyio.ClosedResourceError):
  122. # the response was queued before the exception.
  123. # the exception was raised on aiter_raw.
  124. # we do nothing here: in the finally block, None will be queued
  125. # so stream(method, url, **kwargs) generator can stop
  126. pass
  127. except Exception as e: # pylint: disable=broad-except
  128. # broad except to avoid this scenario:
  129. # exception in network.stream(method, url, **kwargs)
  130. # -> the exception is not catch here
  131. # -> queue None (in finally)
  132. # -> the function below steam(method, url, **kwargs) has nothing to return
  133. queue.put(e)
  134. finally:
  135. queue.put(None)
  136. def _stream_generator(method, url, **kwargs):
  137. queue = SimpleQueue()
  138. network = get_context_network()
  139. future = asyncio.run_coroutine_threadsafe(
  140. stream_chunk_to_queue(network, queue, method, url, **kwargs),
  141. get_loop()
  142. )
  143. # yield chunks
  144. obj_or_exception = queue.get()
  145. while obj_or_exception is not None:
  146. if isinstance(obj_or_exception, Exception):
  147. raise obj_or_exception
  148. yield obj_or_exception
  149. obj_or_exception = queue.get()
  150. future.result()
  151. def _close_response_method(self):
  152. asyncio.run_coroutine_threadsafe(
  153. self.aclose(),
  154. get_loop()
  155. )
  156. # reach the end of _self.generator ( _stream_generator ) to an avoid memory leak.
  157. # it makes sure that :
  158. # * the httpx response is closed (see the stream_chunk_to_queue function)
  159. # * to call future.result() in _stream_generator
  160. for _ in self._generator: # pylint: disable=protected-access
  161. continue
  162. def stream(method, url, **kwargs):
  163. """Replace httpx.stream.
  164. Usage:
  165. response, stream = poolrequests.stream(...)
  166. for chunk in stream:
  167. ...
  168. httpx.Client.stream requires to write the httpx.HTTPTransport version of the
  169. the httpx.AsyncHTTPTransport declared above.
  170. """
  171. generator = _stream_generator(method, url, **kwargs)
  172. # yield response
  173. response = next(generator) # pylint: disable=stop-iteration-return
  174. if isinstance(response, Exception):
  175. raise response
  176. response._generator = generator # pylint: disable=protected-access
  177. response.close = MethodType(_close_response_method, response)
  178. return response, generator