11import json
2- from typing import Optional
2+ from typing import Any , Dict , Optional
3+
4+ from ld_eventsource .errors import ExceptionWithHeaders
35
46
57class Action :
@@ -110,9 +112,25 @@ class Start(Action):
110112 Instances of this class are only available from :attr:`.SSEClient.all`.
111113 A ``Start`` is returned for the first successful connection. If the client reconnects
112114 after a failure, there will be a :class:`.Fault` followed by a ``Start``.
115+
116+ Each ``Start`` action may include HTTP response headers from the connection. These headers
117+ are available via the :attr:`headers` property. On reconnection, a new ``Start`` will be
118+ emitted with the headers from the new connection, which may differ from the previous one.
113119 """
114120
115- pass
121+ def __init__ (self , headers : Optional [Dict [str , Any ]] = None ):
122+ self ._headers = headers
123+
124+ @property
125+ def headers (self ) -> Optional [Dict [str , Any ]]:
126+ """
127+ The HTTP response headers from the stream connection, if available.
128+
129+ The headers dict uses case-insensitive keys (via urllib3's HTTPHeaderDict).
130+
131+ :return: the response headers, or ``None`` if not available
132+ """
133+ return self ._headers
116134
117135
118136class Fault (Action ):
@@ -125,6 +143,9 @@ class Fault(Action):
125143 connection attempt has failed or an existing connection has been closed. The SSEClient
126144 will attempt to reconnect if you either call :meth:`.SSEClient.start()`
127145 or simply continue reading events after this point.
146+
147+ When the error includes HTTP response headers (such as for :class:`.HTTPStatusError`
148+ or :class:`.HTTPContentTypeError`), they are accessible via the :attr:`headers` property.
128149 """
129150
130151 def __init__ (self , error : Optional [Exception ]):
@@ -138,3 +159,18 @@ def error(self) -> Optional[Exception]:
138159 in an orderly way after sending an EOF chunk as defined by chunked transfer encoding.
139160 """
140161 return self .__error
162+
163+ @property
164+ def headers (self ) -> Optional [Dict [str , Any ]]:
165+ """
166+ The HTTP response headers from the failed connection, if available.
167+
168+ This property returns headers when the error is an exception that includes them,
169+ such as :class:`.HTTPStatusError` or :class:`.HTTPContentTypeError`. For other
170+ error types or when the stream ended normally, this returns ``None``.
171+
172+ :return: the response headers, or ``None`` if not available
173+ """
174+ if isinstance (self .__error , ExceptionWithHeaders ):
175+ return self .__error .headers
176+ return None
0 commit comments