1717from couchbase_analytics .common ._core .error_context import ErrorContext
1818from couchbase_analytics .common .backoff_calculator import DefaultBackoffCalculator
1919from couchbase_analytics .common .errors import AnalyticsError
20- from couchbase_analytics .common .streaming import StreamingState
20+ from couchbase_analytics .common .request import RequestState
2121from couchbase_analytics .protocol .connection import DEFAULT_TIMEOUTS
2222from couchbase_analytics .protocol .errors import ErrorMapper
2323
@@ -43,7 +43,7 @@ def __init__(
4343 self ._backend = backend or current_async_library ()
4444 self ._backoff_calc = DefaultBackoffCalculator ()
4545 self ._error_ctx = ErrorContext (num_attempts = 0 , method = request .method , statement = request .get_request_statement ())
46- self ._request_state = StreamingState .NotStarted
46+ self ._request_state = RequestState .NotStarted
4747 self ._stream_config = stream_config or JsonStreamConfig ()
4848 self ._json_stream : AsyncJsonStream
4949 self ._stage_completed : Optional [anyio .Event ] = None
@@ -56,7 +56,7 @@ def __init__(
5656 @property
5757 def cancelled (self ) -> bool :
5858 self ._check_cancelled_or_timed_out ()
59- return self ._request_state in [StreamingState .Cancelled , StreamingState .AsyncCancelledPriorToTimeout ]
59+ return self ._request_state in [RequestState .Cancelled , RequestState .AsyncCancelledPriorToTimeout ]
6060
6161 @property
6262 def error_context (self ) -> ErrorContext :
@@ -73,19 +73,19 @@ def is_shutdown(self) -> bool:
7373 @property
7474 def okay_to_iterate (self ) -> bool :
7575 self ._check_cancelled_or_timed_out ()
76- return StreamingState .okay_to_iterate (self ._request_state )
76+ return RequestState .okay_to_iterate (self ._request_state )
7777
7878 @property
7979 def okay_to_stream (self ) -> bool :
8080 self ._check_cancelled_or_timed_out ()
81- return StreamingState .okay_to_stream (self ._request_state )
81+ return RequestState .okay_to_stream (self ._request_state )
8282
8383 @property
8484 def request_error (self ) -> Optional [Union [BaseException , Exception ]]:
8585 return self ._request_error
8686
8787 @property
88- def request_state (self ) -> StreamingState :
88+ def request_state (self ) -> RequestState :
8989 return self ._request_state
9090
9191 @property
@@ -99,10 +99,10 @@ def results_or_errors_type(self) -> ParsedResultType:
9999 @property
100100 def timed_out (self ) -> bool :
101101 self ._check_cancelled_or_timed_out ()
102- return self ._request_state == StreamingState .Timeout
102+ return self ._request_state == RequestState .Timeout
103103
104104 def _check_cancelled_or_timed_out (self ) -> None :
105- if self ._request_state in [StreamingState .Timeout , StreamingState .Cancelled , StreamingState .Error ]:
105+ if self ._request_state in [RequestState .Timeout , RequestState .Cancelled , RequestState .Error ]:
106106 return
107107
108108 if hasattr (self , '_request_deadline' ) is False :
@@ -115,10 +115,10 @@ def _check_cancelled_or_timed_out(self) -> None:
115115 timed_out = current_time >= self ._request_deadline
116116
117117 if timed_out :
118- if self ._request_state == StreamingState .Cancelled :
119- self ._request_state = StreamingState .AsyncCancelledPriorToTimeout
118+ if self ._request_state == RequestState .Cancelled :
119+ self ._request_state = RequestState .AsyncCancelledPriorToTimeout
120120 else :
121- self ._request_state = StreamingState .Timeout
121+ self ._request_state = RequestState .Timeout
122122
123123 async def _execute (self , fn : Callable [..., Awaitable [Any ]], * args : object ) -> None :
124124 await fn (* args )
@@ -131,22 +131,22 @@ def _maybe_set_request_error(
131131 self ._check_cancelled_or_timed_out ()
132132 if exc_val is None :
133133 return
134- if not StreamingState .is_timeout_or_cancelled (self ._request_state ):
134+ if not RequestState .is_timeout_or_cancelled (self ._request_state ):
135135 # This handles httpx timeouts
136136 if exc_type is not None and issubclass (exc_type , TimeoutException ):
137- self ._request_state = StreamingState .Timeout
137+ self ._request_state = RequestState .Timeout
138138 elif issubclass (type (exc_val ), TimeoutException ):
139- self ._request_state = StreamingState .Timeout
139+ self ._request_state = RequestState .Timeout
140140 elif isinstance (exc_val , CancelledError ):
141- self ._request_state = StreamingState .Cancelled
141+ self ._request_state = RequestState .Cancelled
142142 else :
143- self ._request_state = StreamingState .Error
143+ self ._request_state = RequestState .Error
144144 self ._request_error = exc_val
145145
146146 async def _process_error (
147147 self , json_data : Union [str , List [Dict [str , Any ]]], handle_context_shutdown : Optional [bool ] = False
148148 ) -> None :
149- self ._request_state = StreamingState .Error
149+ self ._request_state = RequestState .Error
150150 if isinstance (json_data , str ):
151151 self ._request_error = ErrorMapper .build_error_from_http_status_code (json_data , self ._error_ctx )
152152 elif not isinstance (json_data , list ):
@@ -163,7 +163,7 @@ async def _process_error(
163163 def _reset_stream (self ) -> None :
164164 if hasattr (self , '_json_stream' ):
165165 del self ._json_stream
166- self ._request_state = StreamingState .ResetAndNotStarted
166+ self ._request_state = RequestState .ResetAndNotStarted
167167 self ._stage_completed = None
168168 self ._cancel_scope_deadline_updated = False
169169
@@ -211,10 +211,10 @@ def calculate_backoff(self) -> float:
211211 def cancel_request (self , fn : Optional [Callable [..., Awaitable [Any ]]] = None , * args : object ) -> None :
212212 if fn is not None :
213213 self ._taskgroup .start_soon (fn , * args )
214- if self ._request_state == StreamingState .Timeout :
214+ if self ._request_state == RequestState .Timeout :
215215 return
216216 self ._taskgroup .cancel_scope .cancel ()
217- self ._request_state = StreamingState .Cancelled
217+ self ._request_state = RequestState .Cancelled
218218
219219 def create_response_task (self , fn : Callable [..., Coroutine [Any , Any , Any ]], * args : object ) -> Task [Any ]:
220220 if self ._backend is None or self ._backend .backend_lib != 'asyncio' :
@@ -246,12 +246,12 @@ async def get_result_from_stream(self) -> ParsedResult:
246246
247247 async def initialize (self ) -> None :
248248 # TODO: Add useful logging messages
249- if self ._request_state == StreamingState .ResetAndNotStarted :
249+ if self ._request_state == RequestState .ResetAndNotStarted :
250250 self ._update_cancel_scope_deadline (self ._connect_deadline , is_absolute = True )
251251 # print('Skipping initialization as request is a retry')
252252 return
253253 await self .__aenter__ ()
254- self ._request_state = StreamingState .Started
254+ self ._request_state = RequestState .Started
255255 # we set the request timeout once the context is initialized in order to create the deadline
256256 # closer to when the upstream logic will begin to use the request context
257257 timeouts = self ._request .get_request_timeouts () or {}
@@ -272,18 +272,18 @@ def maybe_continue_to_process_stream(self) -> None:
272272 def okay_to_delay_and_retry (self , delay : float ) -> bool :
273273 # TODO: Add useful logging messages
274274 self ._check_cancelled_or_timed_out ()
275- if self ._request_state in [StreamingState .Timeout , StreamingState .Cancelled ]:
275+ if self ._request_state in [RequestState .Timeout , RequestState .Cancelled ]:
276276 return False
277277
278278 current_time = get_time ()
279279 delay_time = current_time + delay
280280 will_time_out = self ._request_deadline < delay_time
281281 # print(f'{current_time=}; {delay_time=}; req_deadline={self._request_deadline}; {will_time_out=}')
282282 if will_time_out :
283- self ._request_state = StreamingState .Timeout
283+ self ._request_state = RequestState .Timeout
284284 return False
285285 elif self .retry_limit_exceeded :
286- self ._request_state = StreamingState .Error
286+ self ._request_state = RequestState .Error
287287 return False
288288 else :
289289 self ._reset_stream ()
@@ -358,8 +358,8 @@ async def shutdown(
358358 else :
359359 self ._maybe_set_request_error (exc_type , exc_val )
360360
361- if StreamingState .is_okay (self ._request_state ):
362- self ._request_state = StreamingState .Completed
361+ if RequestState .is_okay (self ._request_state ):
362+ self ._request_state = RequestState .Completed
363363 self ._shutdown = True
364364
365365 def start_stream (self , core_response : HttpCoreResponse ) -> None :
@@ -374,7 +374,7 @@ async def wait_for_results_or_errors(self) -> None:
374374 await self ._json_stream .has_results_or_errors .wait ()
375375 if self ._json_stream .results_or_errors_type == ParsedResultType .ROW :
376376 # we move to iterating rows
377- self ._request_state = StreamingState .StreamingResults
377+ self ._request_state = RequestState .StreamingResults
378378
379379 async def __aenter__ (self ) -> AsyncRequestContext :
380380 self ._taskgroup = anyio .create_task_group ()
0 commit comments