-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
add support for form data request #225
Merged
kemingy
merged 4 commits into
0b01001001:master
from
yedpodtrzitko:yed/form-data-support
Aug 4, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Empty file.
This file contains 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,20 @@ | ||
from pydantic import BaseModel, Field | ||
|
||
from spectree import BaseFile | ||
|
||
|
||
class File(BaseModel): | ||
uid: str | ||
file: BaseFile | ||
|
||
|
||
class FileResp(BaseModel): | ||
filename: str | ||
type: str | ||
|
||
|
||
class Query(BaseModel): | ||
text: str = Field( | ||
..., | ||
max_length=100, | ||
) |
This file contains 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,119 @@ | ||
import logging | ||
from random import random | ||
|
||
import falcon.asgi | ||
import uvicorn | ||
from pydantic import BaseModel, Field | ||
|
||
from examples.common import File, FileResp, Query | ||
from spectree import Response, SpecTree, Tag | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
logger = logging.getLogger() | ||
|
||
api = SpecTree( | ||
"falcon-asgi", | ||
title="Demo Service", | ||
version="0.1.2", | ||
unknown="test", | ||
) | ||
|
||
demo = Tag(name="demo", description="😊", externalDocs={"url": "https://github.com"}) | ||
|
||
|
||
class Resp(BaseModel): | ||
label: int = Field( | ||
..., | ||
ge=0, | ||
le=9, | ||
) | ||
score: float = Field( | ||
..., | ||
gt=0, | ||
lt=1, | ||
) | ||
|
||
|
||
class BadLuck(BaseModel): | ||
loc: str | ||
msg: str | ||
typ: str | ||
|
||
|
||
class Data(BaseModel): | ||
uid: str | ||
limit: int | ||
vip: bool | ||
|
||
|
||
class Ping: | ||
def check(self): | ||
pass | ||
|
||
@api.validate(tags=[demo]) | ||
async def on_get(self, req, resp): | ||
""" | ||
health check | ||
""" | ||
self.check() | ||
logger.debug("ping <> pong") | ||
resp.media = {"msg": "pong"} | ||
|
||
|
||
class Classification: | ||
""" | ||
classification demo | ||
""" | ||
|
||
@api.validate(tags=[demo]) | ||
async def on_get(self, req, resp, source, target): | ||
""" | ||
API summary | ||
|
||
description here: test information with `source` and `target` | ||
""" | ||
resp.media = {"msg": f"hello from {source} to {target}"} | ||
|
||
@api.validate( | ||
query=Query, json=Data, resp=Response(HTTP_200=Resp, HTTP_403=BadLuck) | ||
) | ||
async def on_post(self, req, resp, source, target): | ||
""" | ||
post demo | ||
|
||
demo for `query`, `data`, `resp`, `x` | ||
""" | ||
logger.debug(f"{source} => {target}") | ||
logger.info(req.context.query) | ||
logger.info(req.context.json) | ||
if random() < 0.5: | ||
resp.status = falcon.HTTP_403 | ||
resp.media = {"loc": "unknown", "msg": "bad luck", "typ": "random"} | ||
return | ||
resp.media = {"label": int(10 * random()), "score": random()} | ||
|
||
|
||
class FileUpload: | ||
""" | ||
file-handling demo | ||
""" | ||
|
||
@api.validate(form=File, resp=Response(HTTP_200=FileResp), tags=["file-upload"]) | ||
async def on_post(self, req, resp): | ||
""" | ||
post multipart/form-data demo | ||
|
||
demo for 'form' | ||
""" | ||
file = req.context.form.file | ||
resp.media = {"filename": file.filename, "type": file.type} | ||
|
||
|
||
if __name__ == "__main__": | ||
app = falcon.asgi.App() | ||
app.add_route("/ping", Ping()) | ||
app.add_route("/api/{source}/{target}", Classification()) | ||
app.add_route("/api/file_upload", FileUpload()) | ||
api.register(app) | ||
|
||
uvicorn.run(app, log_level="info") |
This file contains 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 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 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 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 |
---|---|---|
|
@@ -52,7 +52,7 @@ | |
"black~=22.3", | ||
"isort~=5.10", | ||
"autoflake~=1.4", | ||
"mypy>=0.942", | ||
"mypy>=0.971", | ||
], | ||
}, | ||
zip_safe=False, | ||
|
This file contains 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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import logging | ||
|
||
from .models import SecurityScheme, Tag | ||
from .models import BaseFile, SecurityScheme, Tag | ||
from .response import Response | ||
from .spec import SpecTree | ||
|
||
__all__ = ["SpecTree", "Response", "Tag", "SecurityScheme"] | ||
__all__ = ["SpecTree", "Response", "Tag", "SecurityScheme", "BaseFile"] | ||
|
||
# setup library logging | ||
logging.getLogger(__name__).addHandler(logging.NullHandler()) |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's another way to return the response (#212). I guess we can make it more clear in the comments.