forked from 0b01001001/spectree
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: flask query list items (0b01001001#357)
* fix: flask query list items Signed-off-by: Keming <kemingy94@gmail.com> * format Signed-off-by: Keming <kemingy94@gmail.com> * fix lint Signed-off-by: Keming <kemingy94@gmail.com> * fix type lint Signed-off-by: Keming <kemingy94@gmail.com> * fix type in py38 Signed-off-by: Keming <kemingy94@gmail.com> * bump version Signed-off-by: Keming <kemingy94@gmail.com> --------- Signed-off-by: Keming <kemingy94@gmail.com>
- Loading branch information
Showing
13 changed files
with
268 additions
and
9 deletions.
There are no files selected for viewing
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,25 @@ | ||
from typing import List, Union | ||
|
||
from flask import Flask, jsonify, request | ||
from pydantic.v1 import BaseModel, Field | ||
|
||
from spectree import SpecTree | ||
|
||
|
||
class SampleQueryParams(BaseModel): | ||
id_list: Union[int, List[int]] = Field(..., description="List of IDs") | ||
|
||
|
||
app = Flask(__name__) | ||
spec = SpecTree("flask") | ||
|
||
|
||
@app.route("/api/v1/samples", methods=["GET"]) | ||
@spec.validate(query=SampleQueryParams) | ||
def get_samples(): | ||
return jsonify(text=f"it works: {request.context.query}") | ||
|
||
|
||
if __name__ == "__main__": | ||
spec.register(app) # if you don't register in api init step | ||
app.run(port=8000) |
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,59 @@ | ||
from typing import List, Union | ||
|
||
from flask import Flask, make_response | ||
from pydantic import BaseModel | ||
|
||
from spectree import Response, SpecTree | ||
|
||
|
||
class AppError(BaseModel): | ||
message: str | ||
|
||
|
||
class User(BaseModel): | ||
user_id: int | ||
|
||
|
||
class UserResponse(BaseModel): | ||
__root__: Union[List[User], AppError] | ||
|
||
|
||
class StrDict(BaseModel): | ||
__root__: dict[str, str] | ||
|
||
|
||
spec = SpecTree("flask") | ||
# spec = SpecTree("falcon") | ||
app = Flask(__name__) | ||
|
||
|
||
@app.route("/ping") | ||
@spec.validate(resp=Response(HTTP_200=StrDict)) | ||
def ping(): | ||
resp = make_response({"msg": "pong"}, 203) | ||
resp.set_cookie(key="pub", value="abcdefg") | ||
return resp | ||
|
||
|
||
@app.route("/users") | ||
@spec.validate(resp=Response(HTTP_200=UserResponse)) | ||
def get_users(): | ||
return [User(user_id=1), User(user_id=2)] | ||
|
||
|
||
class UserResource: | ||
@spec.validate(resp=Response(HTTP_200=UserResponse)) | ||
def on_get(self, req, resp): | ||
resp.media = [User(user_id=0)] | ||
|
||
|
||
if __name__ == "__main__": | ||
spec.register(app) | ||
app.run() | ||
|
||
# app = falcon.App() | ||
# app.add_route("/users", UserResource()) | ||
# spec.register(app) | ||
|
||
# httpd = simple_server.make_server("localhost", 8000, app) | ||
# httpd.serve_forever() |
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
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
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.