smithy-aws-event-stream
events
¶
Serialization and deserialization utilities for the application/vnd.amazon.eventstream format.
This format is used to frame event stream messages for AWS protocols in a compact manner.
HEADERS_DICT = Mapping[str, HEADER_VALUE]
¶
A dictionary of event headers.
HEADER_VALUE = bool | int | bytes | str | datetime.datetime | uuid.UUID
¶
A union of valid value types for event headers.
SizedInt = Byte | Short | Long | int
¶
A union of integer types that indicate their size.
Each member of the union is a Python int or empty subclass of it, so they can be used anywhere an int would be used in exactly the same way. The alternative type name may be used to determine the size. There are four sizes:
- Byte - 8 bits
- Short - 16 bits
- int - 32 bits
- Long - 64 bits
Serialization will fail if the provided values are not within the expected range.
Sizes are not preserved in this way during deserialization.
Byte
¶
Bases: int
An 8-bit integer.
This is a sentinel class that subclasses int with no additional functionality. It may be used to indicate the size of the int so that the appropriate type is used during manual header serialization to ensure the data is as compact as possible.
This type is unnecessary and unused when serializing a SerializeableShape, which is able to indicate its size via a schema.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
Event
dataclass
¶
A complete event, including the prelude and trailing crc.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
|
crc
instance-attribute
¶
The CRC32 checksum of the event.
This checksum is computed using the prelude CRC bytes followed by the header bytes and then the payload bytes. The initial CRC value is the payload CRC value.
message
instance-attribute
¶
The message portion of the event.
prelude
instance-attribute
¶
Information sent first as part of an event.
decode(source)
classmethod
¶
Decode an event from a byte stream.
:param source: An object to read event bytes from. It must have a read
method
that accepts a number of bytes to read.
:returns: An Event representing the next event on the source, or None if no data can be read from the source.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
decode_async(source)
async
classmethod
¶
Decode an event from an async byte stream.
:param source: An object to read event bytes from. It must have a read
method
that accepts a number of bytes to read.
:returns: An Event representing the next event on the source, or None if no data can be read from the source.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
EventHeaderDecoder
¶
Bases: Iterator[tuple[str, HEADER_VALUE]]
A utility class that decodes headers from bytes.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
__init__(header_bytes)
¶
Initialize an event header decoder.
:param header_bytes: A bytes or memoryview to read headers from.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
decode_header()
¶
Decode a single key-value pair from the source.
:returns: A single key-value pair read from the source.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
decode_headers()
¶
Decode all remaining headers.
:returns: A dict containing all remaining headers read from the source.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
EventHeaderEncoder
¶
A utility class that encodes event headers into bytes.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
|
clear()
¶
encode_blob(key, value)
¶
Encode a binary header.
:param key: The header key to encode. :param value: The binary value to encode.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
encode_boolean(key, value)
¶
Encode a boolean header.
:param key: The header key to encode. :param value: The boolean value to encode.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
encode_byte(key, value)
¶
Encode an 8-bit int header.
:param key: The header key to encode. :param value: The int value to encode.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
encode_headers(headers)
¶
Encode a map of headers.
:param headers: A mapping of headers to encode.
Sized integer values may be indicated using the `Byte`, `Short`, or `Long`
types. int values of unspecified size will be assumed to be 32-bit.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
encode_integer(key, value)
¶
Encode a 32-bit int header.
:param key: The header key to encode. :param value: The int value to encode.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
encode_long(key, value)
¶
Encode a 64-bit int header.
:param key: The header key to encode. :param value: The int value to encode.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
encode_short(key, value)
¶
Encode a 16-bit int header.
:param key: The header key to encode. :param value: The int value to encode.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
encode_string(key, value)
¶
Encode a string header.
:param key: The header key to encode. :param value: The string value to encode.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
encode_timestamp(key, value)
¶
Encode a timestamp header.
:param key: The header key to encode. :param value: The timestamp value to encode.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
encode_uuid(key, value)
¶
Encode a UUID header.
:param key: The header key to encode. :param value: The UUID value to encode.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
get_result()
¶
Get all the encoded header bytes.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
EventMessage
dataclass
¶
A message that may be sent over an event stream.
AWS events indicate their semantic structure with the :message-type
header. This
may have one of three string values: event
, exception
, or error
.
event
messages are modeled data events. In addition to the :message-type
header,
they utilize the following headers:
:event-type
- Required. This further refines the semantic structure of the event, determining what other headers may be set as well as the structure of the payload. The value is a string representing one of the event stream union member names in the Smithy model for the operation.
The value may instead be initial-request
or initial-response
to indicate that
the event represents an even stream initial message. These map to the Smithy
operation input and output, respectively.
* :content-type
- A string indicating the media type of the payload.
exception
messages are modeled error events. In addition to the :message-type
header, they utilize the following headers:
:exception-type
- Required. This further refines the semantic structure of the event, determining what other headers may be set as well as the structure of the payload. The value is a string representing one of the event stream union member names in the Smithy model for the operation.:content-type
- A string indicating the media type of the payload.
error
messages are unmodeled error events. In addition to the :message-type
header, they utilize the following headers:
:error-code
- Required. An alphanumeric string containing the name, type, or category of the error.:error-message
- Required. A human-readable string containing an error message.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
headers = field(default_factory=(dict[str, HEADER_VALUE]))
class-attribute
instance-attribute
¶
The headers present in the event message.
Sized integer values may be indicated for the purpose of serialization
using the Byte
, Short
, or Long
types. int values of unspecified size
will be assumed to be 32-bit.
payload = b''
class-attribute
instance-attribute
¶
The serialized bytes of the message payload.
EventPrelude
dataclass
¶
Information sent first as part of an event.
This includes the sizes of different parts of the event structure, which are used to know exactly how many bytes to expect.
The prelude is always exactly 12 bytes long when serialized.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
crc
instance-attribute
¶
The CRC32 checksum of the prelude.
The bytes for this value are used when calculating the checksum at the end of the event message.
headers_length
instance-attribute
¶
The length of the headers section.
This value may be between 0 and 128 * 1024.
total_length
instance-attribute
¶
The total length of the event message.
This includes:
- The length of the prelude (always 12 bytes)
- The length of the headers (between 0b and 128Kb)
- The length of the payload (between 0b and 16Mb)
- The length of the trailing crc (always 4 bytes)
Long
¶
Bases: int
A 64-bit integer.
This is a sentinel class that subclasses int with no additional functionality. It may be used to indicate the size of the int so that the appropriate type is used during manual header serialization to ensure the data is as compact as possible.
This type is unnecessary and unused when serializing a SerializeableShape, which is able to indicate its size via a schema.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
Short
¶
Bases: int
A 16-bit integer.
This is a sentinel class that subclasses int with no additional functionality. It may be used to indicate the size of the int so that the appropriate type is used during manual header serialization to ensure the data is as compact as possible.
This type is unnecessary and unused when serializing a SerializeableShape, which is able to indicate its size via a schema.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/events.py
exceptions
¶
Binary Event Stream support for the application/vnd.amazon.eventstream format.
ChecksumMismatch
¶
Bases: EventError
Calculated checksum did not match the expected checksum.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/exceptions.py
DuplicateHeader
¶
Bases: EventError
Duplicate header found in the event.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/exceptions.py
EventError
¶
Bases: SmithyError
Base error for all errors thrown during event stream handling.
InvalidHeadersLength
¶
Bases: EventError
Headers length is longer than the maximum.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/exceptions.py
InvalidPayloadLength
¶
Bases: EventError
Payload length is longer than the maximum.
Source code in packages/smithy-aws-event-stream/src/smithy_aws_event_stream/exceptions.py
UnmodeledEventError
¶
Bases: EventError
Unmodeled event error was read from the event stream.
These classes of errors tend to be internal server errors or other unexpected errors on the service side.