From 34aba03f61efd0b35f9d0ee15f1206daa265676d Mon Sep 17 00:00:00 2001 From: Vladimir Kibisov Date: Tue, 27 Feb 2024 21:52:58 +0300 Subject: [PATCH] ruff config --- pyproject.toml | 28 +++++++++++++++++++ .../adapters/database/ram_chat_db.py | 14 +++++----- .../adapters/database/ram_message_db.py | 13 +++------ .../adapters/database/ram_session_db.py | 4 +-- .../adapters/database/ram_user_db.py | 14 +++++----- .../events/websocket_event_gateway.py | 17 +++++------ .../adapters/security/password_provider.py | 3 +- .../chat/create_chat_with_random_user.py | 2 +- .../application/common/chat_gateway.py | 2 +- src/just_chat/application/common/event_bus.py | 4 +-- .../application/common/event_gateway.py | 2 +- .../application/common/message_gateway.py | 2 +- .../application/common/password_provider.py | 4 +-- .../application/common/session_gateway.py | 3 +- .../application/common/user_gateway.py | 4 +-- .../application/message/create_message.py | 4 +-- .../application/message/get_chat_messages.py | 2 +- src/just_chat/application/user/create_user.py | 2 +- .../application/user/get_user_by_id.py | 2 +- .../application/user/get_user_by_token.py | 3 +- src/just_chat/application/user/login.py | 10 +++---- src/just_chat/domain/exceptions/__init__.py | 6 ++-- src/just_chat/domain/exceptions/access.py | 4 +-- src/just_chat/domain/models/message.py | 2 +- src/just_chat/domain/services/chat.py | 6 ++-- src/just_chat/domain/services/chat_access.py | 8 +++--- src/just_chat/domain/services/message.py | 6 ++-- src/just_chat/domain/services/notification.py | 4 +-- src/just_chat/domain/services/user.py | 2 +- src/just_chat/main/ioc.py | 12 ++++---- src/just_chat/main/telegram_bot.py | 4 +-- src/just_chat/main/web.py | 5 ++-- .../presentation/interactor_factory/chat.py | 13 +++++---- .../presentation/interactor_factory/event.py | 8 +++--- .../interactor_factory/message.py | 6 ++-- .../presentation/interactor_factory/user.py | 10 +++---- .../presentation/telegram_bot/__init__.py | 11 ++++---- .../presentation/telegram_bot/login.py | 6 ++-- .../presentation/telegram_bot/states.py | 2 +- .../presentation/web_api/__init__.py | 2 +- .../presentation/web_api/admin/__init__.py | 2 +- .../web_api/admin/chat/__init__.py | 10 ++++--- .../web_api/admin/chat/create_chat.py | 2 +- .../web_api/admin/chat/delete_chat.py | 10 +++---- .../web_api/admin/chat/get_chat.py | 10 +++---- .../web_api/admin/user/__init__.py | 4 +-- .../web_api/admin/user/get_user.py | 6 ++-- .../web_api/dependencies/id_provider.py | 7 +++-- .../presentation/web_api/dependencies/stub.py | 7 +++-- .../presentation/web_api/dependencies/user.py | 2 +- .../presentation/web_api/public/__init__.py | 6 ++-- .../web_api/public/chat/__init__.py | 4 +-- .../chat/create_chat_with_random_user.py | 4 +-- .../web_api/public/event/__init__.py | 4 +-- .../web_api/public/event/listen.py | 12 ++++---- .../web_api/public/message/__init__.py | 6 ++-- .../public/message/get_chat_messages.py | 10 +++---- .../web_api/public/message/send_message.py | 10 +++---- .../web_api/public/user/__init__.py | 4 +-- .../web_api/public/user/create_user.py | 8 +++--- .../presentation/web_api/public/user/login.py | 12 ++++---- .../web_api/public/user/router.py | 1 - tests/application/test_chat.py | 6 ++-- tests/application/test_user.py | 4 +-- 64 files changed, 217 insertions(+), 190 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bbc85e8..a034922 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,34 @@ disallow_untyped_defs = true [tool.ruff] line-length = 120 +target-version="py311" +src = ["src"] +include = ["src/**.py", "tests/**.py"] +lint.select = [ + "ALL" +] +lint.ignore = [ + "ARG", + "ANN", + "D", + "EM101", + "EM102", + "PT001", + "PT023", + "SIM108", + "SIM114", + "TRY003", + "PLW2901", + "RET505", + "PLR0913", + "S101", +] + +[tool.ruff.lint.per-file-ignores] +"tests/**" = ["TID252", "PLR2004", "S101"] + +[tool.ruff.lint.isort] +no-lines-before = ["local-folder"] [project] name = "just_chat" diff --git a/src/just_chat/adapters/database/ram_chat_db.py b/src/just_chat/adapters/database/ram_chat_db.py index 2c986ee..23fbf27 100644 --- a/src/just_chat/adapters/database/ram_chat_db.py +++ b/src/just_chat/adapters/database/ram_chat_db.py @@ -2,9 +2,9 @@ from dataclasses import asdict from just_chat.adapters.database.ram_user_db import RAM_USERS_DB -from just_chat.application.common.chat_gateway import ChatGateway, ChatNotFound -from just_chat.application.common.user_gateway import UserNotFound -from just_chat.domain.models.chat import ChatId, Chat +from just_chat.application.common.chat_gateway import ChatGateway, ChatNotFoundError +from just_chat.application.common.user_gateway import UserNotFoundError +from just_chat.domain.models.chat import Chat, ChatId from just_chat.domain.models.user import UserId RAM_CHATS_DB: list[Chat] = [] @@ -17,7 +17,7 @@ class RAMChatGateway(ChatGateway): async def save_chat(self, chat: Chat) -> Chat: async with self.next_chat_id_lock: chat_in_db = Chat( - **asdict(chat) | {"id": self.next_chat_id} + **asdict(chat) | {"id": self.next_chat_id}, ) type(self).next_chat_id += 1 @@ -33,7 +33,7 @@ async def create_chat_with_random_user(self, title: str, user_id: UserId) -> Cha for user in RAM_USERS_DB: assert user.id is not None - + if user.id in excluded_users_ids: continue @@ -48,14 +48,14 @@ async def create_chat_with_random_user(self, title: str, user_id: UserId) -> Cha return chat - raise UserNotFound + raise UserNotFoundError async def get_chat_by_id(self, id_: ChatId) -> Chat: for chat in RAM_CHATS_DB: if chat.id == id_: return chat - raise ChatNotFound(f"Chat with id {id_} not found") + raise ChatNotFoundError(f"Chat with id {id_} not found") async def delete_chat_by_id(self, id_: ChatId) -> Chat: chat = await self.get_chat_by_id(id_) diff --git a/src/just_chat/adapters/database/ram_message_db.py b/src/just_chat/adapters/database/ram_message_db.py index c58c531..0cc6006 100644 --- a/src/just_chat/adapters/database/ram_message_db.py +++ b/src/just_chat/adapters/database/ram_message_db.py @@ -1,7 +1,7 @@ import asyncio from dataclasses import asdict -from just_chat.application.common.message_gateway import MessageGateway, MessageNotFound +from just_chat.application.common.message_gateway import MessageGateway, MessageNotFoundError from just_chat.domain.models.chat import ChatId from just_chat.domain.models.message import Message, MessageId @@ -15,7 +15,7 @@ class RAMMessageGateway(MessageGateway): async def save_message(self, message: Message) -> Message: async with self.next_message_id_lock: message_in_db = Message( - **asdict(message) | {"id": self.next_message_id} + **asdict(message) | {"id": self.next_message_id}, ) type(self).next_message_id += 1 @@ -28,12 +28,7 @@ async def get_message_by_id(self, id_: MessageId) -> Message: if message.id == id_: return message - raise MessageNotFound(f"Message with id {id_} not found") + raise MessageNotFoundError(f"Message with id {id_} not found") async def get_chat_messages_by_chat_id(self, chat_id: ChatId) -> list[Message]: - messages = [] - for message in RAM_MESSAGES_DB: - if message.chat_id == chat_id: - messages.append(message) - - return messages + return [message for message in RAM_MESSAGES_DB if message.chat_id == chat_id] diff --git a/src/just_chat/adapters/database/ram_session_db.py b/src/just_chat/adapters/database/ram_session_db.py index 22a664b..445649c 100644 --- a/src/just_chat/adapters/database/ram_session_db.py +++ b/src/just_chat/adapters/database/ram_session_db.py @@ -1,4 +1,4 @@ -from just_chat.application.common.session_gateway import SessionGateway, SessionToken, SessionNotFound +from just_chat.application.common.session_gateway import SessionGateway, SessionNotFoundError, SessionToken from just_chat.domain.models.user import UserId RAM_SESSION_DB: dict[SessionToken, UserId] = {} @@ -10,7 +10,7 @@ async def get_user_id(self, token: SessionToken) -> UserId: if token in RAM_SESSION_DB: return RAM_SESSION_DB[token] - raise SessionNotFound + raise SessionNotFoundError async def save_session_token(self, user_id: UserId, token: SessionToken) -> None: RAM_SESSION_DB[token] = user_id diff --git a/src/just_chat/adapters/database/ram_user_db.py b/src/just_chat/adapters/database/ram_user_db.py index 40bbda7..b3e22f2 100644 --- a/src/just_chat/adapters/database/ram_user_db.py +++ b/src/just_chat/adapters/database/ram_user_db.py @@ -1,9 +1,9 @@ import asyncio +from collections.abc import Container from dataclasses import asdict -from typing import Container -from just_chat.application.common.user_gateway import UserGateway, UserNotFound -from just_chat.domain.models.user import UserId, User +from just_chat.application.common.user_gateway import UserGateway, UserNotFoundError +from just_chat.domain.models.user import User, UserId RAM_USERS_DB: list[User] = [] @@ -15,7 +15,7 @@ class RAMUserGateway(UserGateway): async def save_user(self, user: User) -> User: async with self.next_user_id_lock: user_in_db = User( - **asdict(user) | {"id": self.next_user_id} + **asdict(user) | {"id": self.next_user_id}, ) type(self).next_user_id += 1 @@ -28,21 +28,21 @@ async def get_user_by_id(self, id_: UserId) -> User: if user.id == id_: return user - raise UserNotFound(f"User with id {id_} not found") + raise UserNotFoundError(f"User with id {id_} not found") async def get_user_by_username(self, username: str) -> User: for user in RAM_USERS_DB: if user.username == username: return user - raise UserNotFound(f"User with username {username} not found") + raise UserNotFoundError(f"User with username {username} not found") async def get_random_user(self, exclude: Container[UserId]) -> User: for user in RAM_USERS_DB: if user.id not in exclude: return user - raise UserNotFound("User not found") + raise UserNotFoundError("User not found") async def delete_user_by_id(self, id_: UserId) -> User: user = await self.get_user_by_id(id_) diff --git a/src/just_chat/adapters/events/websocket_event_gateway.py b/src/just_chat/adapters/events/websocket_event_gateway.py index 8c7fa2a..c8956b6 100644 --- a/src/just_chat/adapters/events/websocket_event_gateway.py +++ b/src/just_chat/adapters/events/websocket_event_gateway.py @@ -1,31 +1,28 @@ +from collections.abc import Sequence +from contextlib import suppress from dataclasses import asdict -from typing import Sequence - -from just_chat.application.common.event_bus import EventBus, ConnectionClosed +from just_chat.application.common.event_bus import ConnectionClosedError, EventBus from just_chat.application.common.event_gateway import EventGateway from just_chat.domain.models.event import NewMessage from just_chat.domain.models.user import UserId - EVENT_BUS_DB: dict[UserId, EventBus] = {} class WSEventGateway(EventGateway): async def send_new_message_event(self, event: NewMessage, target_user_ids: Sequence[UserId]) -> None: for user_id in target_user_ids: - bus = EVENT_BUS_DB.get(user_id, None) + bus = EVENT_BUS_DB.get(user_id) if bus is None: continue - try: + with suppress(ConnectionClosedError): await bus.send_json({ "event": "new_message", - "message": asdict(event.message) - }) - except ConnectionClosed: - pass + "message": asdict(event.message), + }) async def add_user_event_bus(self, event_bus: EventBus, user_id: UserId) -> None: EVENT_BUS_DB[user_id] = event_bus diff --git a/src/just_chat/adapters/security/password_provider.py b/src/just_chat/adapters/security/password_provider.py index 0dfd894..70401d1 100644 --- a/src/just_chat/adapters/security/password_provider.py +++ b/src/just_chat/adapters/security/password_provider.py @@ -1,4 +1,3 @@ -from typing import Type from passlib.ifc import PasswordHash @@ -6,7 +5,7 @@ class HashingPasswordProvider(PasswordProvider): - def __init__(self, hasher: PasswordHash | Type[PasswordHash]) -> None: + def __init__(self, hasher: PasswordHash | type[PasswordHash]) -> None: self._hasher = hasher def hash_password(self, password: str) -> str: diff --git a/src/just_chat/application/chat/create_chat_with_random_user.py b/src/just_chat/application/chat/create_chat_with_random_user.py index b83c834..e614696 100644 --- a/src/just_chat/application/chat/create_chat_with_random_user.py +++ b/src/just_chat/application/chat/create_chat_with_random_user.py @@ -31,7 +31,7 @@ async def __call__(self, data: NewChatDTO) -> Chat: chat = await self._chat_gateway.create_chat_with_random_user( title=data.title, - user_id=user_id + user_id=user_id, ) assert chat.id is not None diff --git a/src/just_chat/application/common/chat_gateway.py b/src/just_chat/application/common/chat_gateway.py index 56c5428..0ec0c62 100644 --- a/src/just_chat/application/common/chat_gateway.py +++ b/src/just_chat/application/common/chat_gateway.py @@ -4,7 +4,7 @@ from just_chat.domain.models.user import UserId -class ChatNotFound(ValueError): +class ChatNotFoundError(ValueError): pass diff --git a/src/just_chat/application/common/event_bus.py b/src/just_chat/application/common/event_bus.py index ed9d540..a1ce8bd 100644 --- a/src/just_chat/application/common/event_bus.py +++ b/src/just_chat/application/common/event_bus.py @@ -1,8 +1,8 @@ -from abc import abstractmethod, ABC +from abc import ABC, abstractmethod from typing import Any -class ConnectionClosed(Exception): +class ConnectionClosedError(Exception): pass diff --git a/src/just_chat/application/common/event_gateway.py b/src/just_chat/application/common/event_gateway.py index 88c4154..28a17a8 100644 --- a/src/just_chat/application/common/event_gateway.py +++ b/src/just_chat/application/common/event_gateway.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import Sequence +from collections.abc import Sequence from just_chat.application.common.event_bus import EventBus from just_chat.domain.models.event import NewMessage diff --git a/src/just_chat/application/common/message_gateway.py b/src/just_chat/application/common/message_gateway.py index 096dc88..ec4df7a 100644 --- a/src/just_chat/application/common/message_gateway.py +++ b/src/just_chat/application/common/message_gateway.py @@ -4,7 +4,7 @@ from just_chat.domain.models.message import Message, MessageId -class MessageNotFound(ValueError): +class MessageNotFoundError(ValueError): pass diff --git a/src/just_chat/application/common/password_provider.py b/src/just_chat/application/common/password_provider.py index 3b2fc7b..4bf91cb 100644 --- a/src/just_chat/application/common/password_provider.py +++ b/src/just_chat/application/common/password_provider.py @@ -1,7 +1,7 @@ -from abc import abstractmethod, ABC +from abc import ABC, abstractmethod -class PasswordProvider(ABC): # TODO: mb better naming? +class PasswordProvider(ABC): @abstractmethod def hash_password(self, password: str) -> str: raise NotImplementedError diff --git a/src/just_chat/application/common/session_gateway.py b/src/just_chat/application/common/session_gateway.py index 468d915..aad1524 100644 --- a/src/just_chat/application/common/session_gateway.py +++ b/src/just_chat/application/common/session_gateway.py @@ -3,11 +3,10 @@ from just_chat.domain.models.user import UserId - SessionToken = NewType("SessionToken", str) -class SessionNotFound(ValueError): +class SessionNotFoundError(ValueError): pass diff --git a/src/just_chat/application/common/user_gateway.py b/src/just_chat/application/common/user_gateway.py index 5ac8cf3..a07ab88 100644 --- a/src/just_chat/application/common/user_gateway.py +++ b/src/just_chat/application/common/user_gateway.py @@ -1,10 +1,10 @@ from abc import ABC, abstractmethod -from typing import Container +from collections.abc import Container from just_chat.domain.models.user import User, UserId -class UserNotFound(ValueError): +class UserNotFoundError(ValueError): pass diff --git a/src/just_chat/application/message/create_message.py b/src/just_chat/application/message/create_message.py index 962b517..4ebeeb5 100644 --- a/src/just_chat/application/message/create_message.py +++ b/src/just_chat/application/message/create_message.py @@ -25,7 +25,7 @@ def __init__( event_service: EventService, event_gateway: EventGateway, message_gateway: MessageGateway, - id_provider: IdProvider + id_provider: IdProvider, ) -> None: self._chat_access_service = chat_access_service self._chat_gateway = chat_gateway @@ -47,7 +47,7 @@ async def __call__(self, data: NewMessageDTO) -> Message: chat_id=data.chat_id, author_id=user_id, owner_id=user_id, - ) + ), ) assert message.id is not None diff --git a/src/just_chat/application/message/get_chat_messages.py b/src/just_chat/application/message/get_chat_messages.py index 0143a27..030b9bb 100644 --- a/src/just_chat/application/message/get_chat_messages.py +++ b/src/just_chat/application/message/get_chat_messages.py @@ -14,7 +14,7 @@ def __init__( chat_access_service: ChatAccessService, message_gateway: MessageGateway, chat_gateway: ChatGateway, - id_provider: IdProvider + id_provider: IdProvider, ) -> None: self._chat_access_service = chat_access_service self._message_gateway = message_gateway diff --git a/src/just_chat/application/user/create_user.py b/src/just_chat/application/user/create_user.py index 515e836..ff1f76e 100644 --- a/src/just_chat/application/user/create_user.py +++ b/src/just_chat/application/user/create_user.py @@ -35,5 +35,5 @@ async def __call__(self, data: NewUserDTO) -> CreatedUserDTO: return CreatedUserDTO( id=user.id, - username=user.username + username=user.username, ) diff --git a/src/just_chat/application/user/get_user_by_id.py b/src/just_chat/application/user/get_user_by_id.py index 99f27ba..3c20505 100644 --- a/src/just_chat/application/user/get_user_by_id.py +++ b/src/just_chat/application/user/get_user_by_id.py @@ -24,5 +24,5 @@ async def __call__(self, data: UserId) -> UserDTO: return UserDTO( id=user.id, - username=user.username + username=user.username, ) diff --git a/src/just_chat/application/user/get_user_by_token.py b/src/just_chat/application/user/get_user_by_token.py index ea8d150..3d919df 100644 --- a/src/just_chat/application/user/get_user_by_token.py +++ b/src/just_chat/application/user/get_user_by_token.py @@ -8,5 +8,4 @@ def __init__(self, session_gateway: SessionGateway) -> None: self._session_gateway = session_gateway async def __call__(self, data: SessionToken) -> UserId: - user_id = await self._session_gateway.get_user_id(token=data) - return user_id + return await self._session_gateway.get_user_id(token=data) diff --git a/src/just_chat/application/user/login.py b/src/just_chat/application/user/login.py index 62d621b..88f2496 100644 --- a/src/just_chat/application/user/login.py +++ b/src/just_chat/application/user/login.py @@ -4,11 +4,11 @@ from just_chat.application.common.interactor import Interactor from just_chat.application.common.password_provider import PasswordProvider from just_chat.application.common.session_gateway import SessionGateway, SessionToken -from just_chat.application.common.user_gateway import UserGateway, UserNotFound +from just_chat.application.common.user_gateway import UserGateway, UserNotFoundError from just_chat.domain.services.user import UserService -class WrongCredentials(Exception): +class WrongCredentialsError(Exception): pass @@ -34,13 +34,13 @@ def __init__( async def __call__(self, data: LoginDTO) -> SessionToken: try: user = await self._user_gateway.get_user_by_username(data.username) - except UserNotFound: - raise WrongCredentials + except UserNotFoundError as err: + raise WrongCredentialsError from err assert user.id is not None if not self._password_provider.verify_password(data.password, user.hashed_password): - raise WrongCredentials + raise WrongCredentialsError token = uuid.uuid4() diff --git a/src/just_chat/domain/exceptions/__init__.py b/src/just_chat/domain/exceptions/__init__.py index a528e9f..0a2524f 100644 --- a/src/just_chat/domain/exceptions/__init__.py +++ b/src/just_chat/domain/exceptions/__init__.py @@ -1,8 +1,8 @@ __all__ = ( "DomainError", - "AccessDenied", + "AccessDeniedError", ) -from .base import DomainError # noqa F401 -from .access import AccessDenied # noqa F401 +from .access import AccessDeniedError +from .base import DomainError diff --git a/src/just_chat/domain/exceptions/access.py b/src/just_chat/domain/exceptions/access.py index a331ca6..1fd63a0 100644 --- a/src/just_chat/domain/exceptions/access.py +++ b/src/just_chat/domain/exceptions/access.py @@ -1,5 +1,5 @@ -from just_chat.domain.exceptions import DomainError +from just_chat.domain.exceptions.base import DomainError -class AccessDenied(DomainError): +class AccessDeniedError(DomainError): pass diff --git a/src/just_chat/domain/models/message.py b/src/just_chat/domain/models/message.py index 9b741ae..53be6ea 100644 --- a/src/just_chat/domain/models/message.py +++ b/src/just_chat/domain/models/message.py @@ -1,8 +1,8 @@ from dataclasses import dataclass from typing import NewType -from .user import UserId from .chat import ChatId +from .user import UserId MessageId = NewType("MessageId", int) diff --git a/src/just_chat/domain/services/chat.py b/src/just_chat/domain/services/chat.py index 43c5b7a..7350245 100644 --- a/src/just_chat/domain/services/chat.py +++ b/src/just_chat/domain/services/chat.py @@ -1,7 +1,7 @@ -from typing import Sequence +from collections.abc import Sequence -from ..models.chat import Chat -from ..models.user import UserId +from just_chat.domain.models.chat import Chat +from just_chat.domain.models.user import UserId class ChatService: diff --git a/src/just_chat/domain/services/chat_access.py b/src/just_chat/domain/services/chat_access.py index c8cef3a..cabe291 100644 --- a/src/just_chat/domain/services/chat_access.py +++ b/src/just_chat/domain/services/chat_access.py @@ -1,9 +1,9 @@ -from ..exceptions.access import AccessDenied -from ..models.chat import Chat -from ..models.user import UserId +from just_chat.domain.exceptions.access import AccessDeniedError +from just_chat.domain.models.chat import Chat +from just_chat.domain.models.user import UserId class ChatAccessService: def ensure_user_can_write_to_chat(self, chat: Chat, user_id: UserId) -> None: if user_id not in chat.users_ids: - raise AccessDenied + raise AccessDeniedError diff --git a/src/just_chat/domain/services/message.py b/src/just_chat/domain/services/message.py index f99199a..53d6fd2 100644 --- a/src/just_chat/domain/services/message.py +++ b/src/just_chat/domain/services/message.py @@ -1,6 +1,6 @@ -from ..models.message import Message -from ..models.user import UserId -from ..models.chat import ChatId +from just_chat.domain.models.chat import ChatId +from just_chat.domain.models.message import Message +from just_chat.domain.models.user import UserId class MessageService: diff --git a/src/just_chat/domain/services/notification.py b/src/just_chat/domain/services/notification.py index 4bf3c52..4f6cdf0 100644 --- a/src/just_chat/domain/services/notification.py +++ b/src/just_chat/domain/services/notification.py @@ -1,5 +1,5 @@ -from ..models.user import UserId -from ..models.notification import NotificationsSettings +from just_chat.domain.models.notification import NotificationsSettings +from just_chat.domain.models.user import UserId class NotificationsSettingsService: diff --git a/src/just_chat/domain/services/user.py b/src/just_chat/domain/services/user.py index af5d620..e9d7c83 100644 --- a/src/just_chat/domain/services/user.py +++ b/src/just_chat/domain/services/user.py @@ -1,4 +1,4 @@ -from ..models.user import User +from just_chat.domain.models.user import User class UserService: diff --git a/src/just_chat/main/ioc.py b/src/just_chat/main/ioc.py index 5d4859d..4e7e19a 100644 --- a/src/just_chat/main/ioc.py +++ b/src/just_chat/main/ioc.py @@ -1,5 +1,5 @@ +from collections.abc import AsyncGenerator from contextlib import asynccontextmanager -from typing import AsyncGenerator from passlib.handlers.argon2 import argon2 @@ -14,14 +14,14 @@ from just_chat.application.chat.delete_chat import DeleteChat from just_chat.application.chat.get_chat import GetChat from just_chat.application.common.id_provider import IdProvider +from just_chat.application.event.add_user_event_bus import AddUserEventBus +from just_chat.application.event.delete_user_event_bus import DeleteUserEventBus from just_chat.application.message.create_message import CreateMessage from just_chat.application.message.get_chat_messages import GetChatMessages from just_chat.application.user.create_user import CreateUser from just_chat.application.user.get_user_by_id import GetUserById from just_chat.application.user.get_user_by_token import GetUserIdByToken from just_chat.application.user.login import Login -from just_chat.application.event.add_user_event_bus import AddUserEventBus -from just_chat.application.event.delete_user_event_bus import DeleteUserEventBus from just_chat.domain.services.chat import ChatService from just_chat.domain.services.chat_access import ChatAccessService from just_chat.domain.services.event import EventService @@ -48,7 +48,7 @@ async def create_chat(self) -> AsyncGenerator[CreateChat, None]: @asynccontextmanager async def create_chat_with_random_user( self, - id_provider: IdProvider + id_provider: IdProvider, ) -> AsyncGenerator[CreateChatWithRandomUser, None]: yield CreateChatWithRandomUser( ChatService(), @@ -72,7 +72,7 @@ def __init__(self) -> None: async def get_user(self) -> AsyncGenerator[GetUserById, None]: yield GetUserById( UserService(), - self._user_gateway + self._user_gateway, ) @asynccontextmanager @@ -85,7 +85,7 @@ async def get_user_id_by_token(self) -> AsyncGenerator[GetUserIdByToken, None]: async def create_user(self) -> AsyncGenerator[CreateUser, None]: yield CreateUser( UserService(), - self._user_gateway + self._user_gateway, ) @asynccontextmanager diff --git a/src/just_chat/main/telegram_bot.py b/src/just_chat/main/telegram_bot.py index cf68fbc..5718dac 100644 --- a/src/just_chat/main/telegram_bot.py +++ b/src/just_chat/main/telegram_bot.py @@ -3,7 +3,7 @@ import sys from os import getenv -from aiogram import Dispatcher, Bot +from aiogram import Bot, Dispatcher from aiogram.enums import ParseMode from just_chat.main.ioc import UserIoC @@ -14,7 +14,7 @@ async def main() -> None: user_ioc = UserIoC() dispatcher = Dispatcher( - user_ioc=user_ioc + user_ioc=user_ioc, ) dispatcher.include_router(router) diff --git a/src/just_chat/main/web.py b/src/just_chat/main/web.py index 1220143..03f52d4 100644 --- a/src/just_chat/main/web.py +++ b/src/just_chat/main/web.py @@ -1,4 +1,5 @@ -from typing import TypeVar, Callable +from collections.abc import Callable +from typing import TypeVar from fastapi import FastAPI from passlib.handlers.argon2 import argon2 @@ -6,7 +7,7 @@ from just_chat.adapters.security.password_provider import HashingPasswordProvider from just_chat.application.common.id_provider import IdProvider from just_chat.application.common.password_provider import PasswordProvider -from just_chat.main.ioc import ChatIoC, UserIoC, MessageIoC, EventIoC +from just_chat.main.ioc import ChatIoC, EventIoC, MessageIoC, UserIoC from just_chat.presentation.interactor_factory.chat import ChatInteractorFactory from just_chat.presentation.interactor_factory.event import EventInteractorFactory from just_chat.presentation.interactor_factory.message import MessageInteractorFactory diff --git a/src/just_chat/presentation/interactor_factory/chat.py b/src/just_chat/presentation/interactor_factory/chat.py index cc447ae..3609222 100644 --- a/src/just_chat/presentation/interactor_factory/chat.py +++ b/src/just_chat/presentation/interactor_factory/chat.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import AsyncContextManager +from contextlib import AbstractAsyncContextManager from just_chat.application.chat.create_chat import CreateChat from just_chat.application.chat.create_chat_with_random_user import CreateChatWithRandomUser @@ -10,17 +10,20 @@ class ChatInteractorFactory(ABC): @abstractmethod - def get_chat(self) -> AsyncContextManager[GetChat]: + def get_chat(self) -> AbstractAsyncContextManager[GetChat]: raise NotImplementedError @abstractmethod - def create_chat(self) -> AsyncContextManager[CreateChat]: + def create_chat(self) -> AbstractAsyncContextManager[CreateChat]: raise NotImplementedError @abstractmethod - def create_chat_with_random_user(self, id_provider: IdProvider) -> AsyncContextManager[CreateChatWithRandomUser]: + def create_chat_with_random_user( + self, + id_provider: IdProvider, + ) -> AbstractAsyncContextManager[CreateChatWithRandomUser]: raise NotImplementedError @abstractmethod - def delete_chat(self) -> AsyncContextManager[DeleteChat]: + def delete_chat(self) -> AbstractAsyncContextManager[DeleteChat]: raise NotImplementedError diff --git a/src/just_chat/presentation/interactor_factory/event.py b/src/just_chat/presentation/interactor_factory/event.py index 6652e89..6c3b477 100644 --- a/src/just_chat/presentation/interactor_factory/event.py +++ b/src/just_chat/presentation/interactor_factory/event.py @@ -1,16 +1,16 @@ from abc import ABC, abstractmethod -from typing import AsyncContextManager +from contextlib import AbstractAsyncContextManager -from just_chat.application.event.add_user_event_bus import AddUserEventBus from just_chat.application.common.id_provider import IdProvider +from just_chat.application.event.add_user_event_bus import AddUserEventBus from just_chat.application.event.delete_user_event_bus import DeleteUserEventBus class EventInteractorFactory(ABC): @abstractmethod - def add_user_event_bus(self, id_provider: IdProvider) -> AsyncContextManager[AddUserEventBus]: + def add_user_event_bus(self, id_provider: IdProvider) -> AbstractAsyncContextManager[AddUserEventBus]: raise NotImplementedError @abstractmethod - def delete_user_event_bus(self, id_provider: IdProvider) -> AsyncContextManager[DeleteUserEventBus]: + def delete_user_event_bus(self, id_provider: IdProvider) -> AbstractAsyncContextManager[DeleteUserEventBus]: raise NotImplementedError diff --git a/src/just_chat/presentation/interactor_factory/message.py b/src/just_chat/presentation/interactor_factory/message.py index e13f57e..6e67c7a 100644 --- a/src/just_chat/presentation/interactor_factory/message.py +++ b/src/just_chat/presentation/interactor_factory/message.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import AsyncContextManager +from contextlib import AbstractAsyncContextManager from just_chat.application.common.id_provider import IdProvider from just_chat.application.message.create_message import CreateMessage @@ -8,9 +8,9 @@ class MessageInteractorFactory(ABC): @abstractmethod - def create_message(self, id_provider: IdProvider) -> AsyncContextManager[CreateMessage]: + def create_message(self, id_provider: IdProvider) -> AbstractAsyncContextManager[CreateMessage]: raise NotImplementedError @abstractmethod - def get_chat_messages(self, id_provider: IdProvider) -> AsyncContextManager[GetChatMessages]: + def get_chat_messages(self, id_provider: IdProvider) -> AbstractAsyncContextManager[GetChatMessages]: raise NotImplementedError diff --git a/src/just_chat/presentation/interactor_factory/user.py b/src/just_chat/presentation/interactor_factory/user.py index 6e0a18d..35d60fc 100644 --- a/src/just_chat/presentation/interactor_factory/user.py +++ b/src/just_chat/presentation/interactor_factory/user.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import AsyncContextManager +from contextlib import AbstractAsyncContextManager from just_chat.application.user.create_user import CreateUser from just_chat.application.user.get_user_by_id import GetUserById @@ -9,17 +9,17 @@ class UserInteractorFactory(ABC): @abstractmethod - def get_user(self) -> AsyncContextManager[GetUserById]: + def get_user(self) -> AbstractAsyncContextManager[GetUserById]: raise NotImplementedError @abstractmethod - def get_user_id_by_token(self) -> AsyncContextManager[GetUserIdByToken]: + def get_user_id_by_token(self) -> AbstractAsyncContextManager[GetUserIdByToken]: raise NotImplementedError @abstractmethod - def create_user(self) -> AsyncContextManager[CreateUser]: + def create_user(self) -> AbstractAsyncContextManager[CreateUser]: raise NotImplementedError @abstractmethod - def login(self) -> AsyncContextManager[Login]: + def login(self) -> AbstractAsyncContextManager[Login]: raise NotImplementedError diff --git a/src/just_chat/presentation/telegram_bot/__init__.py b/src/just_chat/presentation/telegram_bot/__init__.py index e75a742..51dc3dd 100644 --- a/src/just_chat/presentation/telegram_bot/__init__.py +++ b/src/just_chat/presentation/telegram_bot/__init__.py @@ -2,9 +2,10 @@ "router", ) +from . import ( + get_chats, # noqa: F401 + login, # noqa: F401 + select_chat, # noqa: F401 + start, # noqa: F401 +) from .router import router -from . import get_chats # noqa F401 -from . import login # noqa F401 -from . import select_chat # noqa F401 -from . import start # noqa F401 -from . import start # noqa F401 diff --git a/src/just_chat/presentation/telegram_bot/login.py b/src/just_chat/presentation/telegram_bot/login.py index e6d488e..021e652 100644 --- a/src/just_chat/presentation/telegram_bot/login.py +++ b/src/just_chat/presentation/telegram_bot/login.py @@ -1,10 +1,10 @@ from aiogram.fsm.context import FSMContext from aiogram.types import Message -from just_chat.application.user.login import Login, LoginDTO, WrongCredentials +from just_chat.application.user.login import Login, LoginDTO, WrongCredentialsError from just_chat.presentation.interactor_factory.user import UserInteractorFactory from just_chat.presentation.telegram_bot.get_chats import get_chats -from just_chat.presentation.telegram_bot.router import router +from just_chat.presentation.telegram_bot.router import router from just_chat.presentation.telegram_bot.states import LoginForm @@ -46,7 +46,7 @@ async def authenticate(interactor: Login, message: Message, state: FSMContext) - username=data["login"], password=data["password"], )) - except WrongCredentials: + except WrongCredentialsError: await message.answer("Authentication failed") await state.clear() await get_login(message, state) diff --git a/src/just_chat/presentation/telegram_bot/states.py b/src/just_chat/presentation/telegram_bot/states.py index 12b2c62..87c21db 100644 --- a/src/just_chat/presentation/telegram_bot/states.py +++ b/src/just_chat/presentation/telegram_bot/states.py @@ -1,4 +1,4 @@ -from aiogram.fsm.state import StatesGroup, State +from aiogram.fsm.state import State, StatesGroup class LoginForm(StatesGroup): diff --git a/src/just_chat/presentation/web_api/__init__.py b/src/just_chat/presentation/web_api/__init__.py index 8be8567..7a00052 100644 --- a/src/just_chat/presentation/web_api/__init__.py +++ b/src/just_chat/presentation/web_api/__init__.py @@ -1,5 +1,5 @@ __all__ = [ - "api_router" + "api_router", ] from fastapi import APIRouter diff --git a/src/just_chat/presentation/web_api/admin/__init__.py b/src/just_chat/presentation/web_api/admin/__init__.py index 0dd0ca2..2fad7d1 100644 --- a/src/just_chat/presentation/web_api/admin/__init__.py +++ b/src/just_chat/presentation/web_api/admin/__init__.py @@ -1,5 +1,5 @@ __all__ = [ - "admin_router" + "admin_router", ] from fastapi import APIRouter diff --git a/src/just_chat/presentation/web_api/admin/chat/__init__.py b/src/just_chat/presentation/web_api/admin/chat/__init__.py index cb04121..7893168 100644 --- a/src/just_chat/presentation/web_api/admin/chat/__init__.py +++ b/src/just_chat/presentation/web_api/admin/chat/__init__.py @@ -1,8 +1,10 @@ __all__ = [ - "chat_router" + "chat_router", ] +from . import ( + create_chat, # noqa: F401 + delete_chat, # noqa: F401 + get_chat, # noqa: F401 +) from .router import chat_router -from . import create_chat # noqa: F401 -from . import get_chat # noqa: F401 -from . import delete_chat # noqa: F401 diff --git a/src/just_chat/presentation/web_api/admin/chat/create_chat.py b/src/just_chat/presentation/web_api/admin/chat/create_chat.py index 2533275..aed3ca9 100644 --- a/src/just_chat/presentation/web_api/admin/chat/create_chat.py +++ b/src/just_chat/presentation/web_api/admin/chat/create_chat.py @@ -4,8 +4,8 @@ from just_chat.application.chat.create_chat import NewChatDTO from just_chat.domain.models.chat import Chat -from just_chat.presentation.web_api.admin.chat.router import chat_router from just_chat.presentation.interactor_factory.chat import ChatInteractorFactory +from just_chat.presentation.web_api.admin.chat.router import chat_router @chat_router.post("/") diff --git a/src/just_chat/presentation/web_api/admin/chat/delete_chat.py b/src/just_chat/presentation/web_api/admin/chat/delete_chat.py index 9fd1244..66bb52a 100644 --- a/src/just_chat/presentation/web_api/admin/chat/delete_chat.py +++ b/src/just_chat/presentation/web_api/admin/chat/delete_chat.py @@ -3,10 +3,10 @@ from fastapi import Depends, Path, status from fastapi.exceptions import HTTPException -from just_chat.application.common.chat_gateway import ChatNotFound -from just_chat.domain.models.chat import ChatId, Chat -from just_chat.presentation.web_api.admin.chat.router import chat_router +from just_chat.application.common.chat_gateway import ChatNotFoundError +from just_chat.domain.models.chat import Chat, ChatId from just_chat.presentation.interactor_factory.chat import ChatInteractorFactory +from just_chat.presentation.web_api.admin.chat.router import chat_router @chat_router.delete("/{chat_id}") @@ -17,5 +17,5 @@ async def delete_chat( async with interactor_factory.delete_chat() as delete_chat_interactor: try: return await delete_chat_interactor(ChatId(chat_id)) - except ChatNotFound: - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) + except ChatNotFoundError as err: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from err diff --git a/src/just_chat/presentation/web_api/admin/chat/get_chat.py b/src/just_chat/presentation/web_api/admin/chat/get_chat.py index 8fe4107..5ccf36a 100644 --- a/src/just_chat/presentation/web_api/admin/chat/get_chat.py +++ b/src/just_chat/presentation/web_api/admin/chat/get_chat.py @@ -3,10 +3,10 @@ from fastapi import Depends, Path, status from fastapi.exceptions import HTTPException -from just_chat.application.common.chat_gateway import ChatNotFound -from just_chat.domain.models.chat import ChatId, Chat -from just_chat.presentation.web_api.admin.chat.router import chat_router +from just_chat.application.common.chat_gateway import ChatNotFoundError +from just_chat.domain.models.chat import Chat, ChatId from just_chat.presentation.interactor_factory.chat import ChatInteractorFactory +from just_chat.presentation.web_api.admin.chat.router import chat_router @chat_router.get("/{chat_id}") @@ -17,5 +17,5 @@ async def get_chat( async with interactor_factory.get_chat() as get_chat_interactor: try: return await get_chat_interactor(ChatId(chat_id)) - except ChatNotFound: - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) + except ChatNotFoundError as err: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from err diff --git a/src/just_chat/presentation/web_api/admin/user/__init__.py b/src/just_chat/presentation/web_api/admin/user/__init__.py index b749bda..7913472 100644 --- a/src/just_chat/presentation/web_api/admin/user/__init__.py +++ b/src/just_chat/presentation/web_api/admin/user/__init__.py @@ -1,6 +1,6 @@ __all__ = [ - "user_router" + "user_router", ] +from . import get_user # noqa: F401 from .router import user_router -from . import get_user # noqa F401 diff --git a/src/just_chat/presentation/web_api/admin/user/get_user.py b/src/just_chat/presentation/web_api/admin/user/get_user.py index 29b08b1..74f9461 100644 --- a/src/just_chat/presentation/web_api/admin/user/get_user.py +++ b/src/just_chat/presentation/web_api/admin/user/get_user.py @@ -3,7 +3,7 @@ from fastapi import Depends, HTTPException from starlette import status -from just_chat.application.common.user_gateway import UserNotFound +from just_chat.application.common.user_gateway import UserNotFoundError from just_chat.application.user.get_user_by_id import UserDTO from just_chat.domain.models.user import UserId from just_chat.presentation.interactor_factory.user import UserInteractorFactory @@ -18,5 +18,5 @@ async def get_user_by_id( async with interactor_factory.get_user() as get_user_interactor: try: return await get_user_interactor(UserId(user_id)) - except UserNotFound: - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) + except UserNotFoundError as err: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from err diff --git a/src/just_chat/presentation/web_api/dependencies/id_provider.py b/src/just_chat/presentation/web_api/dependencies/id_provider.py index 52ee82e..7affbe0 100644 --- a/src/just_chat/presentation/web_api/dependencies/id_provider.py +++ b/src/just_chat/presentation/web_api/dependencies/id_provider.py @@ -1,6 +1,7 @@ -from typing import Annotated, AsyncGenerator +from collections.abc import AsyncGenerator +from typing import Annotated -from fastapi import Depends, Cookie +from fastapi import Cookie, Depends from just_chat.adapters.auth.password import SessionIdProvider from just_chat.application.common.session_gateway import SessionToken @@ -9,7 +10,7 @@ async def get_session_id_provider( ioc: Annotated[UserInteractorFactory, Depends()], - token: Annotated[str, Cookie()] + token: Annotated[str, Cookie()], ) -> AsyncGenerator[SessionIdProvider, None]: async with ioc.get_user_id_by_token() as get_user_id_by_token_interactor: yield SessionIdProvider(SessionToken(token), get_user_id_by_token_interactor) diff --git a/src/just_chat/presentation/web_api/dependencies/stub.py b/src/just_chat/presentation/web_api/dependencies/stub.py index 1a9f76f..cb51b29 100644 --- a/src/just_chat/presentation/web_api/dependencies/stub.py +++ b/src/just_chat/presentation/web_api/dependencies/stub.py @@ -1,4 +1,5 @@ -from typing import Callable, Any +from collections.abc import Callable +from typing import Any class Stub: @@ -23,7 +24,7 @@ def __init__(self, dependency: Callable[..., Any], **kwargs: Any) -> None: def __call__(self) -> None: raise NotImplementedError - def __eq__(self, other: Any) -> bool: + def __eq__(self, other: object) -> bool: if isinstance(other, Stub): return ( self._dependency == other._dependency @@ -31,7 +32,7 @@ def __eq__(self, other: Any) -> bool: ) else: if not self._kwargs: - return self._dependency == other # type: ignore[no-any-return] + return self._dependency == other return False def __hash__(self) -> int: diff --git a/src/just_chat/presentation/web_api/dependencies/user.py b/src/just_chat/presentation/web_api/dependencies/user.py index f73d7fd..08677a2 100644 --- a/src/just_chat/presentation/web_api/dependencies/user.py +++ b/src/just_chat/presentation/web_api/dependencies/user.py @@ -7,6 +7,6 @@ async def get_hashed_password( password: Annotated[str, Body(embed=True)], - password_provider: Annotated[PasswordProvider, Depends()] + password_provider: Annotated[PasswordProvider, Depends()], ) -> str: return password_provider.hash_password(password) diff --git a/src/just_chat/presentation/web_api/public/__init__.py b/src/just_chat/presentation/web_api/public/__init__.py index c16fd0d..4a1f5fc 100644 --- a/src/just_chat/presentation/web_api/public/__init__.py +++ b/src/just_chat/presentation/web_api/public/__init__.py @@ -1,13 +1,13 @@ __all__ = [ - "public_router" + "public_router", ] from fastapi import APIRouter from .chat import chat_router -from .user import user_router -from .message import message_router from .event import event_router +from .message import message_router +from .user import user_router public_router = APIRouter(prefix="") public_router.include_router(chat_router) diff --git a/src/just_chat/presentation/web_api/public/chat/__init__.py b/src/just_chat/presentation/web_api/public/chat/__init__.py index cb88e44..3ffc256 100644 --- a/src/just_chat/presentation/web_api/public/chat/__init__.py +++ b/src/just_chat/presentation/web_api/public/chat/__init__.py @@ -1,6 +1,6 @@ __all__ = [ - "chat_router" + "chat_router", ] +from . import create_chat_with_random_user # noqa: F401 from .router import chat_router -from . import create_chat_with_random_user # noqa F401 diff --git a/src/just_chat/presentation/web_api/public/chat/create_chat_with_random_user.py b/src/just_chat/presentation/web_api/public/chat/create_chat_with_random_user.py index 3e104be..2ec779e 100644 --- a/src/just_chat/presentation/web_api/public/chat/create_chat_with_random_user.py +++ b/src/just_chat/presentation/web_api/public/chat/create_chat_with_random_user.py @@ -5,9 +5,9 @@ from just_chat.application.chat.create_chat_with_random_user import NewChatDTO from just_chat.application.common.id_provider import IdProvider from just_chat.domain.models.chat import Chat -from .router import chat_router -from just_chat.presentation.web_api.dependencies.stub import Stub from just_chat.presentation.interactor_factory.chat import ChatInteractorFactory +from just_chat.presentation.web_api.dependencies.stub import Stub +from .router import chat_router @chat_router.post("/random") diff --git a/src/just_chat/presentation/web_api/public/event/__init__.py b/src/just_chat/presentation/web_api/public/event/__init__.py index 9d69c54..d206c33 100644 --- a/src/just_chat/presentation/web_api/public/event/__init__.py +++ b/src/just_chat/presentation/web_api/public/event/__init__.py @@ -1,6 +1,6 @@ __all__ = [ - "event_router" + "event_router", ] -from .router import event_router from . import listen # noqa: F401 +from .router import event_router diff --git a/src/just_chat/presentation/web_api/public/event/listen.py b/src/just_chat/presentation/web_api/public/event/listen.py index ec26acf..c939d7b 100644 --- a/src/just_chat/presentation/web_api/public/event/listen.py +++ b/src/just_chat/presentation/web_api/public/event/listen.py @@ -1,14 +1,14 @@ import asyncio from typing import Annotated, Any -from fastapi import WebSocket, Depends -from starlette.websockets import WebSocketState, WebSocketDisconnect +from fastapi import Depends, WebSocket +from starlette.websockets import WebSocketDisconnect, WebSocketState -from just_chat.application.common.event_bus import EventBus, ConnectionClosed +from just_chat.application.common.event_bus import ConnectionClosedError, EventBus from just_chat.application.common.id_provider import IdProvider from just_chat.presentation.interactor_factory.event import EventInteractorFactory from just_chat.presentation.web_api.dependencies.stub import Stub -from just_chat.presentation.web_api.public.event import event_router +from just_chat.presentation.web_api.public.event.router import event_router class WebsocketEventBus(EventBus): @@ -18,8 +18,8 @@ def __init__(self, websocket: WebSocket) -> None: async def send_json(self, data: Any) -> None: try: await self._websocket.send_json(data) - except WebSocketDisconnect: - raise ConnectionClosed + except WebSocketDisconnect as err: + raise ConnectionClosedError from err @event_router.websocket("/listen") diff --git a/src/just_chat/presentation/web_api/public/message/__init__.py b/src/just_chat/presentation/web_api/public/message/__init__.py index 5e39af1..7192cdb 100644 --- a/src/just_chat/presentation/web_api/public/message/__init__.py +++ b/src/just_chat/presentation/web_api/public/message/__init__.py @@ -2,6 +2,8 @@ "message_router", ) +from . import ( + get_chat_messages, # noqa: F401 + send_message, # noqa: F401 +) from .router import message_router -from . import send_message # noqa F401 -from . import get_chat_messages # noqa F401 diff --git a/src/just_chat/presentation/web_api/public/message/get_chat_messages.py b/src/just_chat/presentation/web_api/public/message/get_chat_messages.py index 8bf94e8..0d0d51b 100644 --- a/src/just_chat/presentation/web_api/public/message/get_chat_messages.py +++ b/src/just_chat/presentation/web_api/public/message/get_chat_messages.py @@ -3,12 +3,12 @@ from fastapi import Depends, HTTPException, Path from just_chat.application.common.id_provider import IdProvider -from just_chat.domain.exceptions import AccessDenied +from just_chat.domain.exceptions import AccessDeniedError from just_chat.domain.models.chat import ChatId from just_chat.domain.models.message import Message -from .router import message_router -from just_chat.presentation.web_api.dependencies.stub import Stub from just_chat.presentation.interactor_factory.message import MessageInteractorFactory +from just_chat.presentation.web_api.dependencies.stub import Stub +from .router import message_router @message_router.get("/chat/{chat_id}") @@ -20,6 +20,6 @@ async def get_chat_messages( try: async with interactor_factory.get_chat_messages(id_provider) as get_chat_messages_interactor: return await get_chat_messages_interactor(chat_id) - except AccessDenied: - raise HTTPException(status_code=403, detail="Access denied") + except AccessDeniedError as err: + raise HTTPException(status_code=403, detail="Access denied") from err diff --git a/src/just_chat/presentation/web_api/public/message/send_message.py b/src/just_chat/presentation/web_api/public/message/send_message.py index b36afe5..f1ba981 100644 --- a/src/just_chat/presentation/web_api/public/message/send_message.py +++ b/src/just_chat/presentation/web_api/public/message/send_message.py @@ -4,11 +4,11 @@ from just_chat.application.common.id_provider import IdProvider from just_chat.application.message.create_message import NewMessageDTO -from just_chat.domain.exceptions import AccessDenied +from just_chat.domain.exceptions import AccessDeniedError from just_chat.domain.models.message import Message -from .router import message_router -from just_chat.presentation.web_api.dependencies.stub import Stub from just_chat.presentation.interactor_factory.message import MessageInteractorFactory +from just_chat.presentation.web_api.dependencies.stub import Stub +from .router import message_router @message_router.post("") @@ -20,6 +20,6 @@ async def send_message( try: async with interactor_factory.create_message(id_provider) as create_message_interactor: return await create_message_interactor(data) - except AccessDenied: - raise HTTPException(status_code=403, detail="Access denied") + except AccessDeniedError as err: + raise HTTPException(status_code=403, detail="Access denied") from err diff --git a/src/just_chat/presentation/web_api/public/user/__init__.py b/src/just_chat/presentation/web_api/public/user/__init__.py index 1c000ba..96cfbb1 100644 --- a/src/just_chat/presentation/web_api/public/user/__init__.py +++ b/src/just_chat/presentation/web_api/public/user/__init__.py @@ -1,7 +1,7 @@ __all__ = [ - "user_router" + "user_router", ] -from .router import user_router from .create_user import create_user # noqa: F401 from .login import login # noqa: F401 +from .router import user_router diff --git a/src/just_chat/presentation/web_api/public/user/create_user.py b/src/just_chat/presentation/web_api/public/user/create_user.py index a679708..5576492 100644 --- a/src/just_chat/presentation/web_api/public/user/create_user.py +++ b/src/just_chat/presentation/web_api/public/user/create_user.py @@ -1,11 +1,11 @@ from typing import Annotated -from fastapi import Depends, Body +from fastapi import Body, Depends -from just_chat.application.user.create_user import NewUserDTO, CreatedUserDTO -from .router import user_router -from just_chat.presentation.web_api.dependencies.user import get_hashed_password +from just_chat.application.user.create_user import CreatedUserDTO, NewUserDTO from just_chat.presentation.interactor_factory.user import UserInteractorFactory +from just_chat.presentation.web_api.dependencies.user import get_hashed_password +from .router import user_router @user_router.post(r"/") diff --git a/src/just_chat/presentation/web_api/public/user/login.py b/src/just_chat/presentation/web_api/public/user/login.py index 5e58a6e..4503f3f 100644 --- a/src/just_chat/presentation/web_api/public/user/login.py +++ b/src/just_chat/presentation/web_api/public/user/login.py @@ -1,11 +1,11 @@ from typing import Annotated -from fastapi import Depends, Body, Response, status, HTTPException +from fastapi import Body, Depends, HTTPException, Response, status -from just_chat.application.common.user_gateway import UserNotFound -from just_chat.application.user.login import LoginDTO, WrongCredentials -from .router import user_router +from just_chat.application.common.user_gateway import UserNotFoundError +from just_chat.application.user.login import LoginDTO, WrongCredentialsError from just_chat.presentation.interactor_factory.user import UserInteractorFactory +from .router import user_router @user_router.post("/login") @@ -21,8 +21,8 @@ async def login( username=username, password=password, )) - except (UserNotFound, WrongCredentials): - raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) + except (UserNotFoundError, WrongCredentialsError) as err: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) from err response.set_cookie("token", value=token) diff --git a/src/just_chat/presentation/web_api/public/user/router.py b/src/just_chat/presentation/web_api/public/user/router.py index 1195384..e1fd484 100644 --- a/src/just_chat/presentation/web_api/public/user/router.py +++ b/src/just_chat/presentation/web_api/public/user/router.py @@ -1,4 +1,3 @@ from fastapi import APIRouter - user_router = APIRouter(prefix=r"/user", tags=["User"]) diff --git a/tests/application/test_chat.py b/tests/application/test_chat.py index 6d7f6cb..f92f75e 100644 --- a/tests/application/test_chat.py +++ b/tests/application/test_chat.py @@ -6,7 +6,7 @@ from just_chat.application.chat.create_chat import CreateChat, NewChatDTO from just_chat.application.chat.delete_chat import DeleteChat from just_chat.application.chat.get_chat import GetChat -from just_chat.application.common.chat_gateway import ChatGateway, ChatNotFound +from just_chat.application.common.chat_gateway import ChatGateway, ChatNotFoundError from just_chat.domain.models.chat import Chat, ChatId from just_chat.domain.models.user import UserId from just_chat.domain.services.chat import ChatService @@ -31,7 +31,7 @@ async def save(chat: Chat): async def get_by_id(chat_id: int): if chat_id in gateway.db: return gateway.db[chat_id] - raise ChatNotFound + raise ChatNotFoundError gateway.get_chat_by_id = get_by_id @@ -93,5 +93,5 @@ async def test_delete_chat_by_id(chat_gateway): assert chat.title == CHAT_TITLE assert chat.users_ids == CHAT_USER_IDS - with pytest.raises(ChatNotFound): + with pytest.raises(ChatNotFoundError): await interactor(ChatId(chat_id)) diff --git a/tests/application/test_user.py b/tests/application/test_user.py index 8cefb8f..c9551fd 100644 --- a/tests/application/test_user.py +++ b/tests/application/test_user.py @@ -3,7 +3,7 @@ import pytest from just_chat.application.common.password_provider import PasswordProvider -from just_chat.application.common.session_gateway import SessionGateway, SessionNotFound +from just_chat.application.common.session_gateway import SessionGateway, SessionNotFoundError from just_chat.application.common.user_gateway import UserGateway from just_chat.application.user.create_user import CreateUser, NewUserDTO from just_chat.application.user.get_user_by_id import GetUserById @@ -50,7 +50,7 @@ async def save(user_id: UserId, token: str): async def get(token: str): if token in gateway.user_ids: return gateway.user_ids[token] - raise SessionNotFound + raise SessionNotFoundError gateway.get_user_id = get return gateway