BTHLABS-61: Service layer refactoring

A journey to fix `ValidationError` in Pocket imports turned service
layer refactoring :D
This commit is contained in:
2025-10-12 18:37:32 +00:00
parent ac7a8dd90e
commit 8b86145519
45 changed files with 1023 additions and 337 deletions

View File

@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
import http
import uuid
from hotpocket_backend.apps.accounts.services import (
AuthKeysService as BackendAuthKeysService,
)
from hotpocket_soa.dto.accounts import AuthKeyOut
from hotpocket_soa.exceptions.backend import NotFound
from .base import ProxyService, SOAError
@@ -15,23 +17,16 @@ class AuthKeysService(ProxyService):
class AuthKeysServiceError(SOAError):
pass
class AuthKeyNotFound(AuthKeysServiceError):
class NotFound(AuthKeysServiceError):
pass
class AuthKeyAccessDenied(AuthKeysServiceError):
class AccessDenied(AuthKeysServiceError):
pass
def __init__(self):
super().__init__()
self.backend_auth_keys_service = BackendAuthKeysService()
def wrap_exception(self, exception: Exception) -> Exception:
new_exception_args = []
if len(exception.args) > 0:
new_exception_args = [exception.args[0]]
return self.AuthKeysServiceError(*new_exception_args)
def _check_auth_key_access(self,
auth_key: AuthKeyOut,
account_uuid: uuid.UUID | None,
@@ -70,16 +65,14 @@ class AuthKeysService(ProxyService):
)
if self._check_auth_key_access(result, account_uuid) is False:
raise self.AuthKeyAccessDenied(
raise self.AccessDenied(
http.HTTPStatus.FORBIDDEN.value,
f'account_uuid=`{account_uuid}` pk=`{pk}`',
)
return result
except SOAError as exception:
if isinstance(exception.__cause__, BackendAuthKeysService.AuthKeyNotFound) is True:
raise self.AuthKeyNotFound(*exception.args) from exception
else:
raise
except NotFound as exception:
raise self.NotFound.from_backend_error(exception)
def get_by_key(self,
*,
@@ -97,13 +90,11 @@ class AuthKeysService(ProxyService):
)
if self._check_auth_key_access(result, account_uuid) is False:
raise self.AuthKeyAccessDenied(
raise self.AccessDenied(
http.HTTPStatus.FORBIDDEN.value,
f'account_uuid=`{account_uuid}` key=`{key}`',
)
return result
except SOAError as exception:
if isinstance(exception.__cause__, BackendAuthKeysService.AuthKeyNotFound) is True:
raise self.AuthKeyNotFound(*exception.args) from exception
else:
raise
except NotFound as exception:
raise self.NotFound.from_backend_error(exception)