mdrs-client-python/mdrsclient/api/folders.py

122 lines
5.0 KiB
Python
Raw Normal View History

from typing import Any, Final
2023-05-01 20:00:32 +09:00
2023-06-01 16:52:41 +09:00
import requests
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
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
params: dict[str, str | int] = {"path": path, "laboratory_id": laboratory_id}
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(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 + "/"
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 = 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
data: dict[str, str | int] = {"name": name, "parent_id": parent_id, "description": "", "template_id": -1}
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)
url = self.ENTRYPOINT + folder.id + "/"
data: dict[str, str | int] = {
2023-05-01 20:00:32 +09:00
"name": folder.name,
"description": folder.description,
}
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
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 + "/"
params: dict[str, str | int] = {"recursive": str(recursive)}
token_check(self.connection)
response = self.connection.delete(url, params=params)
2023-05-01 20:00:32 +09:00
self._raise_response_error(response)
return True
def auth(self, id: str, password: str) -> bool:
# print(self.__class__.__name__ + "::" + sys._getframe().f_code.co_name)
url = self.ENTRYPOINT + id + "/auth/"
data: dict[str, str | int] = {"password": password}
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:
raise UnauthorizedException("Password is incorrect.")
2023-06-01 16:52:41 +09:00
self._raise_response_error(response)
return True
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, str | int] = {"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
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)
url = self.ENTRYPOINT + folder.id + "/move/"
data: dict[str, str | int] = {"parent": folder_id, "name": name}
token_check(self.connection)
2023-05-10 14:46:08 +09:00
response = self.connection.post(url, data=data)
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
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/"
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()