-
Notifications
You must be signed in to change notification settings - Fork 3
feat: initial code release #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4938ce8
feat: migrate to github
cyclimse 0bf7719
chore: stop using |=
cyclimse 5acb4de
refactor: change versioning order
cyclimse 86a17f7
refactor: raise the exception instead of catching it
cyclimse 41314e3
fix: bad imports with src layout
cyclimse 45ec411
fix(framework): use older hints api to keep python3.8 compat
cyclimse 2a36352
chore: disable locally mypy because of issue with not-required
cyclimse 4aa180a
fix(examples): change hints location
cyclimse 38cccb6
chore: run mypy in strict mode
cyclimse 104312c
fix: small error in code found by pylance
cyclimse 7935326
chore: rename project with dashes
cyclimse 71a83d4
chore: refrasing and spacing mistakes
cyclimse b5a1bee
chore(test): format dict in test
cyclimse c58df9c
chore(ci): remove reporting
cyclimse 6aa9a30
docs: update emoji in readme reach-us for consistency accross repo
cyclimse a19b274
refactor: rename folders
cyclimse 2131f7d
refactor: add dunder to _format_request_context
cyclimse 113cda7
docs: remove unecessary emoji
cyclimse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from . import local as local | ||
| from .framework import v1 as v1 | ||
| from .local.serving import serve_handler as serve_handler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import v1 as v1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import hints as hints |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import typing as t | ||
|
|
||
| try: | ||
| from typing import NotRequired # type: ignore | ||
| except ImportError: | ||
| from typing_extensions import NotRequired | ||
|
|
||
|
|
||
| class RequestContext(t.TypedDict): | ||
| """Request context that is sent in the http event.""" | ||
|
|
||
| accountId: str | ||
| resourceId: str | ||
| stage: str | ||
| requestId: str | ||
| resourcePath: str | ||
| authorizer: t.Literal[None] | ||
| httpMethod: str | ||
| apiId: str | ||
|
|
||
|
|
||
| class Event(t.TypedDict): | ||
| """Event dictionnary passed to the function.""" | ||
|
|
||
| path: str | ||
| httpMethod: str | ||
| headers: t.Dict[str, str] | ||
| multiValueHeaders: t.Literal[None] | ||
| queryStringParameters: t.Dict[str, str] | ||
| multiValueQueryStringParameters: t.Literal[None] | ||
| pathParameters: t.Literal[None] | ||
| stageVariable: t.Dict[str, str] | ||
| requestContext: RequestContext | ||
| body: str | ||
| isBase64Encoded: NotRequired[t.Literal[True]] | ||
|
|
||
|
|
||
| class Context(t.TypedDict): | ||
| """Context dictionnary passed to the function.""" | ||
|
|
||
| memoryLimitInMb: int | ||
| functionName: str | ||
| functionVersion: str | ||
|
|
||
|
|
||
| class ResponseRecord(t.TypedDict, total=False): | ||
| """Response dictionnary that the handler is expected to return.""" | ||
|
|
||
| body: str | ||
| headers: t.Dict[str, str] | ||
| statusCode: int | ||
| isBase64Encoded: bool | ||
|
|
||
|
|
||
| # Type that the Serverless handler is expected to return | ||
| Response = t.Union[str, ResponseRecord] | ||
|
|
||
| Handler = t.Callable[[Event, Context], Response] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .serving import serve_handler as serve_handler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ..framework.v1.hints import Context, Handler | ||
|
|
||
|
|
||
| def format_context(handler: "Handler") -> "Context": | ||
| """Formats the request context from the request.""" | ||
| return { | ||
| "memoryLimitInMb": 128, | ||
| "functionName": handler.__name__, | ||
| "functionVersion": "", | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import binascii | ||
| from base64 import b64decode | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from flask.wrappers import Request | ||
|
|
||
| from ..framework.v1.hints import Event, RequestContext | ||
|
|
||
|
|
||
| def _format_request_context(request: "Request") -> "RequestContext": | ||
| """Format the request context from the request.""" | ||
| return { | ||
| "accountId": "", | ||
| "resourceId": "", | ||
| "stage": "", | ||
| "requestId": "", | ||
| "resourcePath": "", | ||
| "authorizer": None, | ||
| "httpMethod": request.method, | ||
| "apiId": "", | ||
| } | ||
|
|
||
|
|
||
| def format_http_event(request: "Request") -> "Event": | ||
| """Format the event from a generic http request.""" | ||
| context = _format_request_context(request) | ||
| body = request.get_data(as_text=True) | ||
| event: "Event" = { | ||
| "path": request.path, | ||
| "httpMethod": request.method, | ||
| "headers": dict(request.headers.items()), | ||
| "multiValueHeaders": None, | ||
| "queryStringParameters": request.args.to_dict(), | ||
| "multiValueQueryStringParameters": None, | ||
| "pathParameters": None, | ||
| "stageVariable": {}, | ||
| "requestContext": context, | ||
| "body": body, | ||
| } # type: ignore # NotRequired works with Pylance here but not mypy 1.0 (bug?) | ||
| try: | ||
| b64decode(body, validate=True).decode("utf-8") | ||
| event["isBase64Encoded"] = True | ||
| except (binascii.Error, UnicodeDecodeError): | ||
| pass | ||
| return event |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| """Utility module to inject provider-side headers.""" | ||
|
|
||
| import uuid | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from flask.wrappers import Request, Response | ||
|
|
||
| from ..framework.v1.hints import Event | ||
|
|
||
|
|
||
| def inject_ingress_headers(request: "Request", event: "Event") -> None: | ||
| """Inject headers for incoming requests. | ||
|
|
||
| ..note:: | ||
|
|
||
| Because WGSI request headers are immutable, | ||
| it's simpler to inject them into the event object directly. | ||
| """ | ||
| if not request.remote_addr: | ||
| raise RuntimeWarning("remote_addr is not set in the request") | ||
| headers = { | ||
| "Forwarded": f"for={request.remote_addr};proto=http", | ||
| "X-Forwarded-For": request.remote_addr, | ||
| "X-Envoy-External-Adrdress": request.remote_addr, | ||
| "X-Forwarded-Proto": "http", | ||
| # In this context "X-Forwared-For" == "X-Envoy-External-Address" | ||
| # this property doesn't hold for actual functions | ||
| "X-Envoy-External-Address": request.remote_addr, | ||
| "X-Request-Id": str(uuid.uuid4()), | ||
| } | ||
| # Not using |= to keep compatibility with python 3.8 | ||
| event["headers"].update(**headers) | ||
|
|
||
|
|
||
| def inject_egress_headers(response: "Response") -> None: | ||
| """Inject headers for outgoing requests.""" | ||
| response.headers.add("server", "envoy") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.