Birmingham-mumbai


@app.patch("/ch01/account/profile/update/names/{username}")



tải về 6.62 Mb.
Chế độ xem pdf
trang15/275
Chuyển đổi dữ liệu09.10.2023
Kích6.62 Mb.
#55278
1   ...   11   12   13   14   15   16   17   18   ...   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

@app.patch("/ch01/account/profile/update/names/{username}")
def 
update_profile_names
(username: str, id: UUID, 
new_names: Dict[str, str]
):
if valid_users.get(username) == None:
return {"message": "user does not exist"}
elif new_names == None:
return {"message": "new names are required"}
else:
user = valid_users.get(username)
if user.id == id:
profile = valid_profiles[username]
profile.firstname = new_names['fname']
profile.lastname = new_names['lname']
profile.middle_initial = new_names['mi']
valid_profiles[username] = profile
return {"message": "successfully updated"}
else:
return {"message": "user does not exist"}
The last essential HTTP services that we included before building the application are the 
DELETE
API 
methods. We use these services to delete records or information given a unique identification, such as 
username
and a hashed 
id
. An example is the following 
delete_post_discussion()
service 
that allows a user to delete a posted discussion when given a username and the UUID (Universally 
Unique Identifier) of the posted message:
@app.delete("/ch01/discussion/posts/remove/{username}")
def 
delete_discussion
(username: str, id: UUID):
if valid_users.get(username) == None:
return {"message": "user does not exist"}
elif discussion_posts.get(id) == None:


Designing and implementing REST APIs
9
return {"message": "post does not exist"}
else:
del discussion_posts[id] 
return {"message": "main post deleted"}
All path operations require a unique endpoint URL in the 
str
format. A good practice is to start all 
URLs with the same top-level base path, such as 
/ch01
, and then differ when reaching their respective 
subdirectories. After running the uvicorn server, we can check and validate whether all our URLs 
are valid and running by accessing the documentation URL, 
http://localhost:8000/docs

This path will show us a 
OpenAPI
dashboard, as shown in Figure 1.2, listing all the API methods 
created for the application. Discussions on the OpenAPI will be covered in Chapter 9, Utilizing Other 
Advanced Features.
Figure 1.2 – A Swagger OpenAPI dashboard
After creating the endpoint services, let us scrutinize how FastAPI manages its incoming request 
body and the outgoing response.


Setting Up FastAPI for Starters
10

tải về 6.62 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   11   12   13   14   15   16   17   18   ...   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