2023-05-01 20:00:32 +09:00
|
|
|
from typing import Final
|
|
|
|
|
|
|
|
from pydantic import parse_obj_as
|
|
|
|
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)
|
|
|
|
class FolderCreateResponse:
|
|
|
|
id: str
|
|
|
|
|
|
|
|
|
|
|
|
class FolderApi(BaseApi):
|
|
|
|
ENTRYPOINT: Final[str] = "v2/folder/"
|
|
|
|
|
|
|
|
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
|
|
|
|
params = {"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():
|
|
|
|
ret.append(parse_obj_as(FolderSimple, data))
|
|
|
|
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)
|
|
|
|
ret = parse_obj_as(Folder, response.json())
|
|
|
|
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
|
|
|
|
data = {"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)
|
|
|
|
ret = parse_obj_as(FolderCreateResponse, response.json())
|
|
|
|
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-05-01 20:00:32 +09:00
|
|
|
data = {
|
|
|
|
"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-05-26 17:58:25 +09:00
|
|
|
params = {"recursive": 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/"
|
|
|
|
data = {"password": password}
|
|
|
|
token_check(self.connection)
|
|
|
|
response = self.connection.post(url, data=data)
|
|
|
|
# stupid! always returned status code 200
|
|
|
|
self._raise_response_error(response)
|
|
|
|
data = response.json()
|
|
|
|
if data.get("status") is not True:
|
|
|
|
raise UnauthorizedException("Password is incorrect.")
|
|
|
|
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/"
|
|
|
|
data: dict[str, int | str] = {"access_level": access_level}
|
|
|
|
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-05-09 18:38:58 +09:00
|
|
|
def move(self, folder: FolderSimple, folder_id: 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/"
|
|
|
|
data = {"parent": folder_id}
|
|
|
|
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-05-01 20:00:32 +09:00
|
|
|
def metadata(self, id: str) -> dict:
|
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()
|