Birmingham-mumbai


@router.post("/user/profile")



tải về 6.62 Mb.
Chế độ xem pdf
trang271/275
Chuyển đổi dữ liệu09.10.2023
Kích6.62 Mb.
#55278
1   ...   267   268   269   270   271   272   273   274   275
Sherwin John Tragura - Building Python Microservices with FastAPI Build secure, scalable, and structured Python microservices from design concepts to infrastructure-Packt Publishing - ebooks Account

@router.post("/user/profile")
async def create_profile(req: Request, 
firstname: str = Form(...), 
lastname: str = Form(...), age: int = Form(...), 
birthday: date = Form(...), 
user: str = Depends(get_current_user)):


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

tải về 6.62 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   267   268   269   270   271   272   273   274   275




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương