Utilizing
Other Advanced Features
300
user_details =
req.session["user_details"]
return {'profile' : user_details}
The overridden
form()
method of
ExtractionRequest
is
responsible for the
user_details
attribute containing all the user details.
On the other hand, the given
set_ratings()
method has an incoming
dictionary of various
ratings in which the
json()
override will derive some basic statistics. All the results will be returned
as
Request
’s state objects or request attributes:
@router.post("/rating/top/three")
async def set_ratings(req: Request, data :
Dict[str, float],
user: str = Depends(get_current_user)):
stats = dict()
stats['sum'] =
req.state.sum
stats['average'] =
req.state.avg
return {'stats' : stats }
And lastly, the preceding
compute_data()
service will have an
incoming list of ratings as
a source of some basic statistics like in the previous service. The
body()
method override of
ExtractionRequest
will process the computation:
@router.post("/rating/data/list")
async def compute_data(req: Request,
data: List[float]
,
user: str = Depends(get_current_user)):
stats = dict()
stats['sum'] =
req.state.sum
stats['average'] =
req.state.avg
return {'stats' : stats }
Encrypting and decrypting the message body
Another scenario where we need to customize the routing of the endpoints
is when we must secure
the message body through encryption. The following custom request decrypts an encrypted body
using Python’s
cryptography
module and the key of the encrypted body:
from cryptography.fernet
import Fernet
Chia sẻ với bạn bè của bạn: