API Thoughts & Styles #14
jacob-a-brown
started this conversation in
General
Replies: 1 comment
-
Should all error messages follow Pydantic's detail format? Doing so would make it easy for users and the frontend to quickly identify where in the payload such an error occurred. This format was utilized in the original DataManager/AMPAPI to dynamically display those messages in appropriate fields and locations. The format is "detail": [
{
"type": error type like value_error,
"loc": where the error occurred in payload, like [body, sample_top],
"msg": error message,
"input": input value that caused the error
}
] |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Thoughts on adopting the following API styles? After debate and discussion and consensus I'll write up an ADR:
Goals
Maintain an API that is easy to upkeep, understand, and use.
How
Style
snake-case
for all paths/observation/groundwater-level
instead of/observation/groundwater_level
or/observation/groundwaterlevel
kebab_case
for all query and path parameters/thing/{thing_id}
or/thing?thing_id=...
?format=
query parameter to let the user specify which format they desire.?format=shapefile
,?format=csv
,?format=geojson
/thing/{thing_id}
and/thing?thing_id=...
/thing?thing_id=1,2,3,4,5
Data Validation
The same custom data validations should be performed for both POST and PATCH requests. This is easily achieved by creating a base Validation Pydantic schema from which Create and Update schemas inherit. Since some fields are required for Create and optional for Update schemas, each field validator should set
check_fields=False
.e.g.
Database errors, such as foreign key constraints and uniqueness, should be handled by the database, not Pydantic. This minimizes the number of calls made to the database because if Pydantic were used for these data validations extra calls would be made to the database (for every request
n
more database calls would be made wheren
are the number of validations performed). try/except clauses should be used to handle these errors and return appropriate error messages.Errors & Empty Data Sets
GET /thing/1
should return a 404 error if there is no object with that IDGET/thing?thing_id=1
should return an empty set if there is no object with that idGET /thing?thing_id=1,2,3,4,5
should return all objects with the valid IDs. No errors should be raised for invalid IDs hereDocumentation
Beta Was this translation helpful? Give feedback.
All reactions