Displaying of FastAPI validation errors to end users - python

I'm looking for some library or example of code to format FastAPI validation messages into human-readable format. E.g. this endpoint:
#app.get("/")
async def hello(name: str):
return {"hello": name}
Will produce the next json output if we miss name query parameter:
{
"detail":[
{
"loc":[
"query",
"name"
],
"msg":"field required",
"type":"value_error.missing"
}
]
}
So my questions is, how to:
Transform it into something like "name field is required" (for all kinds of possible errors) to show in toasts.
Use it to display form validation messages
Generate forms themselves from api description if it's possible

FastAPI has a great Exception Handling, so you can customize your exceptions in many ways.
You can raise an HTTPException, HTTPException is a normal Python exception with additional data relevant for APIs. But you can't return it you need to raise it because it's a Python exception
from fastapi import HTTPException
...
#app.get("/")
async def hello(name: str):
if not name:
raise HTTPException(status_code=404, detail="Name field is required")
return {"Hello": name}
By adding name: str as a query parameter it automatically becomes required so you need to add Optional
from typing import Optional
...
#app.get("/")
async def hello(name: Optional[str] = None):
error = {"Error": "Name field is required"}
if name:
return {"Hello": name}
return error
$ curl 127.0.0.1:8000/?name=imbolc
{"Hello":"imbolc"}
...
$ curl 127.0.0.1:8000
{"Error":"Name field is required"}
But in your case, and i think this is the best way to handling errors in FastAPI overriding the validation_exception_handler:
from fastapi import FastAPI, Request, status
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
...
#app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"detail": exc.errors(), "Error": "Name field is missing"}),
)
...
#app.get("/")
async def hello(name: str):
return {"hello": name}
You will get a response like this:
$ curl 127.0.0.1:8000
{
"detail":[
{
"loc":[
"query",
"name"
],
"msg":"field required",
"type":"value_error.missing"
}
],
"Error":"Name field is missing"
}
You can customize your content however if you like:
{
"Error":"Name field is missing",
"Customize":{
"This":"content",
"Also you can":"make it simpler"
}
}

I reached here with a similar question - and I ended up handling the RequestValidationError to give back a response where every field is an array of the issues with that field.
The response to your request would become (with a status_code=400)
{
"detail": "Invalid request",
"errors": {"name": ["field required"]}
}
that's quite handy to manage on the frontend for snackbar notifications and flexible enough.
Here's the handler
from collections import defaultdict
from fastapi import status
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
#app.exception_handler(RequestValidationError)
async def custom_form_validation_error(request, exc):
reformatted_message = defaultdict(list)
for pydantic_error in exc.errors():
loc, msg = pydantic_error["loc"], pydantic_error["msg"]
filtered_loc = loc[1:] if loc[0] in ("body", "query", "path") else loc
field_string = ".".join(filtered_loc) # nested fields with dot-notation
reformatted_message[field_string].append(msg)
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content=jsonable_encoder(
{"detail": "Invalid request", "errors": reformatted_message}
),
)

I think the best I can come up with is actually PlainTextResponse
Adding these:
from fastapi.exceptions import RequestValidationError
#app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
return PlainTextResponse(str(exc), status_code=400)
You get a more human-friendly error message like these in plain text:
1 validation error
path -> item_id
value is not a valid integer (type=type_error.integer)
It's well documented in FastAPI docs here.

Related

How to redirect to dynamic URL inside a FastAPI endpoint?

I'm doing a feature where the user on their profile page makes changes (not related to the user model). Everything is implemented through static HTML templates. I need the user to click on the button and return to the same page (i.e., their profile page) after processing the request.
Html template
<td>Accept</td>
endpoints.py
#router.get('/invite/{pk}/decline')
async def decline_event_invite(
request: Request,
pk: int,
user_id: str = Depends(get_current_user),
service: InviteService = Depends(),
):
await service.invite_decline(pk)
...
--> here I want redirect to user profile page
return RedirectResponse('DYNAMIC URL WITH ARGS')
profile.py
#router.get('/{pk}')
async def user_profile(
request: Request,
pk: int,
service: UserService = Depends()
):
user = await service.get_user_info(pk)
events_invites = await service.get_user_events_invite_list(pk)
return templates.TemplateResponse(
'profile.html',
context=
{
'request': request,
'user': user,
'events_invites': events_invites,
}
)
But I can't find anywhere how to do a redirect similar to the logic that applies to templates. For example:
Sender
You can use url_for() function and pass the (**kwargs) path parameters.
import uvicorn
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from fastapi.responses import RedirectResponse
import urllib
from fastapi import APIRouter
router = APIRouter()
templates = Jinja2Templates(directory="templates")
#router.get('/invite/{pk}/decline')
def decline_event_invite(request: Request, pk: int):
redirect_url = request.url_for('user_profile', **{ 'pk' : pk})
return RedirectResponse(redirect_url)
#router.get('/{pk}')
def user_profile(request: Request, pk: int):
return templates.TemplateResponse("profile.html", {"request": request, "pk": pk})
if __name__ == "__main__":
uvicorn.run(router, host='127.0.0.1', port=8000, debug=True)
To add query params
In case you had to pass query params as well, you could use the following code (make sure to import urllib). Alternatively, you could use the CustomURLProcessor, as described in this and this answer (which pretty much follows the same approach).
If the endpoint expected query params, for example:
#router.get('/invite/{pk}/decline')
def decline_event_invite(request: Request, pk: int, message: str):
pass
you could use:
redirect_url = request.url_for('user_profile', pk=pk)
parsed = list(urllib.parse.urlparse(redirect_url))
parsed[4] = urllib.parse.urlencode({**{ 'message' : "Success!"}})
redirect_url = urllib.parse.urlunparse(parsed)
or even use:
message = 'Success!'
redirect_url = request.url_for('user_profile', pk=pk) + f'?message={message}'
Update
Another solution would be to use Starlette's starlette.datastructures.URL, which now provides a method to include_query_params. Example:
from starlette.datastructures import URL
redirect_url = URL(request.url_for('user_profile', pk=pk)).include_query_params(message="Success!")

Configure Fastapi router

I have declared router in coupe with middleware and added the generic handler for any exception that may happen inside addressing calls to some abstract endpoints:
app = fastapi.FastAPI(openapi_url='/api/v1/openapi.json')
app.include_router(v1.api_router, prefix='/api/v1')
app.add_middleware(middlewares.ClientIPMiddleware)
#app.exception_handler(starlette.exceptions.HTTPException)
async def on_request_exception_handler(
request: fastapi.Request,
exc: starlette.exceptions.HTTPException,
):
return fastapi.responses.JSONResponse(
status_code=exc.status_code,
content={
'detail': exc.detail,
'status': exc.status,
},
)
class ClientIPMiddleware(starlette.middleware.base.BaseHTTPMiddleware):
async def dispatch(self, request: fastapi.Request, call_next: typing.Callable):
request.state.client_ip = get_client_ip(request.headers)
return await call_next(request)
In ClientIPMiddleware class I have to refactor and add something like:
try:
response = await call_next(request)
if request.method != 'GET':
if response.status_code == 200:
return fastapi.responses.JSONResponse(
status_code=200,
content={'status': 'ok'},
)
return response
except Exception as e:
pass
I just need both mechanisms here: the one is for catching all the errors possible at the endpoints' level, the two is for getting either serialized response, or JSONResponse with status_code and status message. In the last snippet of the code try/except block is odd as it can't catch any error there. Is there any better solution to reach the goal? To add a custom or origin decorator to the app instance?

How to create an OpenAPI schema for an UploadFile in FastAPI?

FastAPI automatically generates a schema in the OpenAPI spec for UploadFile parameters.
For example, this code:
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
#app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(..., description="The file")):
return {"filename": file.filename}
will generate this schema under components:schemas in the OpenAPI spec:
{
"Body_create_upload_file_uploadfile__post": {
"title": "Body_create_upload_file_uploadfile__post",
"required":["file"],
"type":"object",
"properties":{
"file": {"title": "File", "type": "string", "description": "The file","format":"binary"}
}
}
}
How can I explicitly specify the schema for UploadFiles (or at least its name)?
I have read FastAPIs docs and searched the issue tracker but found nothing.
I answered this over on FastAPI#1442, but just in case someone else stumbles upon this question here is a copy-and-paste from the post linked above:
After some investigation this is possible, but it requires some monkey patching. Using the example given here, the solution looks like so:
from fastapi import FastAPI, File, UploadFile
from typing import Callable
app = FastAPI()
#app.post("/files/")
async def create_file(file: bytes = File(...)):
return {"file_size": len(file)}
#app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.filename}
def update_schema_name(app: FastAPI, function: Callable, name: str) -> None:
"""
Updates the Pydantic schema name for a FastAPI function that takes
in a fastapi.UploadFile = File(...) or bytes = File(...).
This is a known issue that was reported on FastAPI#1442 in which
the schema for file upload routes were auto-generated with no
customization options. This renames the auto-generated schema to
something more useful and clear.
Args:
app: The FastAPI application to modify.
function: The function object to modify.
name: The new name of the schema.
"""
for route in app.routes:
if route.endpoint is function:
route.body_field.type_.__name__ = name
break
update_schema_name(app, create_file, "CreateFileSchema")
update_schema_name(app, create_upload_file, "CreateUploadSchema")
You can edit the OpenAPI schema itself. I prefer to just move these schemas to the path (since they are unique to each path anyway):
from fastapi import FastAPI, File, UploadFile
from fastapi.openapi.utils import get_openapi
app = FastAPI()
#app.post("/uploadfile/")
async def create_upload_file(file1: UploadFile = File(...), file2: UploadFile = File(...)):
pass
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Custom title",
version="2.5.0",
description="This is a very custom OpenAPI schema",
routes=app.routes,
)
# Move autogenerated Body_ schemas, see https://github.com/tiangolo/fastapi/issues/1442
for path in openapi_schema["paths"].values():
for method_data in path.values():
if "requestBody" in method_data:
for content_type, content in method_data["requestBody"]["content"].items():
if content_type == "multipart/form-data":
schema_name = content["schema"]["$ref"].lstrip("#/components/schemas/")
schema_data = openapi_schema["components"]["schemas"].pop(schema_name)
content["schema"] = schema_data
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi

how overwrite Response class in django rest framework ( DRF )?

I want to overwrite Response class of django rest framework so that response back responsive dictionary contain three parameter message, status and data
Hello dears all
I try to change Response Class in DRF to pass two extra parameter ( message, status ) plus data provide by DRF serializer. message pass the message like Done, User Created or etc and status pass the message like fail or success or etc message and this message useful for reserve special code between frontend and backend.
I want to if don't set this parameter return empty character or null result back to client side
for example in success mode:
{
'data': {
'value_one': 'some data',
'value_two': 'some data',
'value_three': [
'value', 'value', 'value'
],
},
}
'message': 'Done',
'status': 'success',
}
and in failure mode:
{
'data': ['any error message raise by serializer',]
'message': 'Create User Failed',
'status': 'failure',
}
I search about my question and found this solution:
if i inheritance DRF Response Class in my class and overwrite __init__ method and get message, data and status in this method and call init of parent with own data structure and use this responsive class in my functionality like this implement:
from rest_framework.response import Response
class Response(Response):
def __init__(self, data=None, message=None, data_status=None, status=None,
template_name=None, headers=None,
exception=False, content_type=None):
data_content = {
'status': data_status,
'data': data,
'message': message,
}
super(Response, self).__init__(
data=data_content,
status=status,
template_name=template_name,
headers=headers,
exception=exception,
content_type=content_type
)
in success mode call:
return Response(data=serializer.data, message='Done', data_status='success', status=200)
in failure mode call:
return Response(data=serializer.errors, message='Create User Failed', data_status='failure', status=400)
and use own Response class in all views class
we had problem in this solution: if we use GenericViews Class must be overwrite all http methods we used in view's logic and call own class and this is DRY!!
and other solution i found. in serialized layer, we have abstract method def to_representation(self, instance): in Serializer class and implement in other class like ModelSerializer class inheritance Serializer and if we overwrite this method in our serializer class and re fetch data before send to view layer, implement like:
from collections import OrderedDict
class OurSerializer(serializer.ModelSerializer):
....
def to_representation(self, instance):
data = super(serializers.ModelSerializer, self).to_representation(instance)
result = OrderedDict()
result['data'] = data
result['message'] = 'Done'
result['status'] = 'sucssed'
return result
this solution solve above problem but we have two problem again
one: if we use nested serializer and we had overwrite this function in serializer class return bad data like:
{
'data': {
'value_one': 'some data',
'value_two': 'some data',
'value_three': {
'data': [
'value', 'value', 'value'
],
'message': 'Done',
'status': 'sucssed',
},
}
'message': 'Done',
'status': 'sucssed',
}
and message and status repeated and structure not pretty for client
and two: we cant handle exception in this mode and just way to handle exception just with middleware class like this DRF Exception Handling and this isn't useful way, we can't handle any type of error occurs in view and generate comfortable separate message and status.
IF there's another good solution to this question, please guide me.
thanks :)
To resolve this, best practice (that DRF has proposed) is to use 'renderer' classes. A renderer manipulates and returns structured response.
Django uses renderers like Template Renderer and DRF benefits this feature and provides API Renderers.
To do so, you could provide such this renderer in a package (e.g. app_name.renderers.ApiRenderer):
from rest_framework.renderers import BaseRenderer
from rest_framework.utils import json
class ApiRenderer(BaseRenderer):
def render(self, data, accepted_media_type=None, renderer_context=None):
response_dict = {
'status': 'failure',
'data': {},
'message': '',
}
if data.get('data'):
response_dict['data'] = data.get('data')
if data.get('status'):
response_dict['status'] = data.get('status')
if data.get('message'):
response_dict['message'] = data.get('message')
data = response_dict
return json.dumps(data)
And then in your settings file:
REST_FRAMEWORK = {
...
'DEFAULT_RENDERER_CLASSES': (
'app_name.renderers.ApiRenderer',
),
...
}
By this action all views that extend DRF generic views will use renderer. If you needed to override setting you can use renderer_classes attribute for generic view classes and #renderer_classes decorator for api view functions.
A comprehensive renderer class to override is available at <virtualenv_dir>/lib/python3.6/site-packages/rest_framework/renderers.py.
This would be more a more robust solution, as it can be used with Generic Views hassle free.
In case of Generic views, the data argument we receive in the render() method is sent automatically by the generic view itself (if not overriding the methods, which will be against DRY), so we cannot handle it, as it does in the accepted answer.
Also, the checks in render() can be easily altered as per the needs (Eg., handling no-2XX status codes in this solution).
from rest_framework.renderers import JSONRenderer
class CustomRenderer(JSONRenderer):
def render(self, data, accepted_media_type=None, renderer_context=None):
status_code = renderer_context['response'].status_code
response = {
"status": "success",
"code": status_code,
"data": data,
"message": None
}
if not str(status_code).startswith('2'):
response["status"] = "error"
response["data"] = None
try:
response["message"] = data["detail"]
except KeyError:
response["data"] = data
return super(CustomRenderer, self).render(response, accepted_media_type, renderer_context)
Just an addition: I prefer to inherit from JSONRenderer. That way you get the nice formatting and indentation out of the box
from rest_framework.renderers import JSONRenderer
class CustomRenderer(JSONRenderer):
def render(self, data, accepted_media_type=None, renderer_context=None):
response = {
'error': False,
'message': 'Success',
'data': data
}
return super(CustomRenderer, self).render(response, accepted_media_type, renderer_context)
Then in your views:
from rest_framework.renderers import BrowsableAPIRenderer
from api.renderers import CustomRenderer
class MyViewSet(viewsets.ModelViewSet):
renderer_classes = [CustomRenderer, BrowsableAPIRenderer]
...
When used with the BrowsableAPIRenderer as shown above, you get your nicely formatted custom response rendered in DRF's Browsable API
Did you try to write custom Response middleware:
class ResponseCustomMiddleware(MiddlewareMixin):
def __init__(self, *args, **kwargs):
super(ResponseCustomMiddleware, self).__init__(*args, **kwargs)
def process_template_response(self, request, response):
if not response.is_rendered and isinstance(response, Response):
if isinstance(response.data, dict):
message = response.data.get('message', 'Some error occurred')
if 'data' not in response.data:
response.data = {'data': response.data}
response.data.setdefault('message', message)
# you can add you logic for checking in status code is 2** or 4**.
data_status = 'unknown'
if response.status_code // 100 == 2:
data_status = 'success'
elif response.status_code // 100 == 4:
data_status = 'failure'
response.data.setdefault('data_status', data_status)
return response
Add middleware in settings:
MIDDLEWARE = [
# you all middleware here,
'common.middleware.ResponseCustomMiddleware',
]
So you can return Response like this:
data = {'var1': 1, 'var2': 2}
return Response({'data': data, 'message': 'This is my message'}, status=status.HTTP_201_CREATED)
Response will be like:
{
"data": [
{
"var1": 1,
"var2": 2
}
],
"message": "This is my message",
"data_status": "success"
}
This is how I solve the problem. I hope it helps
def custom_response(data, code=None, message=None):
if not code and not message:
code = SUCCESSFUL_CODE
message = SUCCESSFUL_MESSAGE
return Response(OrderedDict([
('code', code),
('message', message),
('results', data)
]))
Now in your views function. You can custom the response however you want pretty easy return custom_response(data=..,message=...,code=...)

Postman: Could not get any response

My Django Rest API views.py:
# encoding: utf-8
from fileupload.response import JSONResponse, response_mimetype
from fileupload.models import *
from rest_framework.views import *
from rest_framework.parsers import *
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
"""A simple API for file upload."""
class FileUploadView(APIView):
authentication_classes = ()
#method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(FileUploadView, self).dispatch(request, *args, **kwargs)
def put(self, request):
print "xxx:", request
try:
psfile = MyFile.objects.create(file=request.FILES['file'])
psfile.save()
data = {'files': 'testing'}
response = Response(data)
except Exception as e:
print "Exception when put file:", e
data = { 'error' : str(e)}
response = Response(data)
return response
setting.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': ()
}
I am doing PUT in Postman with Postman Interceptor enabled.
Tried to put the file in the body as form-data.
But failed with the following error:
Could not get any response
There was an error connecting to https://ip/projectname/api/upload/.
Why this might have happened:
1. The server couldn't send a response:
Ensure that the backend is working properly
2. SSL connections are being blocked:
Fix this by importing SSL certificates in Chrome
3. Cookies not being sent: Use the Postman Interceptor extension
4. Request timeout:
Change request timeout in Settings > General
Any ideas? Thanks in advance.
UPDATE
https://github.com/postmanlabs/postman-app-support/issues/506
http://blog.getpostman.com/2014/01/28/using-self-signed-certificates-with-postman/

Categories

Resources