Skip to content

Commit 3f312ac

Browse files
Renaming and formatting (#171)
* Rename ScopeDict -> Scope, remove and update docstrings, rename scope -> request in handlers
1 parent 44cb353 commit 3f312ac

12 files changed

Lines changed: 51 additions & 39 deletions

mangum/adapter.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
from .protocols import HTTPCycle, LifespanCycle
1313
from .types import ASGIApp
1414

15+
1516
if TYPE_CHECKING: # pragma: no cover
1617
from awslambdaric.lambda_context import LambdaContext
1718

19+
1820
DEFAULT_TEXT_MIME_TYPES = [
1921
"text/",
2022
"application/json",
@@ -23,6 +25,7 @@
2325
"application/vnd.api+json",
2426
]
2527

28+
2629
logger = logging.getLogger("mangum")
2730

2831

@@ -34,8 +37,6 @@ class Mangum:
3437
specification. This will usually be an ASGI framework application instance.
3538
* **lifespan** - A string to configure lifespan support. Choices are `auto`, `on`,
3639
and `off`. Default is `auto`.
37-
* **log_level** - A string to configure the log level. Choices are: `info`,
38-
`critical`, `error`, `warning`, and `debug`. Default is `info`.
3940
* **text_mime_types** - A list of MIME types to include with the defaults that
4041
should not return a binary response in API Gateway.
4142
"""
@@ -44,11 +45,8 @@ class Mangum:
4445
lifespan: str = "auto"
4546

4647
def __init__(
47-
self,
48-
app: ASGIApp,
49-
lifespan: str = "auto",
50-
**handler_kwargs: Dict[str, Any],
51-
):
48+
self, app: ASGIApp, lifespan: str = "auto", **handler_kwargs: Dict[str, Any]
49+
) -> None:
5250
self.app = app
5351
self.lifespan = lifespan
5452
self.handler_kwargs = handler_kwargs
@@ -69,7 +67,7 @@ def __call__(self, event: dict, context: "LambdaContext") -> dict:
6967
handler = AbstractHandler.from_trigger(
7068
event, context, **self.handler_kwargs
7169
)
72-
http_cycle = HTTPCycle(handler.scope)
70+
http_cycle = HTTPCycle(handler.request)
7371
response = http_cycle(self.app, handler.body)
7472

7573
return handler.transform_response(response)

mangum/handlers/abstract_handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from .. import Response, Request
66

7+
78
if TYPE_CHECKING: # pragma: no cover
89
from awslambdaric.lambda_context import LambdaContext
910

@@ -20,7 +21,7 @@ def __init__(
2021

2122
@property
2223
@abstractmethod
23-
def scope(self) -> Request:
24+
def request(self) -> Request:
2425
"""
2526
Parse an ASGI scope from the request event
2627
"""

mangum/handlers/aws_alb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class AwsAlb(AbstractHandler):
1717
TYPE = "AWS_ALB"
1818

1919
@property
20-
def scope(self) -> Request:
20+
def request(self) -> Request:
2121
event = self.trigger_event
2222

2323
headers = {}

mangum/handlers/aws_api_gateway.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .abstract_handler import AbstractHandler
66
from .. import Response, Request
77

8+
89
if TYPE_CHECKING: # pragma: no cover
910
from awslambdaric.lambda_context import LambdaContext
1011

@@ -30,7 +31,7 @@ def __init__(
3031
self.base_path = base_path
3132

3233
@property
33-
def scope(self) -> Request:
34+
def request(self) -> Request:
3435
event = self.trigger_event
3536

3637
# multiValue versions of headers take precedence over their plain versions

mangum/handlers/aws_cf_lambda_at_edge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AwsCfLambdaAtEdge(AbstractHandler):
1616
TYPE = "AWS_CF_LAMBDA_AT_EDGE"
1717

1818
@property
19-
def scope(self) -> Request:
19+
def request(self) -> Request:
2020
event = self.trigger_event
2121

2222
cf_request = event["Records"][0]["cf"]["request"]

mangum/handlers/aws_http_gateway.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def event_version(self) -> str:
2121
return self.trigger_event.get("version", "")
2222

2323
@property
24-
def scope(self) -> Request:
24+
def request(self) -> Request:
2525
event = self.trigger_event
2626

2727
headers = {}

mangum/protocols/http.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ class HTTPCycle:
3535
"""
3636
Manages the application cycle for an ASGI `http` connection.
3737
38-
* **scope** - A dictionary containing the connection scope used to run the ASGI
39-
application instance.
38+
* **request** - A request object containing the event and context for the connection
39+
scope used to run the ASGI application instance.
4040
* **state** - An enumerated `HTTPCycleState` type that indicates the state of the
4141
ASGI connection.
4242
* **app_queue** - An asyncio queue (FIFO) containing messages to be received by the
4343
application.
4444
* **response** - A dictionary containing the response data to return in AWS Lambda.
4545
"""
4646

47-
scope: Request
47+
request: Request
4848
state: HTTPCycleState = HTTPCycleState.REQUEST
4949
response: Optional[Response] = None
5050

@@ -78,7 +78,7 @@ async def run(self, app: ASGIApp) -> None:
7878
Calls the application with the `http` connection scope.
7979
"""
8080
try:
81-
await app(self.scope.as_dict(), self.receive, self.send)
81+
await app(self.request.scope, self.receive, self.send)
8282
except BaseException as exc:
8383
self.logger.error("Exception in 'http' protocol.", exc_info=exc)
8484
if self.state is HTTPCycleState.REQUEST:

mangum/types.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
import typing
21
from dataclasses import dataclass, field
3-
from typing import List, Tuple, Dict, Any, Union, Optional, TYPE_CHECKING
4-
2+
from typing import (
3+
List,
4+
Tuple,
5+
Dict,
6+
Any,
7+
Union,
8+
Optional,
9+
MutableMapping,
10+
Awaitable,
11+
Callable,
12+
TYPE_CHECKING,
13+
)
514
from typing_extensions import Protocol
615

7-
Message = typing.MutableMapping[str, typing.Any]
8-
ScopeDict = typing.MutableMapping[str, typing.Any]
9-
Receive = typing.Callable[[], typing.Awaitable[Message]]
10-
Send = typing.Callable[[Message], typing.Awaitable[None]]
16+
17+
Message = MutableMapping[str, Any]
18+
Scope = MutableMapping[str, Any]
19+
Receive = Callable[[], Awaitable[Message]]
20+
Send = Callable[[Message], Awaitable[None]]
21+
1122

1223
if TYPE_CHECKING: # pragma: no cover
1324
from awslambdaric.lambda_context import LambdaContext
1425

1526

1627
class ASGIApp(Protocol):
17-
async def __call__(self, scope: ScopeDict, receive: Receive, send: Send) -> None:
28+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
1829
... # pragma: no cover
1930

2031

@@ -46,7 +57,8 @@ class Request:
4657
root_path: str = ""
4758
asgi: Dict[str, str] = field(default_factory=lambda: {"version": "3.0"})
4859

49-
def as_dict(self) -> ScopeDict:
60+
@property
61+
def scope(self) -> Scope:
5062
return {
5163
"type": self.type,
5264
"http_version": self.http_version,

tests/handlers/test_aws_alb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_aws_alb_basic():
7474

7575
example_context = {}
7676
handler = AwsAlb(example_event, example_context)
77-
assert handler.scope.as_dict() == {
77+
assert handler.request.scope == {
7878
"asgi": {"version": "3.0"},
7979
"aws.context": {},
8080
"aws.event": example_event,
@@ -171,7 +171,7 @@ def test_aws_alb_scope_real(
171171
if scope_path == "":
172172
scope_path = "/"
173173

174-
assert handler.scope.as_dict() == {
174+
assert handler.request.scope == {
175175
"asgi": {"version": "3.0"},
176176
"aws.context": {},
177177
"aws.event": event,

tests/handlers/test_aws_api_gateway.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_aws_api_gateway_scope_basic():
9696
}
9797
example_context = {}
9898
handler = AwsApiGateway(example_event, example_context)
99-
assert handler.scope.as_dict() == {
99+
assert handler.request.scope == {
100100
"asgi": {"version": "3.0"},
101101
"aws.context": {},
102102
"aws.event": example_event,
@@ -180,7 +180,7 @@ def test_aws_api_gateway_scope_real(
180180
if scope_path == "":
181181
scope_path = "/"
182182

183-
assert handler.scope.as_dict() == {
183+
assert handler.request.scope == {
184184
"asgi": {"version": "3.0"},
185185
"aws.context": {},
186186
"aws.event": event,

0 commit comments

Comments
 (0)