diff --git a/docs/http.md b/docs/http.md index 1cb8cf6b..72c61c8e 100644 --- a/docs/http.md +++ b/docs/http.md @@ -39,7 +39,7 @@ Additionally, any `Content-Type` header prefixed with `text/` is automatically e If the `Content-Encoding` header is set to `gzip`, then a binary response will be returned regardless of MIME type. -## API +## State machine The `HTTPCycle` is used by the adapter to communicate message events between the application and AWS. It is a state machine that handles the entire ASGI request and response cycle. diff --git a/docs/lifespan.md b/docs/lifespan.md index 19069545..e23daa2f 100644 --- a/docs/lifespan.md +++ b/docs/lifespan.md @@ -46,7 +46,7 @@ Lifespan support is automatically determined unless explicitly turned on or off. Defaults to `auto`. -## API +## State machine The `LifespanCycle` is a state machine that handles ASGI `lifespan` events intended to run before and after HTTP and WebSocket requests are handled. diff --git a/docs/websockets.md b/docs/websockets.md index 98f926eb..c4630af3 100644 --- a/docs/websockets.md +++ b/docs/websockets.md @@ -235,7 +235,7 @@ sqlite://[file_path].db The file name or path to an sqlite3 database file. If one does not exist, then it will be created automatically. -## API +## State machine The `WebSocketCycle` is used by the adapter to communicate message events between the application and WebSocket client connections in API Gateway using a storage backend to persist the connection `scope`. It is a state machine that handles the ASGI request and response cycle for each individual message sent by a client. @@ -245,7 +245,7 @@ The `WebSocketCycle` is used by the adapter to communicate message events betwee :docstring: :members: run receive send -#### Handling API Gateway events +#### API Gateway events There are three WebSocket events sent by API Gateway for a WebSocket API connection. Each event requires returning a response immediately, and the information required to create the connection scope is only available in the initial `CONNECT` event. Messages are only sent in `MESSAGE` events that occur after the initial connection is established, and they do not include the details of the initial connect event. Due to the stateless nature of AWS Lambda, a storage backend is required to persist the WebSocket connection details for the duration of a client connection. diff --git a/mangum/websocket.py b/mangum/websocket.py index a8d500bd..3bedf98a 100644 --- a/mangum/websocket.py +++ b/mangum/websocket.py @@ -6,9 +6,11 @@ from mangum.types import Scope from mangum.exceptions import WebSocketError, ConfigurationError - -import boto3 -from botocore.exceptions import ClientError +try: + import boto3 + from botocore.exceptions import ClientError +except ImportError: # pragma: no cover + boto3 = None @dataclass @@ -20,6 +22,8 @@ class WebSocket: api_gateway_endpoint_url: str def __post_init__(self) -> None: + if boto3 is None: # pragma: no cover + raise WebSocketError("boto3 must be installed to use WebSockets.") self.logger: logging.Logger = logging.getLogger("mangum.websocket") parsed_dsn = urlparse(self.dsn) if not any((parsed_dsn.hostname, parsed_dsn.path)):