Skip to content

Commit

Permalink
Merge pull request #14 from odd12258053/support_multipart_in_gcp
Browse files Browse the repository at this point in the history
Support request forms and files in Google Cloud Functions
  • Loading branch information
odd12258053 authored Feb 4, 2022
2 parents 1f191e5 + 8b99321 commit d42fde2
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 9 deletions.
2 changes: 1 addition & 1 deletion agraffe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" Agraffe, build API with ASGI in Serverless services (e.g AWS lambda, Google Cloud Functions and Azure Functions). """ # noqa: E501

__version__ = "0.4.0"
__version__ = "0.5.0"

import asyncio
from enum import Enum
Expand Down
2 changes: 1 addition & 1 deletion agraffe/services/google_cloud_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def receive(self) -> Message:
return {
'type': 'http.request',
'body': self.request.get_data(
cache=False, as_text=False, parse_form_data=True
cache=False, as_text=False, parse_form_data=False
)
or b'',
'more_body': False,
Expand Down
31 changes: 30 additions & 1 deletion example/aws/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional

from fastapi import Cookie, FastAPI, Header, HTTPException
from fastapi import Cookie, FastAPI, Header, HTTPException, Form, File, UploadFile
from fastapi.responses import PlainTextResponse, Response

from models import Item
Expand Down Expand Up @@ -60,3 +60,32 @@ def image():
content=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x0c\x00\x00\x00\x0c\x08\x02\x00\x00\x00\xd9\x17\xcb\xb0\x00\x00\x00\x16IDATx\x9ccLIIa \x04\x98\x08\xaa\x18U4\x00\x8a\x00\x1c\xa2\x01D2\xdd\xa6B\x00\x00\x00\x00IEND\xaeB`\x82',
media_type='image/png'
)


@app.post('/form')
def form(token: str = Form(...)):
return {
'token': token
}


@app.post('/file')
async def file(file: bytes = File(...)):
return {'file_size': len(file)}


@app.post('/uploadfile')
async def uploadfile(file: UploadFile = File(...)):
return {'filename': file.filename}


@app.post('/file_and_form')
async def file_and_form(
file: bytes = File(...), fileb: UploadFile = File(...), token: str = Form(...)
):
return {
'file_size': len(file),
'token': token,
'fileb_content_type': fileb.content_type,
'filename': fileb.filename
}
2 changes: 1 addition & 1 deletion example/aws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"sls": "sls"
},
"dependencies": {
"serverless": "^2.57.0"
"serverless": "^3.1.1"
},
"devDependencies": {
"serverless-python-requirements": "^5.1.0"
Expand Down
3 changes: 2 additions & 1 deletion example/aws/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ fastapi==0.68.1
pydantic==1.8.2
starlette==0.14.2
typing-extensions==3.7.4.3
agraffe==0.4.0
python-multipart==0.0.5
agraffe==0.5.0
12 changes: 12 additions & 0 deletions example/aws/serverless_http.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ functions:
- httpApi:
path: /image
method: GET
- httpApi:
path: /form
method: POST
- httpApi:
path: /file
method: POST
- httpApi:
path: /uploadfile
method: POST
- httpApi:
path: /file_and_form
method: POST


plugins:
Expand Down
12 changes: 12 additions & 0 deletions example/aws/serverless_rest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ functions:
- http:
path: /image
method: get
- http:
path: /form
method: post
- http:
path: /file
method: post
- http:
path: /uploadfile
method: post
- http:
path: /file_and_form
method: post

plugins:
- serverless-python-requirements
Expand Down
31 changes: 30 additions & 1 deletion example/azure/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional

from fastapi import Cookie, FastAPI, Header, HTTPException
from fastapi import Cookie, FastAPI, Header, HTTPException, Form, File, UploadFile
from fastapi.responses import PlainTextResponse, Response

from models import Item
Expand Down Expand Up @@ -60,3 +60,32 @@ def image():
content=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x0c\x00\x00\x00\x0c\x08\x02\x00\x00\x00\xd9\x17\xcb\xb0\x00\x00\x00\x16IDATx\x9ccLIIa \x04\x98\x08\xaa\x18U4\x00\x8a\x00\x1c\xa2\x01D2\xdd\xa6B\x00\x00\x00\x00IEND\xaeB`\x82',
media_type='image/png'
)


@app.post('/form')
def form(token: str = Form(...)):
return {
'token': token
}


@app.post('/file')
async def file(file: bytes = File(...)):
return {'file_size': len(file)}


@app.post('/uploadfile')
async def uploadfile(file: UploadFile = File(...)):
return {'filename': file.filename}


@app.post('/file_and_form')
async def file_and_form(
file: bytes = File(...), fileb: UploadFile = File(...), token: str = Form(...)
):
return {
'file_size': len(file),
'token': token,
'fileb_content_type': fileb.content_type,
'filename': fileb.filename
}
1 change: 1 addition & 0 deletions example/azure/file/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from entry_point import main
18 changes: 18 additions & 0 deletions example/azure/file/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "Anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["post"],
"route": "file"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
1 change: 1 addition & 0 deletions example/azure/file_and_form/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from entry_point import main
18 changes: 18 additions & 0 deletions example/azure/file_and_form/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "Anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["post"],
"route": "file_and_form"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
1 change: 1 addition & 0 deletions example/azure/form/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from entry_point import main
18 changes: 18 additions & 0 deletions example/azure/form/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "Anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["post"],
"route": "form"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
3 changes: 2 additions & 1 deletion example/azure/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ fastapi==0.68.1
pydantic==1.8.2
starlette==0.14.2
typing-extensions==3.7.4.3
agraffe==0.4.0
python-multipart==0.0.5
agraffe==0.5.0
1 change: 1 addition & 0 deletions example/azure/uploadfile/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from entry_point import main
18 changes: 18 additions & 0 deletions example/azure/uploadfile/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "Anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["post"],
"route": "uploadfile"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
31 changes: 30 additions & 1 deletion example/gcp/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional

from fastapi import Cookie, FastAPI, Header, HTTPException
from fastapi import Cookie, FastAPI, Header, HTTPException, Form, File, UploadFile
from fastapi.responses import PlainTextResponse, Response

from models import Item
Expand Down Expand Up @@ -60,3 +60,32 @@ def image():
content=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x0c\x00\x00\x00\x0c\x08\x02\x00\x00\x00\xd9\x17\xcb\xb0\x00\x00\x00\x16IDATx\x9ccLIIa \x04\x98\x08\xaa\x18U4\x00\x8a\x00\x1c\xa2\x01D2\xdd\xa6B\x00\x00\x00\x00IEND\xaeB`\x82',
media_type='image/png'
)


@app.post('/form')
def form(token: str = Form(...)):
return {
'token': token
}


@app.post('/file')
async def file(file: bytes = File(...)):
return {'file_size': len(file)}


@app.post('/uploadfile')
async def uploadfile(file: UploadFile = File(...)):
return {'filename': file.filename}


@app.post('/file_and_form')
async def file_and_form(
file: bytes = File(...), fileb: UploadFile = File(...), token: str = Form(...)
):
return {
'file_size': len(file),
'token': token,
'fileb_content_type': fileb.content_type,
'filename': fileb.filename
}
3 changes: 2 additions & 1 deletion example/gcp/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ fastapi==0.68.1
pydantic==1.8.2
starlette==0.14.2
typing-extensions==3.7.4.3
agraffe==0.4.0
python-multipart==0.0.5
agraffe==0.5.0

0 comments on commit d42fde2

Please sign in to comment.