Skip to content

Commit

Permalink
mapping service
Browse files Browse the repository at this point in the history
  • Loading branch information
ensaremirerol committed Dec 13, 2024
1 parent 0ee7009 commit 1848f97
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
4 changes: 4 additions & 0 deletions server/const/err_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ class ErrCodes(Enum):
JSON_PATH_DATA_NOT_UNIQUE = 103
JSON_PATH_NOT_ARRAY = 104
JSON_PATH_NOT_PROVIDED = 105

# Mapping Service
MAPPING_NOT_FOUND = 120
MAPPING_ILLEGAL_UPDATE_OPERATION = 121
96 changes: 95 additions & 1 deletion server/services/local/local_mapping_service.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,109 @@
import json
import logging

from kink import inject

from server.exceptions import ErrCodes
from server.facades import ServerException
from server.models.mapping import MappingGraph
from server.service_protocols.mapping_service_protocol import (
MappingServiceProtocol,
)
from server.services import LocalFSService


@inject(alias=MappingServiceProtocol)
class LocalMappingService(MappingServiceProtocol):
def __init__(self) -> None:
def __init__(self, fs_service: LocalFSService):
self.logger = logging.getLogger(__name__)
self._fs_service: LocalFSService = fs_service

self.logger.info("LocalMappingService initialized")

def get_mapping(self, mapping_id: str) -> MappingGraph:
self.logger.info(f"Getting mapping {mapping_id}")
try:
raw_mapping = (
self._fs_service.download_file_with_uuid(
mapping_id
)
)
except ServerException as e:
if e.code == ErrCodes.FILE_NOT_FOUND:
self.logger.error(
f"Mapping {mapping_id} not found"
)
raise ServerException(
f"Mapping {mapping_id} not found",
code=ErrCodes.MAPPING_NOT_FOUND,
)
self.logger.error(
f"Failed to get mapping {mapping_id}"
)
raise e
except Exception as e:
self.logger.error(
f"Unexpected error while getting mapping {mapping_id}",
exc_info=e,
)
raise ServerException(
"Unexpected error",
code=ErrCodes.UNKNOWN_ERROR,
)

self.logger.info(
f"Mapping {mapping_id} found, parsing"
)
mapping = MappingGraph.from_dict(
json.loads(raw_mapping.decode("utf-8"))
)
self.logger.info(
f"Mapping {mapping_id} parsed successfully"
)
return mapping

def update_mapping(
self, mapping_id: str, graph: MappingGraph
) -> None:
self.logger.info(f"Fetching mapping {mapping_id}")
self.get_mapping(
mapping_id
) # This will raise an exception if the mapping does not exist

if mapping_id != graph.uuid:
raise ServerException(
f"Mapping ID {mapping_id} does not match graph UUID {graph.uuid}",
code=ErrCodes.MAPPING_ILLEGAL_UPDATE_OPERATION,
)

self.logger.info(f"Updating mapping {mapping_id}")
self._fs_service.upload_file(
mapping_id,
json.dumps(graph.to_dict()).encode("utf-8"),
allow_overwrite=True,
)

self.logger.info(
f"Mapping {mapping_id} updated successfully"
)

def create_mapping(self, graph: MappingGraph) -> str:
self.logger.info(f"Creating mapping {graph.uuid}")
self._fs_service.upload_file(
graph.uuid,
json.dumps(graph.to_dict()).encode(
"utf-8",
),
)

self.logger.info(
f"Mapping {graph.uuid} created successfully"
)
return graph.uuid

def delete_mapping(self, mapping_id: str) -> None:
self.logger.info(f"Deleting mapping {mapping_id}")
self.get_mapping(
mapping_id
) # This will raise an exception if the mapping does not exist
self._fs_service.delete_file_with_uuid(mapping_id)

0 comments on commit 1848f97

Please sign in to comment.