Birmingham-mumbai


part of a URL indicated by curly braces (



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


part of a URL indicated by curly braces (
{}
). After setting off these path parameters within the URL, 
FastAPI requires these parameters to be declared by applying type hints
The following 
delete_user()
service is a 
DELETE
API method that uses a 
username
path 
parameter to search for a login record for deletion:
@app.delete("/ch01/login/remove/{
username
}")
def delete_user(
username: str
):
if username == None:
return {"message": "invalid user"}
else:
del valid_users[username]
return {"message": "deleted user"}


Managing user requests and server response
11
Multiple path parameters are acceptable if the leftmost variables are more likely to be filled with values 
than the rightmost variables. In other words, the importance of the leftmost path variables will make 
the process more relevant and correct than those on the right. This standard is applied to ensure that 
the endpoint URL will not look like other URLs, which might cause some conflicts and confusion. 
The following 
login_with_token()
service follows this standard, since 
username
is a primary 
key and is as strong as, or even stronger than, its next parameter, 
password
. There is an assurance 
that the URL will always look unique every time the endpoint is accessed because 
username
will 
always be required, as well as 
password
:
@app.get("/ch01/login/
{username}
/
{password}
")
def login_with_token(
username: str

password:str

id: UUID):
if valid_users.get(username) == None:
return {"message": "user does not exist"}
else:
user = valid_users[username]
if user.id == id and checkpw(password.encode(), 
user.passphrase):
return user
else:
return {"message": "invalid user"}
Unlike other web frameworks, FastAPI is not friendly with endpoint URLs that belong to base paths 
or top-level domain paths with different subdirectories. This occurrence happens when we have 
dynamic URL patterns that look the same as the other fixed endpoint URLs when assigned a specific 
path variable. These fixed URLs are implemented sequentially after these dynamic URLs. An example 
of these are the following services:

tải về 6.62 Mb.

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