level 3 basics
This commit is contained in:
+16
-16
@@ -26,7 +26,7 @@ async def current_user(request: Request) -> User:
|
||||
token = parse_bearer(request.headers.get('Authorization', ''))
|
||||
if token is None:
|
||||
raise unauthorized('Требуется действительная сессия', BEARER_CHALLENGE)
|
||||
user = request.app.state.store.user_for_token(token)
|
||||
user = await request.app.state.store.user_for_token(token)
|
||||
if user is None:
|
||||
raise unauthorized('Требуется действительная сессия', BEARER_CHALLENGE)
|
||||
return user
|
||||
@@ -45,12 +45,12 @@ async def read_body(request: Request, model: Type[Body]) -> Body:
|
||||
raise invalid_request('Тело запроса не соответствует контракту') from None
|
||||
|
||||
|
||||
def _member_chat(store: Store, chat_id: str, actor: User) -> Chat:
|
||||
async def _member_chat(store: Store, chat_id: str, actor: User) -> Chat:
|
||||
try:
|
||||
canonical = str(uuid.UUID(chat_id))
|
||||
except ValueError:
|
||||
raise invalid_request('Некорректный chat_id') from None
|
||||
chat = store.find_chat(canonical, actor.id)
|
||||
chat = await store.find_chat(canonical, actor.id)
|
||||
if chat is None:
|
||||
raise not_found('Чат не найден')
|
||||
return chat
|
||||
@@ -69,15 +69,15 @@ async def liveness() -> JSONResponse:
|
||||
|
||||
|
||||
@router.get('/health/ready')
|
||||
async def readiness() -> JSONResponse:
|
||||
"""In-memory only: the process itself is the single dependency of the sync path."""
|
||||
async def readiness(store: Store = Depends(get_store)) -> JSONResponse:
|
||||
await store.ready()
|
||||
return JSONResponse({'status': 'ok'})
|
||||
|
||||
|
||||
@router.post('/api/v1/users')
|
||||
async def register_user(request: Request, store: Store = Depends(get_store)) -> JSONResponse:
|
||||
payload = await read_body(request, CreateUser)
|
||||
user = store.create_user(payload.username, payload.display_name, payload.password)
|
||||
user = await store.create_user(payload.username, payload.display_name, payload.password)
|
||||
return JSONResponse(user.to_dict(), status_code=201)
|
||||
|
||||
|
||||
@@ -86,10 +86,10 @@ async def create_session(request: Request, store: Store = Depends(get_store)) ->
|
||||
parsed = parse_basic(request.headers.get('Authorization', ''))
|
||||
if parsed is None:
|
||||
raise unauthorized('Неверные учётные данные', BASIC_CHALLENGE)
|
||||
user = store.authenticate(*parsed)
|
||||
user = await store.authenticate(*parsed)
|
||||
if user is None:
|
||||
raise unauthorized('Неверные учётные данные', BASIC_CHALLENGE)
|
||||
token, expires_at = store.create_session(user.id)
|
||||
token, expires_at = await store.create_session(user.id)
|
||||
body = {
|
||||
'token': token,
|
||||
'token_type': 'Bearer',
|
||||
@@ -102,7 +102,7 @@ async def create_session(request: Request, store: Store = Depends(get_store)) ->
|
||||
@router.delete('/api/v1/auth/sessions/current')
|
||||
async def revoke_session(request: Request, actor: User = Depends(current_user)) -> Response:
|
||||
token = parse_bearer(request.headers.get('Authorization', ''))
|
||||
if token is None or not request.app.state.store.revoke_session(token):
|
||||
if token is None or not await request.app.state.store.revoke_session(token):
|
||||
raise unauthorized('Требуется действительная сессия', BEARER_CHALLENGE)
|
||||
return Response(status_code=204)
|
||||
|
||||
@@ -121,7 +121,7 @@ async def create_chat(
|
||||
payload = await read_body(request, CreateChat)
|
||||
if actor.id not in payload.member_ids:
|
||||
raise invalid_request('Создатель чата должен быть указан в member_ids')
|
||||
chat = store.create_chat(payload.title, payload.member_ids)
|
||||
chat = await store.create_chat(payload.title, payload.member_ids)
|
||||
return JSONResponse(chat.to_dict(), status_code=201)
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ async def list_chats(
|
||||
) -> JSONResponse:
|
||||
scope = 'chats:' + actor.id
|
||||
limit, after = parse_page(request.query_params, scope)
|
||||
chats, has_more = store.list_chats(actor.id, limit, after)
|
||||
chats, has_more = await store.list_chats(actor.id, limit, after)
|
||||
return JSONResponse(_page(chats, has_more, scope))
|
||||
|
||||
|
||||
@@ -143,7 +143,7 @@ async def show_chat(
|
||||
actor: User = Depends(current_user),
|
||||
store: Store = Depends(get_store),
|
||||
) -> JSONResponse:
|
||||
return JSONResponse(_member_chat(store, chat_id, actor).to_dict())
|
||||
return JSONResponse((await _member_chat(store, chat_id, actor)).to_dict())
|
||||
|
||||
|
||||
@router.post('/api/v1/chats/{chat_id}/messages')
|
||||
@@ -153,9 +153,9 @@ async def send_message(
|
||||
actor: User = Depends(current_user),
|
||||
store: Store = Depends(get_store),
|
||||
) -> JSONResponse:
|
||||
chat = _member_chat(store, chat_id, actor)
|
||||
chat = await _member_chat(store, chat_id, actor)
|
||||
payload = await read_body(request, CreateMessage)
|
||||
message = store.add_message(chat.id, actor.id, payload.text)
|
||||
message = await store.add_message(chat.id, actor.id, payload.text)
|
||||
return JSONResponse(message.to_dict(), status_code=201)
|
||||
|
||||
|
||||
@@ -166,8 +166,8 @@ async def list_messages(
|
||||
actor: User = Depends(current_user),
|
||||
store: Store = Depends(get_store),
|
||||
) -> JSONResponse:
|
||||
chat = _member_chat(store, chat_id, actor)
|
||||
chat = await _member_chat(store, chat_id, actor)
|
||||
scope = 'messages:' + chat.id
|
||||
limit, after = parse_page(request.query_params, scope)
|
||||
messages, has_more = store.list_messages(chat.id, limit, after)
|
||||
messages, has_more = await store.list_messages(chat.id, limit, after)
|
||||
return JSONResponse(_page(messages, has_more, scope))
|
||||
|
||||
Reference in New Issue
Block a user