Birmingham-mumbai


@router.get("/restaurant/index")



tải về 6.62 Mb.
Chế độ xem pdf
trang273/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.get("/restaurant/index")
def intro_list_restaurants():
return 
PlainTextResponse(content="The Restaurants")
Services can use 
RedirectResponse
if they need to pursue navigation to another entirely 
different application or another endpoint of the same application. The following endpoint jumps 
to a hypertext reference about some known Michelin-starred restaurants:
@router.get("/restaurant/michelin")
def redirect_restaurants_rates():
return 
RedirectResponse(
url="https://guide.michelin.com/en/restaurants")
• A 
FileResponse
type can help services render some content of a file, preferably text-based 
files. The following 
load_questions()
service shows the list of questions saved in the 
questions.txt
file placed inside the 
/file
folder of the application:
@router.get("/question/load/questions")
async def load_questions(user: str = 
Depends(get_current_user)):
file_path = os.getcwd() + 
'\\files\\questions.txt';
return 
FileResponse(path=file_path,
media_type="text/plain")
• 
StreamingResponse
is another response type that can provide us with another approach to 
the 
Server-Sent Events
(
SSE
) implementation. Chapter 8, Creating Coroutines, Events, and Message-
Driven Transactions, has provided us with an SSE that utilizes the 
EventSourceResponse
type:
@router.get("/question/sse/list")
async def list_questions(req:Request, 
engine=Depends(create_db_engine), 


Choosing the appropriate responses
303
user: str = Depends(get_current_user)):
async def print_questions():
repo:QuestionRepository = 
QuestionRepository(engine)
result = await repo.get_all_question()
for q in result:
disconnected = await req.is_disconnected()
if disconnected:
break

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