|
1 | 1 | from functools import wraps |
2 | | -from typing import List |
| 2 | +from typing import List, Union |
3 | 3 | import io |
4 | 4 |
|
5 | 5 | import requests |
@@ -128,28 +128,72 @@ def objects_tree(self, item=''): |
128 | 128 |
|
129 | 129 | return pd.DataFrame(r.json()) |
130 | 130 |
|
131 | | - @_try_relogin |
132 | | - def upload_file(self, name: str, df: pd.DataFrame): |
133 | | - |
134 | | - # convert to file |
| 131 | + @staticmethod |
| 132 | + def read_file_as_bytes(file_path: str): |
| 133 | + """ |
| 134 | + Read and return content of a file in bytes, given its path. |
| 135 | + :param file_path: Path of the file to read. |
| 136 | + :return: File content in bytes. |
| 137 | + """ |
| 138 | + try: |
| 139 | + with open(file_path, 'rb+') as file: |
| 140 | + return file.read() |
| 141 | + except FileNotFoundError: |
| 142 | + raise Exception(f'File {file_path} does not exist.') |
| 143 | + except PermissionError: |
| 144 | + raise Exception(f'Permission denied when reading file {file_path}.') |
| 145 | + except Exception as e: |
| 146 | + raise Exception(f'Unknown error occurred when reading file {file_path} - {str(e)}') |
| 147 | + @staticmethod |
| 148 | + def read_dataframe_as_csv(data: pd.DataFrame): |
| 149 | + """ |
| 150 | + Read and return content of a DataFrame as CSV in bytes. |
| 151 | + :param data: DataFrame to read. |
| 152 | + :return: DataFrame content as CSV in bytes. |
| 153 | + """ |
135 | 154 | fd = io.BytesIO() |
136 | | - df.to_csv(fd, index=False) |
| 155 | + data.to_csv(fd, index=False) |
137 | 156 | fd.seek(0) |
| 157 | + return fd.read() |
| 158 | + |
| 159 | + def upload_data(self, file_name: str, data: bytes): |
| 160 | + """ |
| 161 | + Upload binary data to MindsDB. |
| 162 | + :param file_name: Name of the file. |
| 163 | + :param data: Binary data to upload. |
| 164 | + """ |
| 165 | + # remove suffix from file if present |
| 166 | + name = file_name.split('.')[0] |
138 | 167 |
|
139 | 168 | url = self.url + f'/api/files/{name}' |
140 | 169 | r = self.session.put( |
141 | 170 | url, |
142 | 171 | data={ |
143 | | - 'source': name, |
144 | | - 'name': name, |
145 | | - 'source_type': 'file', |
| 172 | + 'original_file_name':file_name, |
| 173 | + 'name':name, |
| 174 | + 'source_type':'file', |
146 | 175 | }, |
147 | 176 | files={ |
148 | | - 'file': fd, |
| 177 | + 'file': (file_name, data) |
| 178 | + |
149 | 179 | } |
150 | 180 | ) |
151 | 181 | _raise_for_status(r) |
152 | 182 |
|
| 183 | + @_try_relogin |
| 184 | + def upload_file(self, name: str, data: Union[pd.DataFrame, str]): |
| 185 | + """ |
| 186 | + Upload a file or a DataFrame to MindsDB. |
| 187 | + :param name: Name of the file or DataFrame. |
| 188 | + :param data: DataFrame data or file path. |
| 189 | + """ |
| 190 | + if isinstance(data, pd.DataFrame): |
| 191 | + data_in_bytes = self.read_dataframe_as_csv(data) |
| 192 | + else: |
| 193 | + data_in_bytes = self.read_file_as_bytes(data) |
| 194 | + |
| 195 | + self.upload_data(name, data_in_bytes) |
| 196 | + |
153 | 197 | @_try_relogin |
154 | 198 | def get_file_metadata(self, name: str) -> dict: |
155 | 199 | # No endpoint currently to get single file. |
|
0 commit comments