@@ -117,21 +117,23 @@ def format_filename(filename: str) -> str:
117
117
118
118
119
119
def upload_resource_name (filename : str , is_dir : bool , group : Optional [str ] = None , remote_path : Optional [str ] = None ) -> str :
120
- if not group :
121
- group = ""
122
120
if not is_dir :
123
121
debug_step ('uploading file' )
124
- resource = "/" / pathlib .Path (quote (format_filename (filename )))
122
+ resource = pathlib .Path (quote (format_filename (filename )))
125
123
if remote_path :
126
124
resource = quote (remote_path ) / resource
127
125
if group :
128
126
resource = group / resource
129
127
else :
130
128
debug_step ('uploading directory (file)' )
131
- if remote_path :
132
- resource = pathlib .Path ("/" ) / group / quote ( remote_path ) / quote ( filename )
129
+ if filename . startswith ( '/' ) :
130
+ resource = pathlib .Path (filename [ 1 :] )
133
131
else :
134
- resource = pathlib .Path ("/" ) / group / quote (filename )
132
+ resource = pathlib .Path (filename )
133
+ if remote_path :
134
+ resource = quote (remote_path ) / resource
135
+ if group :
136
+ resource = group / resource
135
137
return str (resource )
136
138
137
139
@@ -253,7 +255,7 @@ def streamfile(
253
255
tokens = maybe_refresh (env , pnum , api_key , token , refresh_token , refresh_target )
254
256
token = tokens .get ('access_token' ) if tokens else token
255
257
resource = upload_resource_name (filename , is_dir , group = group , remote_path = remote_path )
256
- endpoint = str ( pathlib . Path ( "stream" ) / f" { resource } ?group={ group } ")
258
+ endpoint = f "stream/ { resource } ?group={ group } "
257
259
url = f'{ file_api_url (env , pnum , backend , endpoint = endpoint )} '
258
260
headers = {'Authorization' : f'Bearer { token } ' }
259
261
debug_step (f'streaming data to { url } ' )
@@ -335,9 +337,9 @@ def import_list(
335
337
if not group :
336
338
group = ""
337
339
if remote_path :
338
- endpoint = str (pathlib .Path ("stream" ) / group / quote (remote_path ) / quote ( resource ) )
340
+ endpoint = str (pathlib .Path ("stream" ) / group / quote (remote_path ) / resource )
339
341
else :
340
- endpoint = str (pathlib .Path ("stream" ) / group / quote ( resource ) )
342
+ endpoint = str (pathlib .Path ("stream" ) / group / resource )
341
343
url = f'{ file_api_url (env , pnum , backend , endpoint = endpoint , page = page , per_page = per_page )} '
342
344
headers = {'Authorization' : 'Bearer {0}' .format (token )}
343
345
debug_step (f'listing resources at { url } ' )
@@ -376,7 +378,7 @@ def survey_list(
376
378
per_page: number of files to list per page
377
379
378
380
"""
379
- endpoint = str ( pathlib . Path ( directory or "" ) / " attachments")
381
+ endpoint = f" { directory } / attachments"
380
382
url = f'{ file_api_url (env , pnum , backend , endpoint = endpoint , page = page , per_page = per_page )} '
381
383
headers = {'Authorization' : 'Bearer {0}' .format (token )}
382
384
debug_step (f'listing resources at { url } ' )
@@ -403,12 +405,10 @@ def import_delete(
403
405
) -> requests .Response :
404
406
tokens = maybe_refresh (env , pnum , api_key , token , refresh_token , refresh_target )
405
407
token = tokens .get ("access_token" ) if tokens else token
406
- endpoint = pathlib .Path ("stream" )
407
- if group :
408
- endpoint = endpoint / group
409
408
if remote_path :
410
- endpoint = endpoint / quote (remote_path )
411
- endpoint = str (endpoint / quote (filename ))
409
+ endpoint = f'stream/{ group } { quote (remote_path )} { quote (filename )} '
410
+ else :
411
+ endpoint = f'stream/{ group } { quote (filename )} '
412
412
url = f'{ file_api_url (env , pnum , "files" , endpoint = endpoint )} '
413
413
headers = {'Authorization' : f'Bearer { token } ' }
414
414
print (f'deleting: { filename } ' )
@@ -431,10 +431,10 @@ def export_delete(
431
431
) -> requests .Response :
432
432
tokens = maybe_refresh (env , pnum , api_key , token , refresh_token , refresh_target )
433
433
token = tokens .get ("access_token" ) if tokens else token
434
- endpoint = pathlib .Path ("export" )
435
434
if remote_path :
436
- endpoint = endpoint / quote (remote_path )
437
- endpoint = str (endpoint / quote (filename ))
435
+ endpoint = f'export{ quote (remote_path )} { quote (filename )} '
436
+ else :
437
+ endpoint = f'export/{ quote (filename )} '
438
438
url = f'{ file_api_url (env , pnum , "files" , endpoint = endpoint )} '
439
439
headers = {'Authorization' : f'Bearer { token } ' }
440
440
print (f'deleting: { filename } ' )
@@ -491,10 +491,9 @@ def export_list(
491
491
sys .exit (f'{ remote_path } is a file, not a directory' )
492
492
if not exists :
493
493
sys .exit (f'{ remote_path } does not exist' )
494
- endpoint = pathlib .Path ("export" )
495
- if remote_path :
496
- endpoint = endpoint / quote (remote_path )
497
- endpoint = str (endpoint / resource )
494
+ endpoint = f"export{ quote (remote_path )} { resource } "
495
+ else :
496
+ endpoint = f'export/{ resource } '
498
497
url = f'{ file_api_url (env , pnum , backend , endpoint = endpoint , page = page , per_page = per_page )} '
499
498
headers = {'Authorization' : 'Bearer {0}' .format (token )}
500
499
debug_step (f'listing resources at { url } ' )
@@ -516,10 +515,10 @@ def export_head(
516
515
remote_path : Optional [str ] = None ,
517
516
) -> requests .Response :
518
517
headers = {'Authorization' : 'Bearer {0}' .format (token ), "Accept-Encoding" : "*" }
519
- endpoint = pathlib .Path ("export" )
520
518
if remote_path :
521
- endpoint = endpoint / quote (remote_path )
522
- endpoint = str (endpoint / quote (filename ))
519
+ endpoint = f"export{ quote (remote_path )} { quote (filename )} "
520
+ else :
521
+ endpoint = f'export/{ quote (filename )} '
523
522
url = f'{ file_api_url (env , pnum , backend , endpoint = endpoint )} '
524
523
resp = session .head (url , headers = headers )
525
524
return resp
@@ -589,12 +588,13 @@ def export_get(
589
588
url = dev_url
590
589
else :
591
590
if backend == 'survey' :
592
- urlpath = pathlib . Path ( "" )
591
+ urlpath = ''
593
592
else :
594
- urlpath = pathlib .Path ("export" )
595
593
if remote_path :
596
- urlpath = urlpath / quote (remote_path )
597
- endpoint = str (urlpath / filename )
594
+ urlpath = f"export{ quote (remote_path )} "
595
+ else :
596
+ urlpath = 'export/'
597
+ endpoint = f'{ urlpath } { filename } '
598
598
# make provision for unsatisfactory semantics
599
599
if backend in ['export' , 'files' ]:
600
600
service = 'files'
@@ -671,7 +671,7 @@ def _resumable_url(
671
671
) -> str :
672
672
resource = upload_resource_name (filename , is_dir , group = group , remote_path = remote_path )
673
673
if not dev_url :
674
- endpoint = str ( pathlib . Path ( "stream" ) / resource )
674
+ endpoint = f "stream/ { resource } "
675
675
url = f'{ file_api_url (env , pnum , backend , endpoint = endpoint )} '
676
676
else :
677
677
url = dev_url
@@ -1124,8 +1124,8 @@ def delete_resumable(
1124
1124
if dev_url :
1125
1125
url = dev_url
1126
1126
else :
1127
- filename = quote (format_filename (filename )) if filename else ''
1128
- endpoint = str ( pathlib . Path ( 'resumables' ) / f' { filename } ?id={ upload_id } ')
1127
+ filename = f'/ { quote (format_filename (filename ))} ' if filename else ''
1128
+ endpoint = f'resumables { filename } ?id={ upload_id } '
1129
1129
url = f'{ file_api_url (env , pnum , backend , endpoint = endpoint )} '
1130
1130
debug_step (f'deleting { filename } using: { url } ' )
1131
1131
resp = session .delete (url , headers = {'Authorization' : 'Bearer {0}' .format (token )})
0 commit comments