API reference#
Brokers#
faststream_sqs_sns.SqsBroker
#
SqsBroker(
*,
region: str | None = None,
endpoint_url: str | None = None,
account_id: str | None = None,
connect_timeout: float = 5.0,
read_timeout: float = 30.0,
max_connections: int | None = None,
graceful_timeout: float | None = 25.0,
decoder: Optional[CustomCallable] = None,
parser: Optional[CustomCallable] = None,
codec: Optional[CodecProto] = None,
dependencies: Sequence[Dependant] = (),
middlewares: Sequence[BrokerMiddleware[Any, Any]] = (),
routers: Iterable[SqsRegistrator] = (),
ack_policy: AckPolicy = EMPTY,
id_generator: IdGenerator = gen_cor_id,
specification_url: str | None = None,
protocol: str | None = None,
protocol_version: str | None = None,
description: str | None = None,
tags: Iterable[Tag | TagDict] = (),
security: Optional[BaseSecurity] = None,
logger: Optional[LoggerProto] = EMPTY,
log_level: int = INFO,
apply_types: bool = True,
serializer: Optional[SerializerProto] = EMPTY,
provider: Optional[Provider] = None,
context: Optional[ContextRepo] = None,
)
Bases: SqsRegistrator, BrokerUsecase[Any, Transport, SqsBrokerConfig]
The Amazon SQS broker with SNS publishing, whatever the backend.
Construct CapoSqsBroker or AiobotocoreSqsBroker; this class holds what they share
and is exported for annotations and isinstance checks. The constructor only records
the configuration; connect() (or start()) builds the clients.
| PARAMETER | DESCRIPTION |
|---|---|
region
|
the AWS region; the backend reads
TYPE:
|
endpoint_url
|
one endpoint for SQS and SNS, such as LocalStack or a VPC endpoint.
TYPE:
|
account_id
|
the account a bare topic name belongs to; it builds the topic ARN without listing topics.
TYPE:
|
connect_timeout
|
seconds to open a connection.
TYPE:
|
read_timeout
|
seconds to wait for a response; it must exceed every subscriber's
TYPE:
|
max_connections
|
connections per host of the SQS client; one per poll loop plus 10 when omitted.
TYPE:
|
graceful_timeout
|
seconds
TYPE:
|
security
|
an
TYPE:
|
Source code in faststream_sqs_sns/broker/core.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 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 | |
declare_queue
async
#
declare_queue(queue: str | SqsQueue) -> str
Create a queue (and its dead-letter queue) unless it exists.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The queue URL. |
Source code in faststream_sqs_sns/broker/core.py
279 280 281 282 283 284 285 286 | |
declare_topic
async
#
declare_topic(topic: str | SnsTopic) -> str
Create a topic unless it exists.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The topic ARN. |
Source code in faststream_sqs_sns/broker/core.py
288 289 290 291 292 293 294 295 | |
publish
async
#
publish(
message: SendableMessage = None,
destination: str | SqsQueue | SnsTopic = "",
*,
headers: dict[str, Any] | None = None,
correlation_id: str | None = None,
reply_to: str = "",
message_group_id: str | None = None,
deduplication_id: str | None = None,
delay: int = 0,
timeout: float | None = None,
) -> str
Send a message to a queue or publish it to a topic.
| PARAMETER | DESCRIPTION |
|---|---|
message
|
the message body.
TYPE:
|
destination
|
a queue (name, URL, ARN or |
headers
|
message attributes:
TYPE:
|
correlation_id
|
the correlation id; generated when omitted.
TYPE:
|
reply_to
|
the queue (name or URL) or topic ARN a consumer replies to.
TYPE:
|
message_group_id
|
the FIFO message group; required by a FIFO queue or topic.
TYPE:
|
deduplication_id
|
the FIFO deduplication id; required unless the queue or topic deduplicates by content.
TYPE:
|
delay
|
seconds before the message becomes visible, 0 to 900; standard queues only.
TYPE:
|
timeout
|
seconds the call may take.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The message id the service assigned. |
Source code in faststream_sqs_sns/broker/core.py
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 335 336 337 338 339 340 341 342 343 | |
publish_batch
async
#
publish_batch(
*messages: SendableMessage,
destination: str | SqsQueue | SnsTopic,
headers: dict[str, Any] | None = None,
correlation_id: str | None = None,
reply_to: str = "",
message_group_id: str | None = None,
deduplication_id: str | None = None,
delay: int = 0,
timeout: float | None = None,
) -> list[str]
Publish several messages, 10 per call, the calls concurrently.
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
The message ids, in the order of the messages. |
| RAISES | DESCRIPTION |
|---|---|
BatchPublishError
|
some messages were not sent; it lists which, after every call is done. |
Source code in faststream_sqs_sns/broker/core.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
request
async
#
request(
message: SendableMessage = None,
destination: str | SqsQueue | SnsTopic = "",
*,
correlation_id: str | None = None,
timeout: float | None = None,
) -> Any
Not available on SQS; raises FeatureNotSupportedException.
A request needs a reply queue per caller. Publish with reply_to naming a queue
your service consumes, and consume the reply there.
Source code in faststream_sqs_sns/broker/core.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
ping
async
#
ping(timeout: float | None = None) -> bool
Source code in faststream_sqs_sns/broker/core.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
faststream_sqs_sns.CapoSqsBroker
#
CapoSqsBroker(
*,
credentials_provider: Any = None,
retry_mode: str | None = None,
retry_max_attempts: int | None = None,
**kwargs: Unpack[SqsBrokerOptions],
)
Bases: SqsBroker
The SQS broker over capo-sqs and capo-sns (extra capo).
It takes every argument of SqsBroker plus capo's own.
| PARAMETER | DESCRIPTION |
|---|---|
credentials_provider
|
a capo
TYPE:
|
retry_mode
|
capo's retry mode for both clients.
TYPE:
|
retry_max_attempts
|
capo's attempts per call for both clients.
TYPE:
|
Source code in faststream_sqs_sns/broker/capo.py
29 30 31 32 33 34 35 36 37 38 39 40 | |
faststream_sqs_sns.AiobotocoreSqsBroker
#
AiobotocoreSqsBroker(
*,
session: AioSession | None = None,
config: Config | None = None,
**kwargs: Unpack[SqsBrokerOptions],
)
Bases: SqsBroker
The SQS broker over aiobotocore (extra aiobotocore).
It takes every argument of SqsBroker plus aiobotocore's own.
| PARAMETER | DESCRIPTION |
|---|---|
session
|
an
TYPE:
|
config
|
a
TYPE:
|
Source code in faststream_sqs_sns/broker/aiobotocore.py
31 32 33 34 35 36 37 38 39 40 | |
Resources#
faststream_sqs_sns.SqsQueue
dataclass
#
SqsQueue(
name: str,
*,
declare: bool = False,
fifo: bool | None = None,
visibility_timeout: int | None = None,
wait_time: int = MAX_WAIT_TIME,
max_messages: int = MAX_BATCH_ENTRIES,
redrive: RedrivePolicy | None = None,
attributes: Mapping[str, str] = dict(),
)
An SQS queue.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
a queue name, a queue URL (
TYPE:
|
declare |
create the queue at start when it does not exist.
TYPE:
|
fifo |
whether the queue is FIFO;
TYPE:
|
visibility_timeout |
seconds, 0 to 43200;
TYPE:
|
wait_time |
long-poll seconds of one receive, 0 to 20.
TYPE:
|
max_messages |
messages of one receive, 1 to 10.
TYPE:
|
redrive |
the dead-letter policy applied on declaration.
TYPE:
|
attributes |
queue attributes in the service's spelling, applied on declaration; the typed fields win over the same key.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
SetupError
|
a value is out of range, |
attributes
class-attribute
instance-attribute
#
attributes: Mapping[str, str] = field(
default_factory=dict, hash=False
)
queue_name
property
#
queue_name: str
The bare queue name: the last path segment of a URL, the name part of an ARN.
account_id
property
#
account_id: str | None
The owner account an ARN or a URL path (/<account>/<name>) names.
content_based_deduplication
property
#
content_based_deduplication: bool
Whether the declaration enables content-based deduplication.
with_prefix
#
with_prefix(prefix: str) -> SqsQueue
The queue with a router prefix; a URL or an ARN is returned as is.
Source code in faststream_sqs_sns/schemas.py
216 217 218 219 220 | |
declare_attributes
#
declare_attributes(
dead_letter_arn: str | None = None,
) -> dict[str, str]
The attributes CreateQueue takes: the user's, then the typed fields.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
|
Source code in faststream_sqs_sns/schemas.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
faststream_sqs_sns.SnsTopic
dataclass
#
SnsTopic(
name: str,
*,
declare: bool = False,
fifo: bool | None = None,
attributes: Mapping[str, str] = dict(),
)
An SNS topic.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
a topic name or a topic ARN.
TYPE:
|
declare |
create the topic at start when it does not exist.
TYPE:
|
fifo |
whether the topic is FIFO;
TYPE:
|
attributes |
topic attributes in the service's spelling, applied on declaration.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
SetupError
|
|
attributes
class-attribute
instance-attribute
#
attributes: Mapping[str, str] = field(
default_factory=dict, hash=False
)
content_based_deduplication
property
#
content_based_deduplication: bool
Whether the declaration enables content-based deduplication.
with_prefix
#
with_prefix(prefix: str) -> SnsTopic
The topic with a router prefix; an ARN is returned as is.
Source code in faststream_sqs_sns/schemas.py
293 294 295 296 297 | |
declare_attributes
#
declare_attributes() -> dict[str, str]
The attributes CreateTopic takes: the user's, then FifoTopic.
Source code in faststream_sqs_sns/schemas.py
299 300 301 302 303 304 | |
faststream_sqs_sns.RedrivePolicy
dataclass
#
RedrivePolicy(
dead_letter_queue: str | SqsQueue,
max_receive_count: int,
)
Where a queue sends a message that was received too many times.
| ATTRIBUTE | DESCRIPTION |
|---|---|
dead_letter_queue |
the dead-letter queue, a name, URL, ARN or a queue schema.
TYPE:
|
max_receive_count |
receives after which the message moves, 1 to 1000.
TYPE:
|
Consuming#
faststream_sqs_sns.FlowControl
dataclass
#
FlowControl(
*,
polls: int = 1,
max_in_flight: int | None = None,
settle_wait: float = 0.05,
max_hold: float = 3600.0,
)
How a subscriber polls, holds and settles messages.
| ATTRIBUTE | DESCRIPTION |
|---|---|
polls |
concurrent ReceiveMessage loops.
TYPE:
|
max_in_flight |
messages received and not settled, at most;
TYPE:
|
settle_wait |
seconds deletes and visibility changes wait to be sent together.
TYPE:
|
max_hold |
seconds a message is held at most; past it its visibility is no longer extended and SQS redelivers it. Capped at 43140 seconds, the 12 hours SQS allows a receive, less a minute.
TYPE:
|
faststream_sqs_sns.message.SqsBatchMessage
#
Bases: StreamMessage[SqsBatchRawMessage]
The messages of one receive in a batch handler; settling settles every one.
message_group_ids
property
#
message_group_ids: list[str | None]
The FIFO message group of each message.
ack
async
#
ack() -> None
Source code in faststream_sqs_sns/message.py
157 158 159 160 161 162 | |
nack
async
#
nack() -> None
Source code in faststream_sqs_sns/message.py
164 165 166 167 168 169 | |
reject
async
#
reject() -> None
Source code in faststream_sqs_sns/message.py
171 172 173 174 175 176 | |
Publishing#
faststream_sqs_sns.SqsResponse
#
SqsResponse(
body: SendableMessage,
*,
headers: dict[str, Any] | None = None,
correlation_id: str | None = None,
message_group_id: str | None = None,
deduplication_id: str | None = None,
delay: int = 0,
)
Bases: Response
A handler result with SQS publish options.
Return it from a handler to set FIFO fields, a delay or attributes on the reply and on every publisher the handler is decorated with.
Source code in faststream_sqs_sns/response.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
as_publish_command
#
as_publish_command() -> SqsPublishCommand
Source code in faststream_sqs_sns/response.py
55 56 57 58 59 60 61 62 63 64 65 | |
Routers#
faststream_sqs_sns.SqsRouter
#
SqsRouter(
prefix: str = "",
handlers: Iterable[SqsRoute] = (),
*,
dependencies: Sequence[Dependant] = (),
middlewares: Sequence[BrokerMiddleware[Any, Any]] = (),
routers: Iterable[SqsRegistrator] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
include_in_schema: bool | None = None,
ack_policy: AckPolicy = EMPTY,
)
Bases: SqsRegistrator, BrokerRouter[Any, SqsBrokerConfig]
A group of subscribers and publishers to include into an SQS broker.
prefix is prepended to the queue and topic names it declares; URLs and ARNs are kept
as they are.
Source code in faststream_sqs_sns/broker/router.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
faststream_sqs_sns.SqsRoute
#
SqsRoute(
call: Callable[..., SendableMessage]
| Callable[..., Awaitable[SendableMessage]],
queue: str | SqsQueue,
*,
topic: str | SnsTopic | None = None,
batch: bool = False,
publishers: Iterable[SqsPublisher] = (),
flow_control: Optional[FlowControl] = None,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
dependencies: Sequence[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
codec: Optional[CodecProto] = None,
max_workers: int = 1,
persistent: bool = True,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
)
Bases: SubscriberRoute
Delayed subscriber registration: the arguments of SqsRegistrator.subscriber.
Source code in faststream_sqs_sns/broker/router.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
faststream_sqs_sns.SqsPublisher
#
SqsPublisher(
destination: str | SqsQueue | SnsTopic,
*,
message_group_id: str | None = None,
headers: dict[str, Any] | None = None,
persistent: bool = True,
title: str | None = None,
description: str | None = None,
schema: Any | None = None,
include_in_schema: bool = True,
)
Bases: ArgsContainer
Delayed publisher registration: the arguments of SqsRegistrator.publisher.
Source code in faststream_sqs_sns/broker/router.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
Security#
faststream_sqs_sns.AwsSecurity
#
AwsSecurity(
access_key_id: str,
secret_access_key: str,
session_token: str | None = None,
)
Bases: BaseSecurity
A static AWS access key pair the broker signs its requests with.
Without a security object the backend resolves credentials on its own (environment, shared config, instance or container role).
Examples:
>>> def from_keys() -> CapoSqsBroker:
... security = AwsSecurity("AKIA...", "secret", session_token="token")
... return CapoSqsBroker(region="eu-west-1", security=security)
Source code in faststream_sqs_sns/security.py
25 26 27 28 29 30 31 32 33 34 | |
get_requirement
#
get_requirement() -> list[dict[str, Any]]
Source code in faststream_sqs_sns/security.py
36 37 38 | |
get_schema
#
get_schema() -> dict[str, dict[str, str]]
Source code in faststream_sqs_sns/security.py
40 41 42 43 44 45 46 47 | |
Exceptions#
faststream_sqs_sns.exceptions
#
The errors the broker raises, whatever the backend.
Each backend's exceptions are mapped by class into this set, so user code catches the same
classes with capo and with aiobotocore. The backend's exception is kept as __cause__.
SqsError
#
SqsError(
message: str = "", *, retryable: bool | None = None
)
Bases: Exception
The base of every SQS and SNS error the broker raises.
| ATTRIBUTE | DESCRIPTION |
|---|---|
retryable |
whether the same call may succeed when repeated later.
TYPE:
|
Source code in faststream_sqs_sns/exceptions.py
32 33 34 35 | |
QueueNotFoundError
#
QueueNotFoundError(
message: str = "", *, retryable: bool | None = None
)
Bases: SqsError
The queue does not exist, or the caller may not see it.
Source code in faststream_sqs_sns/exceptions.py
32 33 34 35 | |
TopicNotFoundError
#
TopicNotFoundError(
message: str = "", *, retryable: bool | None = None
)
Bases: SqsError
The topic does not exist, or the caller may not see it.
Source code in faststream_sqs_sns/exceptions.py
32 33 34 35 | |
ThrottledError
#
ThrottledError(
message: str = "", *, retryable: bool | None = None
)
Bases: SqsError
The service throttled the call (request rate or KMS).
Source code in faststream_sqs_sns/exceptions.py
32 33 34 35 | |
InvalidReceiptError
#
InvalidReceiptError(
message: str = "", *, retryable: bool | None = None
)
Bases: SqsError
A receipt handle is invalid or its message is no longer in flight.
Source code in faststream_sqs_sns/exceptions.py
32 33 34 35 | |
ServiceUnavailableError
#
ServiceUnavailableError(
message: str = "", *, retryable: bool | None = None
)
Bases: SqsError
The service could not be reached: a connection failure or a timeout.
Source code in faststream_sqs_sns/exceptions.py
32 33 34 35 | |
SqsServiceError
#
SqsServiceError(
message: str = "", *, code: str, retryable: bool = False
)
Bases: SqsError
Any other error the service answered with.
| ATTRIBUTE | DESCRIPTION |
|---|---|
code |
the service's error code, such as
|
Source code in faststream_sqs_sns/exceptions.py
69 70 71 | |
BatchFailure
dataclass
#
BatchFailure(id: str, code: str, message: str)
BatchPublishError
#
BatchPublishError(
message_ids: Sequence[str | None],
failures: Sequence[BatchFailure],
)
Bases: SqsError
Some messages of a publish_batch were not sent.
| ATTRIBUTE | DESCRIPTION |
|---|---|
message_ids |
the id of each sent message and
|
failures |
the entries the service refused.
|
Source code in faststream_sqs_sns/exceptions.py
98 99 100 101 102 103 104 105 106 107 108 | |
Testing#
faststream_sqs_sns.testing.TestSqsBroker
#
TestSqsBroker(
broker: SqsBroker,
/,
*,
with_real: bool = False,
connect_only: bool | None = None,
)
TestSqsBroker(
*brokers: SqsBroker,
with_real: bool = False,
connect_only: bool | None = None,
)
TestSqsBroker(
*brokers: SqsBroker,
with_real: bool = False,
connect_only: bool | None = None,
)
Bases: TestBroker[SqsBroker, Any]
The broker in process: a publish reaches the subscribers of its queue or topic directly.
It routes the way SQS and SNS do. A queue publish reaches one subscriber of that queue,
in turn; a topic publish reaches every queue subscribed to the topic (topic= on a
subscriber); a queue read by get_one or iteration keeps what is published until it
is read. A publish AWS refuses (sizes, attribute names and count, FIFO fields, delays)
raises the same SqsServiceError with AWS's error code. Settlement is recorded on the
message; nothing is redelivered.
Examples:
>>> async with TestSqsBroker(broker) as br:
... await br.publish({"id": 1}, "orders")
...
... handle.mock.assert_called_once_with({"id": 1})
Source code in faststream_sqs_sns/testing.py
90 91 92 93 94 95 96 97 | |
create_publisher_fake_subscriber
#
create_publisher_fake_subscriber(
broker: SqsBroker, publisher: Any
) -> tuple[SqsBaseSubscriber, bool]
Source code in faststream_sqs_sns/testing.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
Integrations#
faststream_sqs_sns.fastapi.CapoSqsRouter
#
CapoSqsRouter(
*,
credentials_provider: Any = None,
retry_mode: str | None = None,
retry_max_attempts: int | None = None,
**kwargs: Unpack[SqsRouterOptions],
)
Bases: SqsRouterBase
FastAPI router over CapoSqsBroker (extra capo).
Takes the arguments of CapoSqsBroker and of fastapi.APIRouter: the broker's
dependencies are the FastAPI dependencies here and its tags are
specification_tags.
| PARAMETER | DESCRIPTION |
|---|---|
credentials_provider
|
a capo
TYPE:
|
retry_mode
|
capo's retry mode for both clients.
TYPE:
|
retry_max_attempts
|
capo's attempts per call for both clients.
TYPE:
|
Source code in faststream_sqs_sns/fastapi/capo.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
faststream_sqs_sns.fastapi.AiobotocoreSqsRouter
#
AiobotocoreSqsRouter(
*,
session: AioSession | None = None,
config: Config | None = None,
**kwargs: Unpack[SqsRouterOptions],
)
Bases: SqsRouterBase
FastAPI router over AiobotocoreSqsBroker (extra aiobotocore).
Takes the arguments of AiobotocoreSqsBroker and of fastapi.APIRouter: the
broker's dependencies are the FastAPI dependencies here and its tags are
specification_tags.
| PARAMETER | DESCRIPTION |
|---|---|
session
|
an
TYPE:
|
config
|
a
TYPE:
|
Source code in faststream_sqs_sns/fastapi/aiobotocore.py
32 33 34 35 36 37 38 39 40 41 42 | |
faststream_sqs_sns.opentelemetry.SqsTelemetryMiddleware
#
SqsTelemetryMiddleware(
*,
tracer_provider: TracerProvider | None = None,
meter_provider: MeterProvider | None = None,
meter: Meter | None = None,
include_messages_counters: bool = True,
)
Bases: TelemetryMiddleware[SqsPublishCommand]
Traces and measures SQS and SNS publishes and SQS deliveries with OpenTelemetry.
Source code in faststream_sqs_sns/opentelemetry/middleware.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
faststream_sqs_sns.prometheus.SqsPrometheusMiddleware
#
SqsPrometheusMiddleware(
*,
registry: CollectorRegistry,
app_name: str = EMPTY,
metrics_prefix: str = "faststream",
received_messages_size_buckets: Sequence[float]
| None = None,
custom_labels: dict[str, str | Callable[[Any], str]]
| None = None,
)
Bases: PrometheusMiddleware[SqsRawMessage | SqsBatchRawMessage, SqsPublishCommand]
Exports Prometheus metrics of SQS and SNS publishes and SQS deliveries.
Source code in faststream_sqs_sns/prometheus/middleware.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |