-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from lsst-sqre/tickets/DM-31361
[DM-31361] Safir improvements from mobu
- Loading branch information
Showing
6 changed files
with
90 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Standard models for FastAPI applications. | ||
Examples | ||
-------- | ||
To reference the `ErrorModel` model when returning an error message, use code | ||
similar to this: | ||
.. code-block:: python | ||
@router.get( | ||
"/route/{foo}", | ||
..., | ||
responses={404: {"description": "Not found", "model": ErrorModel}}, | ||
) | ||
async def route(foo: str) -> None: | ||
... | ||
raise HTTPException( | ||
status_code=status.HTTP_404_NOT_FOUND, | ||
detail=[ | ||
{"loc": ["path", "foo"], "msg": msg, "type": "invalid_foo"}, | ||
], | ||
) | ||
Notes | ||
----- | ||
FastAPI does not appear to export its error response model in a usable form, | ||
so define a copy of it so that we can reference it in API definitions to | ||
generate good documentation. | ||
""" | ||
|
||
# Examples and notes kept in the module docstring because they're not | ||
# appropriate for the API documentation generated for a service. | ||
|
||
from typing import List, Optional | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
__all__ = ["ErrorDetail", "ErrorModel"] | ||
|
||
|
||
class ErrorDetail(BaseModel): | ||
"""The detail of the error message.""" | ||
|
||
loc: Optional[List[str]] = Field( | ||
None, title="Location", example=["area", "field"] | ||
) | ||
|
||
msg: str = Field(..., title="Message", example="Some error messge") | ||
|
||
type: str = Field(..., title="Error type", example="some_code") | ||
|
||
|
||
class ErrorModel(BaseModel): | ||
"""A structured API error message.""" | ||
|
||
detail: List[ErrorDetail] = Field(..., title="Detail") |
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,22 @@ | ||
"""Tests for safir.models.""" | ||
|
||
from __future__ import annotations | ||
|
||
import json | ||
|
||
from safir.models import ErrorModel | ||
|
||
|
||
def test_error_model() -> None: | ||
"""Nothing much to test, but make sure the code can be imported.""" | ||
error = { | ||
"detail": [ | ||
{ | ||
"loc": ["path", "foo"], | ||
"msg": "Invalid foo", | ||
"type": "invalid_foo", | ||
} | ||
] | ||
} | ||
model = ErrorModel.parse_raw(json.dumps(error)) | ||
assert model.dict() == error |