generated from ensaremirerol/nextjs-fastapi-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e344d84
commit c5fe128
Showing
34 changed files
with
1,398 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import logging | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
from typing import Any | ||
|
||
from server.exceptions import ErrCodes, ServerException | ||
|
||
|
||
@dataclass | ||
class FacadeResponse: | ||
status: int | ||
message: str | ||
data: Any | None = None | ||
err_code: ErrCodes | None = None | ||
|
||
def to_dict(self): | ||
return { | ||
"status": self.status, | ||
"message": self.message, | ||
"data": self.data, | ||
"err_code": self.err_code.value | ||
if self.err_code | ||
else None, | ||
} | ||
|
||
|
||
class BaseFacade(ABC): | ||
def __init__(self): | ||
self.logger = logging.getLogger( | ||
self.__class__.__name__ | ||
) | ||
|
||
@abstractmethod | ||
def execute(self, *args, **kwargs) -> FacadeResponse: | ||
pass | ||
|
||
@staticmethod | ||
def error_wrapper( | ||
func, | ||
): | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except Exception as e: | ||
return BaseFacade._error_response(e) | ||
|
||
return wrapper | ||
|
||
@staticmethod | ||
def _error_response( | ||
e: Exception, | ||
): | ||
if isinstance(e, ServerException): | ||
return FacadeResponse( | ||
message=e.message, | ||
status=400, | ||
err_code=e.code, | ||
) | ||
return FacadeResponse( | ||
message=str(e), | ||
status=500, | ||
err_code=ErrCodes.UNKNOWN_ERROR, | ||
) | ||
|
||
@staticmethod | ||
def _success_response( | ||
data: Any, | ||
message: str, | ||
status: int = 200, | ||
) -> FacadeResponse: | ||
return FacadeResponse( | ||
message=message, | ||
status=status, | ||
data=data, | ||
) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from kink import inject | ||
|
||
from server.facades import BaseFacade, FacadeResponse | ||
from server.models.workspace import ( | ||
WorkspaceModel, | ||
WorkspaceType, | ||
) | ||
from server.services.core.workspace_metadata_service import ( | ||
WorkspaceMetadataServiceProtocol, | ||
) | ||
from server.services.local.local_workspace_service import ( | ||
WorkspaceServiceProtocol, | ||
) | ||
|
||
|
||
@inject | ||
class CreateWorkspaceFacade(BaseFacade): | ||
def __init__( | ||
self, | ||
workspace_metadata_service: WorkspaceMetadataServiceProtocol, | ||
workspace_service: WorkspaceServiceProtocol, | ||
): | ||
super().__init__() | ||
self.workspace_metadata_service = ( | ||
workspace_metadata_service | ||
) | ||
self.workspace_service = workspace_service | ||
|
||
@BaseFacade.error_wrapper | ||
def execute( | ||
self, | ||
name: str, | ||
description: str, | ||
type: WorkspaceType, | ||
location: str, | ||
) -> FacadeResponse: | ||
self.logger.info("Creating workspace metadata") | ||
|
||
workspace_metadata = self.workspace_metadata_service.create_workspace_metadata( | ||
name=name, | ||
description=description, | ||
type=type, | ||
location=location, | ||
) | ||
|
||
self.logger.info("Creating workspace") | ||
model: WorkspaceModel = ( | ||
WorkspaceModel.create_with_defaults( | ||
uuid=workspace_metadata.uuid, | ||
name=name, | ||
description=description, | ||
type=type, | ||
location=location, | ||
) | ||
) | ||
self.workspace_service.create_workspace( | ||
workspace=model | ||
) | ||
|
||
return self._success_response( | ||
data=workspace_metadata, | ||
message="Workspace created", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from kink import inject | ||
|
||
from server.exceptions import ErrCodes | ||
from server.facades import ( | ||
BaseFacade, | ||
FacadeResponse, | ||
ServerException, | ||
) | ||
from server.services.core.workspace_metadata_service import ( | ||
WorkspaceMetadataServiceProtocol, | ||
) | ||
from server.services.local.local_workspace_service import ( | ||
WorkspaceServiceProtocol, | ||
) | ||
|
||
|
||
@inject | ||
class DeleteWorkspaceFacade(BaseFacade): | ||
def __init__( | ||
self, | ||
workspace_metadata_service: WorkspaceMetadataServiceProtocol, | ||
workspace_service: WorkspaceServiceProtocol, | ||
): | ||
super().__init__() | ||
self.workspace_metadata_service = ( | ||
workspace_metadata_service | ||
) | ||
self.workspace_service = workspace_service | ||
|
||
@BaseFacade.error_wrapper | ||
def execute( | ||
self, | ||
uuid: str, | ||
) -> FacadeResponse: | ||
self.logger.info("Retrieving workspace metadata") | ||
workspace_metadata = self.workspace_metadata_service.get_workspace_metadata( | ||
uuid=uuid, | ||
) | ||
self.logger.info("Deleting workspace") | ||
try: | ||
self.workspace_service.delete_workspace( | ||
location=workspace_metadata.location, | ||
) | ||
|
||
except ServerException as e: | ||
# Ignore workspace not found error | ||
if e.code != ErrCodes.WORKSPACE_NOT_FOUND: | ||
raise e | ||
|
||
self.logger.info("Deleting workspace metadata") | ||
self.workspace_metadata_service.delete_workspace_metadata( | ||
uuid=uuid, | ||
) | ||
return FacadeResponse( | ||
status=204, | ||
message="Workspace deleted", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from kink import inject | ||
|
||
from server.facades import ( | ||
BaseFacade, | ||
FacadeResponse, | ||
) | ||
from server.services.core.workspace_metadata_service import ( | ||
WorkspaceMetadataServiceProtocol, | ||
) | ||
from server.services.local.local_workspace_service import ( | ||
WorkspaceServiceProtocol, | ||
) | ||
|
||
|
||
@inject | ||
class GetWorkspacesFacade(BaseFacade): | ||
def __init__( | ||
self, | ||
workspace_metadata_service: WorkspaceMetadataServiceProtocol, | ||
workspace_service: WorkspaceServiceProtocol, | ||
): | ||
super().__init__() | ||
self.workspace_metadata_service = ( | ||
workspace_metadata_service | ||
) | ||
self.workspace_service = workspace_service | ||
|
||
@BaseFacade.error_wrapper | ||
def execute( | ||
self, | ||
) -> FacadeResponse: | ||
self.logger.info( | ||
"Retrieving all workspace metadata" | ||
) | ||
all_workspace_metadata = ( | ||
self.workspace_metadata_service.get_workspaces() | ||
) | ||
|
||
all_workspaces = [ | ||
self.workspace_service.get_workspace( | ||
metadata.location, | ||
) | ||
for metadata in all_workspace_metadata | ||
] | ||
|
||
return FacadeResponse( | ||
status=200, | ||
message="All workspaces retrieved", | ||
data=all_workspaces, | ||
) |
79 changes: 79 additions & 0 deletions
79
server/facades/workspace/ontology/create_ontology_in_workspace.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from kink import inject | ||
|
||
from server.const.err_enums import ErrCodes | ||
from server.facades import ( | ||
BaseFacade, | ||
FacadeResponse, | ||
) | ||
from server.models.ontology import Ontology | ||
from server.service_protocols.ontology_service_protocol import ( | ||
OntologyServiceProtocol, | ||
) | ||
from server.services.core.workspace_metadata_service import ( | ||
WorkspaceMetadataServiceProtocol, | ||
) | ||
from server.services.local.local_workspace_service import ( | ||
WorkspaceServiceProtocol, | ||
) | ||
|
||
|
||
@inject | ||
class CreateOntologyInWorkspaceFacade(BaseFacade): | ||
def __init__( | ||
self, | ||
workspace_metadata_service: WorkspaceMetadataServiceProtocol, | ||
workspace_service: WorkspaceServiceProtocol, | ||
ontology_service: OntologyServiceProtocol, | ||
): | ||
super().__init__() | ||
self.workspace_metadata_service = ( | ||
workspace_metadata_service | ||
) | ||
self.workspace_service = workspace_service | ||
self.ontology_service = ontology_service | ||
|
||
@BaseFacade.error_wrapper | ||
def execute( | ||
self, | ||
workspace_id: str, | ||
name: str, | ||
description: str, | ||
base_uri: str, | ||
content: bytes, | ||
) -> FacadeResponse: | ||
self.logger.info("Retrieving workspace metadata") | ||
workspace_metadata = self.workspace_metadata_service.get_workspace_metadata( | ||
workspace_id, | ||
) | ||
|
||
self.logger.info("Retrieving workspace") | ||
workspace = self.workspace_service.get_workspace( | ||
workspace_metadata.location, | ||
) | ||
|
||
self.logger.info("Creating ontology") | ||
ontology: Ontology = ( | ||
self.ontology_service.create_ontology( | ||
name, | ||
description, | ||
base_uri, | ||
content, | ||
) | ||
) | ||
|
||
self.logger.info("Adding ontology to workspace") | ||
|
||
new_model = workspace.copy_with( | ||
ontologies=workspace.ontologies | ||
+ [ontology.uuid], | ||
) | ||
|
||
self.workspace_service.update_workspace( | ||
new_model, | ||
) | ||
|
||
return FacadeResponse( | ||
status=200, | ||
message="Ontology created", | ||
data=ontology, | ||
) |
Oops, something went wrong.