Я создаю приложение с помощью Fastapi со следующей структурой папок

Код: Выделить всё
main.py
Код: Выделить всё
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.v1 import lines, upload
from app.core.config import settings
app = FastAPI(
title=settings.PROJECT_NAME,
version=0.1,
openapi_url=f'{settings.API_V1_STR}/openapi.json',
root_path=settings.ROOT_PATH
)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.BACKEND_CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(upload.router, prefix=settings.API_V1_STR)
app.include_router(lines.router, prefix=settings.API_V1_STR)
- --> возвращает случайную строку из файла .txt
Код: Выделить всё
/one-random-line
- --> должен возвращать выходные данные /one-random-line
Код: Выделить всё
/one-random-line-backwards
Коды:
Код: Выделить всё
import random
from fastapi import APIRouter, Request
from starlette.responses import RedirectResponse
router = APIRouter(
prefix="/get-info",
tags=["Get Information"],
responses={
200: {'description': 'Success'},
400: {'description': 'Bad Request'},
403: {'description': 'Forbidden'},
500: {'description': 'Internal Server Error'}
}
)
@router.get('/one-random-line')
def get_one_random_line(request: Request):
lines = open('netflix_list.txt').read().splitlines()
if request.headers.get('accept') in ['application/json', 'application/xml']:
random_line = random.choice(lines)
else:
random_line = 'This is an example'
return {'line': random_line}
@router.get('/one-random-line-backwards')
def get_one_random_line_backwards():
url = router.url_path_for('get_one_random_line')
response = RedirectResponse(url=url)
return {'message': response[::-1]}
Код: Выделить всё
TypeError: 'RedirectResponse' object is not subscriptable

В чем ошибка? занимаюсь?
Пример:
Если конечная точка /one-random-line выводит результат «Maverick», то вывод / one-random-line-backwards должно быть «kcirevam»
Подробнее здесь: https://stackoverflow.com/questions/733 ... fastapi-ap