Skip to content

Commit ed1dd3a

Browse files
authored
Merge pull request #20 from odd12258053/auto_detect_service
Allow calling without no service to `Agraffe.entry_point`.
2 parents 226ef78 + 6435c4f commit ed1dd3a

17 files changed

Lines changed: 72 additions & 61 deletions

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ Agraffe, build API with ASGI in Serverless services (e.g AWS lambda, Google Clou
88

99
## Support Services
1010
- [x] Google Cloud Functions
11+
- Python 3.7, 3.8, 3.9, 3.10, 3.11(preview)
1112
- [x] AWS lambda (with API Gateway HTTP API or REST API, or with Function URL)
13+
- Python 3.7, 3.8, 3.9
1214
- [x] Azure Functions
15+
- Python 3.7, 3.8, 3.9, 3.10(preview)
1316

1417
## Requirements
1518

16-
Python 3.7, 3.8, 3.9
19+
Python 3.7+
1720

1821
## Installation
1922
```sh
@@ -26,9 +29,7 @@ Create it
2629
- Create a file `main.py` with:
2730

2831
```python
29-
from agraffe import Agraffe, Service
30-
31-
from typing import Optional
32+
from agraffe import Agraffe
3233

3334
from fastapi import FastAPI
3435

@@ -41,13 +42,13 @@ def read_root():
4142

4243

4344
@app.get("/items/{item_id}")
44-
def read_item(item_id: int, q: Optional[str] = None):
45+
def read_item(item_id: int, q: str | None = None):
4546
return {"item_id": item_id, "q": q}
4647

47-
entry_point = Agraffe.entry_point(app, Service.google_cloud_functions)
48+
entry_point = Agraffe.entry_point(app)
4849
```
4950
```python
50-
# or
51+
# or, for on GCP
5152
from agraffe.services.google_cloud_functions import HttpCycle
5253

5354
def entry_point(request):
@@ -59,8 +60,10 @@ Deploy it
5960
- Deploy the api with:
6061

6162
```sh
62-
$ gcloud functions deploy {FUNCTION NAME} --entry-point entry_point --runtime python37 --trigger-http --allow-unauthenticated
63+
$ gcloud functions deploy {FUNCTION NAME} --entry-point entry_point --runtime python310 --trigger-http --allow-unauthenticated
6364
```
6465

66+
See `/example` for other services.
67+
6568
## License
6669
This project is licensed under the terms of the MIT license.

agraffe/__init__.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
""" Agraffe, build API with ASGI in Serverless services (e.g AWS lambda, Google Cloud Functions and Azure Functions). """ # noqa: E501
22

3-
__version__ = "0.5.0"
3+
__version__ = "0.6.0"
44

55
import asyncio
6-
from enum import Enum
76
from typing import Any, Callable, Type, Union
87

8+
from .services import Service
99
from .types import ASGIApp, ASGICycle
10-
11-
12-
class Service(str, Enum):
13-
google_cloud_functions = 'Google Cloud Functions'
14-
aws_lambda = 'AWS Lambda'
15-
azure_functions = 'Azure Functions'
10+
from .utils import find_service
1611

1712

1813
class Agraffe:
19-
def __init__(self, app: ASGIApp, http_cycle: Type[ASGICycle]):
14+
def __init__(self, app: ASGIApp, http_cycle: Type[ASGICycle]) -> None:
2015
self.app = app
2116
self._http_cycle = http_cycle
2217
loop = asyncio.new_event_loop()
@@ -29,8 +24,11 @@ def __call__(self, request: Any) -> Any:
2924

3025
@classmethod
3126
def entry_point(
32-
cls, app: ASGIApp, service: Union[str, Service]
27+
cls, app: ASGIApp, service: Union[str, Service, None] = None
3328
) -> Callable[..., Any]:
29+
if service is None:
30+
service = find_service()
31+
3432
if service == Service.google_cloud_functions:
3533
from .services.google_cloud_functions import HttpCycle as GCPHttpCycle
3634

agraffe/services/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from enum import Enum
2+
3+
4+
class Service(str, Enum):
5+
google_cloud_functions = 'Google Cloud Functions'
6+
aws_lambda = 'AWS Lambda'
7+
azure_functions = 'Azure Functions'

agraffe/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
import sys
3+
from typing import Union
4+
5+
from .services import Service
6+
7+
8+
def find_service() -> Union[Service, None]:
9+
# ref: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
10+
if 'AWS_LAMBDA_FUNCTION_NAME' in os.environ:
11+
return Service.aws_lambda
12+
13+
# ref: https://learn.microsoft.com/en-us/azure/azure-functions/functions-app-settings # noqa: 501
14+
if 'FUNCTIONS_WORKER_RUNTIME' in os.environ:
15+
return Service.azure_functions
16+
17+
# ref: https://cloud.google.com/functions/docs/configuring/env-var#newer_runtimes
18+
if sys.version_info >= (3, 8) and 'FUNCTION_TARGET' in os.environ:
19+
return Service.google_cloud_functions
20+
elif 'GCP_PROJECT' in os.environ and 'FUNCTION_NAME' in os.environ:
21+
return Service.google_cloud_functions
22+
23+
return None

example/aws/.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
node_modules/
21
agraffe/
3-
.serverless/
4-
package-lock.json
52
samconfig.toml
63
.aws-sam/

example/aws/src/main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import os
2-
31
from agraffe import Agraffe
42

53
from app import app
64

75

8-
entry_point = Agraffe.entry_point(app, os.environ['AgraffeService'])
6+
entry_point = Agraffe.entry_point(app)

example/aws/src/requirements.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
fastapi==0.68.1
2-
pydantic==1.8.2
3-
starlette==0.14.2
4-
typing-extensions==3.7.4.3
5-
python-multipart==0.0.5
6-
agraffe==0.5.0
1+
fastapi
2+
pydantic
3+
typing-extensions
4+
python-multipart
5+
agraffe>=0.6.0

example/aws/template_api.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ Resources:
1010
Handler: main.entry_point
1111
MemorySize: 256
1212
Timeout: 30
13-
Environment:
14-
Variables:
15-
AgraffeService: AWS Lambda
1613
Events:
1714
Index:
1815
Type: Api

example/aws/template_function_url.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,3 @@ Resources:
1212
Timeout: 30
1313
FunctionUrlConfig:
1414
AuthType: NONE
15-
Environment:
16-
Variables:
17-
AgraffeService: AWS Lambda

example/aws/template_httpapi.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ Resources:
1010
Handler: main.entry_point
1111
MemorySize: 256
1212
Timeout: 30
13-
Environment:
14-
Variables:
15-
AgraffeService: AWS Lambda
1613
Events:
1714
Index:
1815
Type: HttpApi

0 commit comments

Comments
 (0)