-
-
Notifications
You must be signed in to change notification settings - Fork 34
feat: Add response models #231
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b7db545
Add HelloResponse model for root endpoint
suchithh 521bbbf
Add models for /keys, /ping, /values and operation endpoints.
suchithh 7519cdf
Update route decorators with proper "response_model" parameters and F…
suchithh 510b8c0
Fix "dubious" spelling.
suchithh ffdb37f
Remove KeyStatistics Model (resolved by #230)
suchithh 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
| from . import db | ||
| from . import settings | ||
| from fastapi.middleware.cors import CORSMiddleware | ||
|
|
||
| from typing import List, Optional, Dict, Any, Union | ||
|
|
||
| description = """ | ||
| Folksonomy Engine API allows you to add free property/value pairs to Open Food Facts products. | ||
|
|
@@ -75,7 +75,7 @@ async def initialize_transactions(request: Request, call_next): | |
| return response | ||
|
|
||
|
|
||
| @app.get("/", status_code=status.HTTP_200_OK) | ||
| @app.get("/", status_code=status.HTTP_200_OK, response_model=HelloResponse) | ||
| async def hello(): | ||
| return {"message": "Hello folksonomy World! Tip: open /docs for documentation"} | ||
|
|
||
|
|
@@ -146,7 +146,7 @@ def get_auth_server(request: Request): | |
| return base_url | ||
|
|
||
|
|
||
| @app.post("/auth") | ||
| @app.post("/auth", response_model=TokenResponse) | ||
| async def authentication(request: Request, response: Response, form_data: OAuth2PasswordRequestForm = Depends()): | ||
| """ | ||
| Authentication: provide user/password and get a bearer token in return | ||
|
|
@@ -195,7 +195,7 @@ async def authentication(request: Request, response: Response, form_data: OAuth2 | |
| status_code=500, detail="Server error") | ||
|
|
||
|
|
||
| @app.post("/auth_by_cookie") | ||
| @app.post("/auth_by_cookie", response_model=TokenResponse) | ||
| async def authentication(request: Request, response: Response, session: Optional[str] = Cookie(None)): | ||
| """ | ||
| Authentication: provide Open Food Facts session cookie and get a bearer token in return | ||
|
|
@@ -410,7 +410,7 @@ async def product_tag_list_versions(response: Response, | |
| return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing": timing}) | ||
|
|
||
|
|
||
| @app.post("/product") | ||
| @app.post("/product", response_model=Dict[str, str]) | ||
| async def product_tag_add(response: Response, | ||
| product_tag: ProductTag, | ||
| user: User = Depends(get_current_user)): | ||
|
|
@@ -439,11 +439,11 @@ async def product_tag_add(response: Response, | |
| return JSONResponse(status_code=422, content={"detail": {"msg": error_msg}}) | ||
|
|
||
| if cur.rowcount == 1: | ||
| return "ok" | ||
| return {"status": "ok"} | ||
| return | ||
|
|
||
|
|
||
| @app.put("/product") | ||
| @app.put("/product", response_model=Dict[str, str]) | ||
| async def product_tag_update(response: Response, | ||
| product_tag: ProductTag, | ||
| user: User = Depends(get_current_user)): | ||
|
|
@@ -469,7 +469,7 @@ async def product_tag_update(response: Response, | |
| detail=re.sub(r'.*@@ (.*) @@\n.*$', r'\1', e.pgerror)[:-1], | ||
| ) | ||
| if cur.rowcount == 1: | ||
| return "ok" | ||
| return {"status": "ok"} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment. |
||
| elif cur.rowcount == 0: # non existing key | ||
| raise HTTPException( | ||
| status_code=404, | ||
|
|
@@ -478,11 +478,11 @@ async def product_tag_update(response: Response, | |
| else: | ||
| raise HTTPException( | ||
| status_code=503, | ||
| detail="Doubious update - more than one row udpated", | ||
| detail="Dubious update - more than one row udpated", | ||
| ) | ||
|
|
||
|
|
||
| @app.delete("/product/{product}/{k}") | ||
| @app.delete("/product/{product}/{k}", response_model=Dict[str, str]) | ||
| async def product_tag_delete(response: Response, | ||
| product: str, k: str, version: int, owner='', | ||
| user: User = Depends(get_current_user)): | ||
|
|
@@ -507,6 +507,8 @@ async def product_tag_delete(response: Response, | |
| status_code=422, | ||
| detail=re.sub(r'.*@@ (.*) @@\n.*$', r'\1', e.pgerror)[:-1], | ||
| ) | ||
| if cur.rowcount == 1: | ||
| return {"status": "ok"} | ||
| if cur.rowcount != 1: | ||
| raise HTTPException( | ||
| status_code=422, | ||
|
|
@@ -519,7 +521,7 @@ async def product_tag_delete(response: Response, | |
| (product, owner, k.lower()), | ||
| ) | ||
| if cur.rowcount == 1: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and the next line are redundant with line 510-511. |
||
| return "ok" | ||
| return {"status": "ok"} | ||
| else: | ||
| # we have a conflict, return an error explaining conflict | ||
| cur, timing = await db.db_exec( | ||
|
|
@@ -570,7 +572,7 @@ async def keys_list(response: Response, | |
| return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing": timing}) | ||
|
|
||
|
|
||
| @app.get("/values/{k}") | ||
| @app.get("/values/{k}", response_model=List[ValueCount]) | ||
| async def get_unique_values(response: Response, | ||
| k: str, | ||
| owner: str = '', | ||
|
|
@@ -622,11 +624,11 @@ async def get_unique_values(response: Response, | |
| return JSONResponse(status_code=200, content=data, headers={"x-pg-timing": timing}) | ||
|
|
||
|
|
||
| @app.get("/ping") | ||
| @app.get("/ping", response_model=PingResponse) | ||
| async def pong(response: Response): | ||
| """ | ||
| Check server health | ||
| """ | ||
| cur, timing = await db.db_exec("SELECT current_timestamp AT TIME ZONE 'GMT'",()) | ||
| pong = await cur.fetchone() | ||
| return {"ping": "pong @ %s" % pong[0]} | ||
| return {"ping": "pong @ %s" % pong[0]} | ||
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
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.
We're changing the API here. @CharlesNepote is that ok?
Uh oh!
There was an error while loading. Please reload this page.
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.
Regarding the change in the response model from a string ("ok") to a dict ({"status": "ok"}): while this change might affect the API, upgrading all responses to Pydantic models is key in the long run. One of FastAPI's core conventions is to use Pydantic models as much as possible, and changing the response structure now could potentially save many headaches in the future. Additionally, for validation failures (422), the response is already a JSON structure; depending on how the endpoint is used, it might be harder to check types on the frontend than to maintain consistency. Just my two cents.
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.
@suchithh I understand your concern, but API change might have impact on reusers: we need to talk with them first and evaluate potential impacts.
So you should push a first PR without modifying the API, and a second one to push API changes.