Skip to content

Commit 0fd28e3

Browse files
Optional boto3 (#115)
* Only require boto3 for WebSocket support
1 parent 292aff2 commit 0fd28e3

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

docs/http.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Additionally, any `Content-Type` header prefixed with `text/` is automatically e
3939

4040
If the `Content-Encoding` header is set to `gzip`, then a binary response will be returned regardless of MIME type.
4141

42-
## API
42+
## State machine
4343

4444
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.
4545

docs/lifespan.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Lifespan support is automatically determined unless explicitly turned on or off.
4646

4747
Defaults to `auto`.
4848

49-
## API
49+
## State machine
5050

5151
The `LifespanCycle` is a state machine that handles ASGI `lifespan` events intended to run before and after HTTP and WebSocket requests are handled.
5252

docs/websockets.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ sqlite://[file_path].db
235235

236236
The file name or path to an sqlite3 database file. If one does not exist, then it will be created automatically.
237237

238-
## API
238+
## State machine
239239

240240
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.
241241

@@ -245,7 +245,7 @@ The `WebSocketCycle` is used by the adapter to communicate message events betwee
245245
:docstring:
246246
:members: run receive send
247247

248-
#### Handling API Gateway events
248+
#### API Gateway events
249249

250250
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.
251251

mangum/websocket.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
from mangum.types import Scope
77
from mangum.exceptions import WebSocketError, ConfigurationError
88

9-
10-
import boto3
11-
from botocore.exceptions import ClientError
9+
try:
10+
import boto3
11+
from botocore.exceptions import ClientError
12+
except ImportError: # pragma: no cover
13+
boto3 = None
1214

1315

1416
@dataclass
@@ -20,6 +22,8 @@ class WebSocket:
2022
api_gateway_endpoint_url: str
2123

2224
def __post_init__(self) -> None:
25+
if boto3 is None: # pragma: no cover
26+
raise WebSocketError("boto3 must be installed to use WebSockets.")
2327
self.logger: logging.Logger = logging.getLogger("mangum.websocket")
2428
parsed_dsn = urlparse(self.dsn)
2529
if not any((parsed_dsn.hostname, parsed_dsn.path)):

0 commit comments

Comments
 (0)