77import time
88import uuid as python_uuid
99from logging .config import dictConfig
10- from typing import Annotated , Any
10+ from typing import Any
1111from urllib .parse import ParseResult , urlparse
1212
1313import shortuuid
1414from dateutil .parser import parse
1515from fastapi import (
1616 FastAPI ,
17- Header ,
1817 HTTPException ,
1918 WebSocket ,
2019 WebSocketDisconnect ,
@@ -195,9 +194,9 @@ class EventStreamGetResponse(BaseModel):
195194async def event_stream (
196195 websocket : WebSocket ,
197196 uuid : str ,
198- x_streamfromdatetime : Annotated [ str | None , Header ()] = None ,
199- x_streamfromordinal : Annotated [ str | None , Header ()] = None ,
200- x_streamfromtimestamp : Annotated [ str | None , Header ()] = None ,
197+ stream_from_datetime : str | None = None ,
198+ stream_from_ordinal : int | None = None ,
199+ stream_from_timestamp : int | None = None ,
201200):
202201 """The websocket handler for the event-stream.
203202 The UUID is returned to the AS when the web-socket is created
@@ -213,14 +212,13 @@ async def event_stream(
213212 await websocket .accept ()
214213 _LOGGER .info ("Accepted connection (uuid=%s)" , uuid )
215214
216- # Custom request headers.
217- # The following are used to identify the first event in a stream: -
215+ # The following parameters are used to identify the first event in a stream: -
218216 #
219- # X-StreamFromDatetime - an ISO8601 date/time string
220- # X-StreamFromTimestamp - an event timestamp (integer) from a prior message
221- # X-StreamFromOrdinal - a message ordinal (integer 0..N)
217+ # stream_from_datetime - an ISO8601 date/time string
218+ # stream_from_ordinal - a message ordinal (integer 0..N)
219+ # stream_from_timestamp - an event timestamp (integer) from a prior message
222220 #
223- # Only one of the above is expected .
221+ # Only one of the above is permitted .
224222 num_stream_from_specified : int = 0
225223 header_value_error : bool = False
226224 header_value_error_msg : str = ""
@@ -229,11 +227,11 @@ async def event_stream(
229227 OffsetType .NEXT
230228 )
231229 # Was a streaming offset provided?
232- if x_streamfromdatetime :
230+ if stream_from_datetime :
233231 num_stream_from_specified += 1
234232 try :
235- _LOGGER .info ("Found X-StreamFromDatetime =%s" , x_streamfromdatetime )
236- from_datetime = parse (x_streamfromdatetime )
233+ _LOGGER .info ("Given stream_from_datetime =%s" , stream_from_datetime )
234+ from_datetime = parse (stream_from_datetime )
237235 # We need a RabbitMQ stream timestamp,
238236 # which is milliseconds since the universal time epoch (1 Jan, 1970).
239237 # It's easy to get 'seconds', which we multiply by 1,000
@@ -243,34 +241,34 @@ async def event_stream(
243241 )
244242 except : # pylint: disable=bare-except
245243 header_value_error = True
246- header_value_error_msg = "Unable to parse X-StreamFromDatetime value"
247- if x_streamfromordinal :
248- _LOGGER .info ("Found X-StreamFromOrdinal=%s " , x_streamfromordinal )
244+ header_value_error_msg = "Unable to parse stream_from_datetime value"
245+ if stream_from_ordinal :
246+ _LOGGER .info ("Given stream_from_ordinal=%d " , stream_from_ordinal )
249247 num_stream_from_specified += 1
250248 try :
251- from_ordinal = int (x_streamfromordinal )
249+ from_ordinal = int (stream_from_ordinal )
252250 offset_specification = ConsumerOffsetSpecification (
253251 OffsetType .OFFSET , from_ordinal
254252 )
255253 except ValueError :
256254 header_value_error = True
257- header_value_error_msg = "X-StreamFromOrdinal must be an integer"
258- if x_streamfromtimestamp :
259- _LOGGER .info ("Found X-StreamFromTimestamp =%s" , x_streamfromtimestamp )
255+ header_value_error_msg = "stream_from_ordinal must be an integer"
256+ if stream_from_timestamp :
257+ _LOGGER .info ("Found stream_from_timestamp =%s" , stream_from_timestamp )
260258 num_stream_from_specified += 1
261259 try :
262- from_timestamp = int (x_streamfromtimestamp )
260+ from_timestamp = int (stream_from_timestamp )
263261 offset_specification = ConsumerOffsetSpecification (
264262 OffsetType .TIMESTAMP , from_timestamp
265263 )
266264 except ValueError :
267265 header_value_error = True
268- header_value_error_msg = "X-StreamFromTimestamp must be an integer"
266+ header_value_error_msg = "stream_from_timestamp must be an integer"
269267
270268 # Replace any error with a 'too many values provided' error if necessary
271269 if num_stream_from_specified > 1 :
272270 header_value_error = True
273- header_value_error_msg = "Cannot provide more than one X-StreamFrom variable"
271+ header_value_error_msg = "Cannot provide more than one 'stream_from_' variable"
274272
275273 if header_value_error :
276274 await websocket .close (
0 commit comments