|
| 1 | +"""REDCap API methods for Project file repository""" |
| 2 | + |
| 3 | +from typing import Any, Dict, IO, Literal, Optional, Union, cast |
| 4 | + |
| 5 | +from redcap.methods.base import Base, FileMap, Json |
| 6 | +from redcap.request import EmptyJson, FileUpload |
| 7 | + |
| 8 | + |
| 9 | +class FileRepository(Base): |
| 10 | + """Responsible for all API methods under 'File Repository' in the API Playground""" |
| 11 | + |
| 12 | + def create_folder_in_repository( |
| 13 | + self, |
| 14 | + name: str, |
| 15 | + folder_id: Optional[int] = None, |
| 16 | + dag_id: Optional[int] = None, |
| 17 | + role_id: Optional[int] = None, |
| 18 | + format_type: Literal["json", "csv", "xml"] = "json", |
| 19 | + return_format_type: Literal["json", "csv", "xml"] = "json", |
| 20 | + ): |
| 21 | + """ |
| 22 | + Create a New Folder in the File Repository |
| 23 | +
|
| 24 | + Args: |
| 25 | + name: |
| 26 | + The desired name of the folder to be created (max length = 150 characters) |
| 27 | + folder_id: |
| 28 | + The folder_id of a specific folder in the File Repository for which you wish |
| 29 | + to create this sub-folder. If none is provided, the folder will be created in |
| 30 | + the top-level directory of the File Repository. |
| 31 | + dag_id: |
| 32 | + The dag_id of the DAG (Data Access Group) to which you wish to restrict |
| 33 | + access for this folder. If none is provided, the folder will accessible to |
| 34 | + users in all DAGs and users in no DAGs. |
| 35 | + role_id: |
| 36 | + The role_id of the User Role to which you wish to restrict access for this |
| 37 | + folder. If none is provided, the folder will accessible to users in all |
| 38 | + User Roles and users in no User Roles. |
| 39 | + format_type: |
| 40 | + Return the metadata in native objects, csv or xml. |
| 41 | + return_format_type: |
| 42 | + Response format. By default, response will be json-decoded. |
| 43 | + Returns: |
| 44 | + Union[str, List[Dict[str, Any]]]: |
| 45 | + List of all changes made to this project, including data exports, |
| 46 | + data changes, and the creation or deletion of users |
| 47 | +
|
| 48 | + Examples: |
| 49 | + >>> proj.create_folder_in_repository(name="New Folder") |
| 50 | + [{'folder_id': ...}] |
| 51 | + """ |
| 52 | + payload: Dict[str, Any] = self._initialize_payload( |
| 53 | + content="fileRepository", |
| 54 | + format_type=format_type, |
| 55 | + return_format_type=return_format_type, |
| 56 | + ) |
| 57 | + |
| 58 | + payload["action"] = "createFolder" |
| 59 | + payload["name"] = name |
| 60 | + |
| 61 | + if folder_id: |
| 62 | + payload["folder_id"] = folder_id |
| 63 | + |
| 64 | + if dag_id: |
| 65 | + payload["dag_id"] = dag_id |
| 66 | + |
| 67 | + if role_id: |
| 68 | + payload["role_id"] = role_id |
| 69 | + |
| 70 | + return_type = self._lookup_return_type(format_type, request_type="export") |
| 71 | + |
| 72 | + return cast(Union[Json, str], self._call_api(payload, return_type)) |
| 73 | + |
| 74 | + def export_file_repository( |
| 75 | + self, |
| 76 | + folder_id: Optional[int] = None, |
| 77 | + format_type: Literal["json", "csv", "xml"] = "json", |
| 78 | + return_format_type: Literal["json", "csv", "xml"] = "json", |
| 79 | + ): |
| 80 | + """ |
| 81 | + Export of list of files/folders in the File Repository |
| 82 | +
|
| 83 | + Only exports the top-level of files/folders. To see which files are contained |
| 84 | + within a folder, use the `folder_id` parameter |
| 85 | +
|
| 86 | + Args: |
| 87 | + folder_id: |
| 88 | + The folder_id of a specific folder in the File Repository for which you wish |
| 89 | + to search for files/folders. If none is provided, the search will be conducted |
| 90 | + in the top-level directory of the File Repository. |
| 91 | + format_type: |
| 92 | + Return the metadata in native objects, csv or xml. |
| 93 | + return_format_type: |
| 94 | + Response format. By default, response will be json-decoded. |
| 95 | + Returns: |
| 96 | + Union[str, List[Dict[str, Any]]]: |
| 97 | + List of all changes made to this project, including data exports, |
| 98 | + data changes, and the creation or deletion of users |
| 99 | +
|
| 100 | + Examples: |
| 101 | + >>> proj.export_file_repository() |
| 102 | + [{'folder_id': ..., 'name': 'New Folder'}, ...] |
| 103 | + """ |
| 104 | + payload: Dict[str, Any] = self._initialize_payload( |
| 105 | + content="fileRepository", |
| 106 | + format_type=format_type, |
| 107 | + return_format_type=return_format_type, |
| 108 | + ) |
| 109 | + |
| 110 | + payload["action"] = "list" |
| 111 | + |
| 112 | + if folder_id: |
| 113 | + payload["folder_id"] = folder_id |
| 114 | + |
| 115 | + return_type = self._lookup_return_type(format_type, request_type="export") |
| 116 | + |
| 117 | + return cast(Union[Json, str], self._call_api(payload, return_type)) |
| 118 | + |
| 119 | + def export_file_from_repository( |
| 120 | + self, |
| 121 | + doc_id: int, |
| 122 | + return_format_type: Literal["json", "csv", "xml"] = "json", |
| 123 | + ) -> FileMap: |
| 124 | + """ |
| 125 | + Export the contents of a file stored in the File Repository |
| 126 | +
|
| 127 | + Args: |
| 128 | + doc_id: The doc_id of the file in the File Repository |
| 129 | + return_format_type: |
| 130 | + Response format. By default, response will be json-decoded. |
| 131 | +
|
| 132 | + Returns: |
| 133 | + Content of the file and content-type dictionary |
| 134 | +
|
| 135 | + Examples: |
| 136 | + >>> file_dir = proj.export_file_repository() |
| 137 | + >>> text_file = [file for file in file_dir if file["name"] == "test.txt"].pop() |
| 138 | + >>> proj.export_file_from_repository(doc_id=text_file["doc_id"]) |
| 139 | + (b'hello', {'name': 'test.txt', 'charset': 'UTF-8'}) |
| 140 | + """ |
| 141 | + payload = self._initialize_payload( |
| 142 | + content="fileRepository", return_format_type=return_format_type |
| 143 | + ) |
| 144 | + # there's no format field in this call |
| 145 | + payload["action"] = "export" |
| 146 | + payload["doc_id"] = doc_id |
| 147 | + |
| 148 | + content, headers = cast( |
| 149 | + FileMap, self._call_api(payload=payload, return_type="file_map") |
| 150 | + ) |
| 151 | + # REDCap adds some useful things in content-type |
| 152 | + content_map = {} |
| 153 | + if "content-type" in headers: |
| 154 | + splat = [ |
| 155 | + key_values.strip() for key_values in headers["content-type"].split(";") |
| 156 | + ] |
| 157 | + key_values = [ |
| 158 | + (key_values.split("=")[0], key_values.split("=")[1].replace('"', "")) |
| 159 | + for key_values in splat |
| 160 | + if "=" in key_values |
| 161 | + ] |
| 162 | + content_map = dict(key_values) |
| 163 | + |
| 164 | + return content, content_map |
| 165 | + |
| 166 | + def import_file_into_repository( |
| 167 | + self, |
| 168 | + file_name: str, |
| 169 | + file_object: IO, |
| 170 | + folder_id: Optional[int] = None, |
| 171 | + ) -> EmptyJson: |
| 172 | + """ |
| 173 | + Import the contents of a file represented by file_object into |
| 174 | + the file repository |
| 175 | +
|
| 176 | + Args: |
| 177 | + file_name: File name visible in REDCap UI |
| 178 | + file_object: File object as returned by `open` |
| 179 | + folder_id: |
| 180 | + The folder_id of a specific folder in the File Repository where |
| 181 | + you wish to store the file. If none is provided, the file will |
| 182 | + be stored in the top-level directory of the File Repository. |
| 183 | +
|
| 184 | + Returns: |
| 185 | + Empty JSON object |
| 186 | +
|
| 187 | + Examples: |
| 188 | + >>> import tempfile |
| 189 | + >>> tmp_file = tempfile.TemporaryFile() |
| 190 | + >>> proj.import_file_into_repository( |
| 191 | + ... file_name="myupload.txt", |
| 192 | + ... file_object=tmp_file, |
| 193 | + ... ) |
| 194 | + [{}] |
| 195 | + """ |
| 196 | + payload: Dict[str, Any] = self._initialize_payload(content="fileRepository") |
| 197 | + payload["action"] = "import" |
| 198 | + |
| 199 | + if folder_id: |
| 200 | + payload["folder_id"] = folder_id |
| 201 | + |
| 202 | + file_upload_dict: FileUpload = {"file": (file_name, file_object)} |
| 203 | + |
| 204 | + return cast( |
| 205 | + EmptyJson, |
| 206 | + self._call_api( |
| 207 | + payload=payload, return_type="empty_json", file=file_upload_dict |
| 208 | + ), |
| 209 | + ) |
| 210 | + |
| 211 | + def delete_file_from_repository( |
| 212 | + self, |
| 213 | + doc_id: int, |
| 214 | + return_format_type: Literal["json", "csv", "xml"] = "json", |
| 215 | + ) -> EmptyJson: |
| 216 | + # pylint: disable=line-too-long |
| 217 | + """ |
| 218 | + Delete a File from the File Repository |
| 219 | +
|
| 220 | + Once deleted, the file will remain in the Recycle Bin folder for up to 30 days. |
| 221 | +
|
| 222 | + Args: |
| 223 | + doc_id: The doc_id of the file in the File Repository |
| 224 | + return_format_type: |
| 225 | + Response format. By default, response will be json-decoded. |
| 226 | +
|
| 227 | + Returns: |
| 228 | + Empty JSON object |
| 229 | +
|
| 230 | + Examples: |
| 231 | + >>> file_dir = proj.export_file_repository() |
| 232 | + >>> test_folder = [folder for folder in file_dir if folder["name"] == "test"].pop() |
| 233 | + >>> test_dir = proj.export_file_repository(folder_id=test_folder["folder_id"]) |
| 234 | + >>> test_file = [file for file in test_dir if file["name"] == "test_in_folder.txt"].pop() |
| 235 | + >>> proj.delete_file_from_repository(doc_id=test_file["doc_id"]) |
| 236 | + [{}] |
| 237 | + """ |
| 238 | + # pylint: enable=line-too-long |
| 239 | + payload = self._initialize_payload( |
| 240 | + content="fileRepository", return_format_type=return_format_type |
| 241 | + ) |
| 242 | + # there's no format field in this call |
| 243 | + payload["action"] = "delete" |
| 244 | + payload["doc_id"] = doc_id |
| 245 | + |
| 246 | + return cast( |
| 247 | + EmptyJson, self._call_api(payload=payload, return_type="empty_json") |
| 248 | + ) |
0 commit comments