Skip to content

Commit

Permalink
refactor: use Headers type if where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
sleistner committed Sep 16, 2019
1 parent 67c09cf commit 9eee909
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lambda_handlers/handlers/http_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""A handler for HTTP request events."""

import logging
from typing import Any, Dict
from typing import Any, Dict, Optional

from lambda_handlers.types import Headers, APIGatewayProxyResult
from lambda_handlers.errors import FormatError, NotFoundError, BadRequestError, ValidationError, ResultValidationError
Expand Down Expand Up @@ -78,7 +78,7 @@ def _create_response(self, result: APIGatewayProxyResult) -> Dict[str, Any]:
result.headers = self._create_headers(result.headers)
return self.format_output(self.validate_result(result.asdict()))

def _create_headers(self, headers: Headers) -> Headers:
def _create_headers(self, headers: Optional[Headers]) -> Optional[Headers]:
if not headers:
headers = {}

Expand Down
6 changes: 3 additions & 3 deletions lambda_handlers/response/cors_headers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""CORS Headers for responses."""

from typing import Dict, Union
from lambda_handlers.types import Headers


class CORSHeaders:
Expand All @@ -10,9 +10,9 @@ def __init__(self, origin: str = None, credentials: bool = False):
self.origin = origin or '*'
self.credentials = credentials

def create_headers(self) -> Dict[str, Union[str, bool]]:
def create_headers(self) -> Headers:
"""Return the CORS-related parts of the response header."""
headers: Dict[str, Union[str, bool]] = {
headers: Headers = {
'Access-Control-Allow-Origin': self.origin,
}
if self.credentials:
Expand Down
6 changes: 3 additions & 3 deletions lambda_handlers/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict, Union, Optional
from dataclasses import asdict, dataclass

Headers = Optional[Dict[str, Union[str, bool, int]]]
Headers = Dict[str, Union[str, bool, int]]


@dataclass
Expand All @@ -12,8 +12,8 @@ class APIGatewayProxyResult:

statusCode: int
body: Union[str, Dict[str, Any]]
headers: Headers = None
multiValueHeaders: Headers = None
headers: Optional[Headers] = None
multiValueHeaders: Optional[Headers] = None
isBase64Encoded: Optional[bool] = None

def asdict(self) -> Dict[str, Any]:
Expand Down

0 comments on commit 9eee909

Please sign in to comment.