Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional boto3 #115

Merged
merged 2 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion docs/lifespan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions docs/websockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

Expand Down
10 changes: 7 additions & 3 deletions mangum/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)):
Expand Down