2023-07-19 21:47:47 +09:00
|
|
|
from typing import Any, Final
|
2023-05-01 20:00:32 +09:00
|
|
|
|
2023-06-01 16:52:41 +09:00
|
|
|
import requests
|
2023-07-19 14:43:16 +09:00
|
|
|
from pydantic import TypeAdapter
|
2023-05-01 20:00:32 +09:00
|
|
|
from pydantic.dataclasses import dataclass
|
|
|
|
|
|
|
|
from mdrsclient.api.base import BaseApi
|
|
|
|
from mdrsclient.api.utils import token_check
|
2023-05-10 20:10:43 +09:00
|
|
|
from mdrsclient.exceptions import UnauthorizedException
|
2023-05-01 20:00:32 +09:00
|
|
|
from mdrsclient.models import Folder, FolderSimple
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
2023-12-12 20:05:46 +09:00
|
|
|
class FoldersApiCreateResponse:
|
2023-05-01 20:00:32 +09:00
|
|
|
id: str
|
|
|
|
|
|
|
|
|
2023-12-12 20:05:46 +09:00
|
|
|
class FoldersApi(BaseApi):
|
|
|
|
ENTRYPOINT: Final[str] = "v3/folders/"
|
2023-05-01 20:00:32 +09:00
|
|
|
|
|
|
|
def list(self, laboratory_id: int, path: str) -> list[FolderSimple]:
|
2023-05-09 19:45:03 +09:00
|
|
|
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
|
2023-05-01 20:00:32 +09:00
|
|
|
url = self.ENTRYPOINT
|
2023-07-19 21:47:47 +09:00
|
|
|
params: dict[str, str | int] = {"path": path, "laboratory_id": laboratory_id}
|
2023-05-09 13:08:50 +09:00
|
|
|
token_check(self.connection)
|
2023-05-10 14:46:08 +09:00
|
|
|
response = self.connection.get(url, params=params)
|
2023-05-01 20:00:32 +09:00
|
|
|
self._raise_response_error(response)
|
|
|
|
ret: list[FolderSimple] = []
|
|
|
|
for data in response.json():
|
2023-07-19 14:43:16 +09:00
|
|
|
ret.append(TypeAdapter(FolderSimple).validate_python(data))
|
2023-05-01 20:00:32 +09:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def retrieve(self, id: str) -> Folder:
|
2023-05-09 19:45:03 +09:00
|
|
|
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
|
2023-05-01 20:00:32 +09:00
|
|
|
url = self.ENTRYPOINT + id + "/"
|
2023-05-09 13:08:50 +09:00
|
|
|
token_check(self.connection)
|
2023-05-10 14:46:08 +09:00
|
|
|
response = self.connection.get(url)
|
2023-05-01 20:00:32 +09:00
|
|
|
self._raise_response_error(response)
|
2023-07-19 14:43:16 +09:00
|
|
|
ret = TypeAdapter(Folder).validate_python(response.json())
|
2023-05-01 20:00:32 +09:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def create(self, name: str, parent_id: str) -> str:
|
2023-05-09 19:45:03 +09:00
|
|
|
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
|
2023-05-01 20:00:32 +09:00
|
|
|
url = self.ENTRYPOINT
|
2023-07-19 21:47:47 +09:00
|
|
|
data: dict[str, str | int] = {"name": name, "parent_id": parent_id, "description": "", "template_id": -1}
|
2023-05-09 13:08:50 +09:00
|
|
|
token_check(self.connection)
|
2023-05-10 14:46:08 +09:00
|
|
|
response = self.connection.post(url, data=data)
|
2023-05-01 20:00:32 +09:00
|
|
|
self._raise_response_error(response)
|
2023-12-12 20:05:46 +09:00
|
|
|
ret = TypeAdapter(FoldersApiCreateResponse).validate_python(response.json())
|
2023-05-01 20:00:32 +09:00
|
|
|
return ret.id
|
|
|
|
|
|
|
|
def update(self, folder: FolderSimple) -> bool:
|
2023-05-09 19:45:03 +09:00
|
|
|
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
|
2023-05-09 18:38:58 +09:00
|
|
|
url = self.ENTRYPOINT + folder.id + "/"
|
2023-07-19 21:47:47 +09:00
|
|
|
data: dict[str, str | int] = {
|
2023-05-01 20:00:32 +09:00
|
|
|
"name": folder.name,
|
|
|
|
"description": folder.description,
|
|
|
|
}
|
2023-05-09 13:08:50 +09:00
|
|
|
token_check(self.connection)
|
2023-05-10 14:46:08 +09:00
|
|
|
response = self.connection.put(url, data=data)
|
2023-05-01 20:00:32 +09:00
|
|
|
self._raise_response_error(response)
|
|
|
|
return True
|
|
|
|
|
2023-05-26 17:58:25 +09:00
|
|
|
def destroy(self, id: str, recursive: bool) -> bool:
|
2023-05-09 19:45:03 +09:00
|
|
|
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
|
2023-05-01 20:00:32 +09:00
|
|
|
url = self.ENTRYPOINT + id + "/"
|
2023-07-19 21:47:47 +09:00
|
|
|
params: dict[str, str | int] = {"recursive": str(recursive)}
|
2023-05-09 13:08:50 +09:00
|
|
|
token_check(self.connection)
|
2023-05-26 17:58:25 +09:00
|
|
|
response = self.connection.delete(url, params=params)
|
2023-05-01 20:00:32 +09:00
|
|
|
self._raise_response_error(response)
|
|
|
|
return True
|
|
|
|
|
2023-05-10 20:10:43 +09:00
|
|
|
def auth(self, id: str, password: str) -> bool:
|
|
|
|
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
|
|
|
|
url = self.ENTRYPOINT + id + "/auth/"
|
2023-07-19 21:47:47 +09:00
|
|
|
data: dict[str, str | int] = {"password": password}
|
2023-05-10 20:10:43 +09:00
|
|
|
token_check(self.connection)
|
|
|
|
response = self.connection.post(url, data=data)
|
2023-06-01 16:52:41 +09:00
|
|
|
if response.status_code == requests.codes.unauthorized:
|
2023-05-10 20:10:43 +09:00
|
|
|
raise UnauthorizedException("Password is incorrect.")
|
2023-06-01 16:52:41 +09:00
|
|
|
self._raise_response_error(response)
|
2023-05-10 20:10:43 +09:00
|
|
|
return True
|
|
|
|
|
2023-05-11 17:00:14 +09:00
|
|
|
def acl(self, id: str, access_level: int, recursive: bool, password: str | None) -> bool:
|
|
|
|
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
|
|
|
|
url = self.ENTRYPOINT + id + "/acl/"
|
2023-07-19 21:47:47 +09:00
|
|
|
data: dict[str, str | int] = {"access_level": access_level}
|
2023-05-11 17:00:14 +09:00
|
|
|
if password is not None:
|
|
|
|
data.update({"password": password})
|
|
|
|
if recursive is True:
|
|
|
|
data.update({"lower": 1})
|
|
|
|
token_check(self.connection)
|
|
|
|
response = self.connection.post(url, data=data)
|
|
|
|
self._raise_response_error(response)
|
|
|
|
return True
|
|
|
|
|
2023-07-26 19:59:07 +09:00
|
|
|
def move(self, folder: FolderSimple, folder_id: str, name: str) -> bool:
|
2023-05-09 19:45:03 +09:00
|
|
|
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
|
2023-05-09 18:38:58 +09:00
|
|
|
url = self.ENTRYPOINT + folder.id + "/move/"
|
2023-07-26 19:59:07 +09:00
|
|
|
data: dict[str, str | int] = {"parent": folder_id, "name": name}
|
2023-05-09 18:38:58 +09:00
|
|
|
token_check(self.connection)
|
2023-05-10 14:46:08 +09:00
|
|
|
response = self.connection.post(url, data=data)
|
2023-05-09 18:38:58 +09:00
|
|
|
self._raise_response_error(response)
|
|
|
|
return True
|
|
|
|
|
2023-07-20 11:43:07 +09:00
|
|
|
def copy(self, folder: FolderSimple, folder_id: str, name: str) -> bool:
|
|
|
|
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
|
|
|
|
url = self.ENTRYPOINT + folder.id + "/copy/"
|
|
|
|
data: dict[str, str | int] = {"parent": folder_id, "name": name}
|
|
|
|
token_check(self.connection)
|
|
|
|
response = self.connection.post(url, data=data)
|
|
|
|
self._raise_response_error(response)
|
|
|
|
return True
|
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
def metadata(self, id: str) -> dict[str, Any]:
|
2023-05-09 19:45:03 +09:00
|
|
|
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
|
2023-05-01 20:00:32 +09:00
|
|
|
url = self.ENTRYPOINT + id + "/metadata/"
|
2023-05-09 13:08:50 +09:00
|
|
|
token_check(self.connection)
|
2023-05-10 14:46:08 +09:00
|
|
|
response = self.connection.get(url)
|
2023-05-01 20:00:32 +09:00
|
|
|
self._raise_response_error(response)
|
|
|
|
return response.json()
|