@@ -147,6 +147,18 @@ def _batch_update_row_link_server_url(self):
147147
148148 def _app_download_link_url (self ):
149149 return self .server_url + '/api/v2.1/dtable/app-download-link/'
150+
151+ def _app_custom_download_link_url (self ):
152+ return self .server_url + '/api/v2.1/dtable/custom/app-download-link/'
153+
154+ def _app_custom_asset_file_url (self ):
155+ return self .server_url + '/api/v2.1/dtable/custom/app-asset-file/'
156+
157+ def _app_custom_asset_dir_url (self ):
158+ return self .server_url + '/api/v2.1/dtable/custom/app-asset-dir/'
159+
160+ def _app_custom_upload_link_url (self ):
161+ return self .server_url + '/api/v2.1/dtable/custom/app-upload-link/'
150162
151163 def _app_upload_link_url (self ):
152164 return self .server_url + '/api/v2.1/dtable/app-upload-link/'
@@ -955,6 +967,87 @@ def big_data_insert_rows(self, table_name, rows_data):
955967 return parse_response (response )
956968
957969
970+ #### custom assets ######
971+ def get_custom_file_download_link (self , path ):
972+ """
973+ :param path: str
974+ :return: str
975+ """
976+ url = self ._app_custom_download_link_url ()
977+ params = {'path' : path }
978+ headers = parse_headers (self .token )
979+ response = requests .get (url , params = params , headers = headers , timeout = self .timeout )
980+ data = parse_response (response )
981+ return data .get ('download_link' )
982+
983+ def get_custom_file_upload_link (self , path ):
984+ url = self ._app_custom_upload_link_url ()
985+ params = {'path' : path }
986+ headers = parse_headers (self .token )
987+ response = requests .get (url , params = params , headers = headers , timeout = self .timeout )
988+ data = parse_response (response )
989+ return data
990+
991+
992+ def download_custom_file (self , path , save_path ):
993+ download_link = self .get_custom_file_download_link (parse .unquote (path ))
994+ response = requests .get (download_link , timeout = self .timeout )
995+ if response .status_code != 200 :
996+ raise Exception ('download file error' )
997+ with open (save_path , 'wb' ) as f :
998+ f .write (response .content )
999+
1000+ def upload_local_file_to_custom_folder (self , local_path , custom_folder_path = None , name = None , ):
1001+ if not name :
1002+ name = local_path .strip ('/' ).split ('/' )[- 1 ]
1003+ if not custom_folder_path :
1004+ custom_folder_path = '/'
1005+
1006+ upload_link_dict = self .get_custom_file_upload_link (parse .unquote (custom_folder_path ))
1007+ upload_link = upload_link_dict .get ('upload_link' ) + '?ret-json=1'
1008+ parent_path = upload_link_dict .get ('parent_path' )
1009+ relative_path = upload_link_dict .get ('relative_path' )
1010+
1011+ response = requests .post (upload_link , data = {
1012+ 'parent_dir' : parent_path ,
1013+ 'relative_path' : relative_path ,
1014+ 'replace' : 0
1015+ }, files = {
1016+ 'file' : (name , open (local_path , 'rb' ))
1017+ }, timeout = self .timeout )
1018+ d = response .json ()[0 ]
1019+
1020+ file_name = d .get ('name' )
1021+ return self .get_custom_file_info (custom_folder_path , file_name )
1022+
1023+
1024+ def get_custom_file_info (self , path , name ):
1025+ url = self ._app_custom_asset_file_url ()
1026+ params = {'path' : path , 'name' : name }
1027+ headers = parse_headers (self .token )
1028+ response = requests .get (url , params = params , headers = headers , timeout = self .timeout )
1029+ data = parse_response (response )
1030+ d = data ['dirent' ]
1031+ file_name = d .get ('obj_name' )
1032+ file_name_ext = file_name .split ('.' )[- 1 ]
1033+ asset_uuid = d .get ('uuid' )
1034+
1035+ return {
1036+ 'type' : 'file' ,
1037+ 'size' : d .get ('file_size' ),
1038+ 'name' : d .get ('obj_name' ),
1039+ 'url' : 'custom-asset://%s.%s' % (asset_uuid , file_name_ext )
1040+ }
1041+
1042+ def list_custom_assets (self , path ):
1043+ url = self ._app_custom_asset_dir_url ()
1044+ params = {'path' : path }
1045+ headers = parse_headers (self .token )
1046+ response = requests .get (url , params = params , headers = headers , timeout = self .timeout )
1047+ data = parse_response (response )
1048+ return data
1049+
1050+
9581051
9591052class Account (object ):
9601053 def __init__ (self , login_name , password , server_url ):
0 commit comments