|
1 | 1 | #!/usr/bin/env python |
| 2 | + |
| 3 | +################################################################################# |
| 4 | +# In this example script, we will upload a file to an experiment and delete it. # |
| 5 | +# You can comment the 'delete' part to see results in your experiment. # |
| 6 | +################################################################################# |
| 7 | + |
| 8 | +# Doc: https://doc.elabftw.net/api/v2/#/Uploads |
| 9 | + |
| 10 | +# part 1 (upload_file_to_experiment) will upload a file to a target experiment. |
| 11 | +# part 2 (download_matching_uploads_locally) will download locally a file from an eLabFTW experiment. |
| 12 | +# part 3 (cleanup) will remove the files from both the experiment and locally. |
2 | 13 | import os |
3 | 14 | import elabapi_python |
4 | | - |
5 | | -######################### |
6 | | -# CONFIG # |
7 | | -######################### |
8 | | -API_HOST_URL = 'https://elab.local:3148/api/v2' |
9 | | -# replace with your api key |
10 | | -API_KEY = 'apiKey4Test' |
11 | | -######################### |
12 | | -# END CONFIG # |
13 | | -######################### |
14 | | - |
15 | | -# Configure the api client |
16 | | -configuration = elabapi_python.Configuration() |
17 | | -configuration.api_key['api_key'] = API_KEY |
18 | | -configuration.api_key_prefix['api_key'] = 'Authorization' |
19 | | -configuration.host = API_HOST_URL |
20 | | -configuration.debug = False |
21 | | -configuration.verify_ssl = False |
22 | | - |
23 | | -# create an instance of the API class |
24 | | -api_client = elabapi_python.ApiClient(configuration) |
25 | | -# fix issue with Authorization header not being properly set by the generated lib |
26 | | -api_client.set_default_header(header_name='Authorization', header_value=API_KEY) |
| 15 | +from client import api_client |
27 | 16 |
|
28 | 17 | # create an instance of Experiments and another for Uploads |
29 | 18 | experimentsApi = elabapi_python.ExperimentsApi(api_client) |
30 | 19 | uploadsApi = elabapi_python.UploadsApi(api_client) |
31 | 20 |
|
32 | | -# get experiment with ID 256 |
33 | | -exp = experimentsApi.get_experiment(256) |
34 | | -# upload the file 'README.md' present in the current folder |
35 | | -uploadsApi.post_upload('experiments', 256, file='README.md', comment='Uploaded with APIv2') |
36 | | -# display id, name and comment of the uploaded files |
37 | | -for upload in uploadsApi.read_uploads('experiments', exp.id): |
38 | | - print(upload.id, upload.real_name, upload.comment) |
39 | | - # get and save file |
40 | | - with open('README.downloaded_with_api.md', 'wb') as file: |
41 | | - # the _preload_content flag is necessary so the api_client doesn't try and deserialize the response |
42 | | - file.write(uploadsApi.read_upload('experiments', 256, upload.id, format='binary', _preload_content=False).data) |
| 21 | +# define the target experiment |
| 22 | +EXPERIMENT_ID = 210 |
| 23 | +# prepare the file. We'll use the 'README.md' present in current folder |
| 24 | +SOURCE_FILE = "README.md" |
| 25 | +# local downloads for the script |
| 26 | +DOWNLOADED_FILENAME = "README.downloaded_with_api.md" |
| 27 | + |
| 28 | +exp = experimentsApi.get_experiment(EXPERIMENT_ID) |
| 29 | + |
| 30 | +# --- part 1: Upload file to experiment --- |
| 31 | +def upload_file_to_experiment(): |
| 32 | + uploadsApi.post_upload('experiments', EXPERIMENT_ID, file=SOURCE_FILE, comment='Uploaded with APIv2') |
| 33 | + |
| 34 | +# --- part 2: Download uploads locally --- |
| 35 | +def download_matching_uploads_locally(): |
| 36 | + # display id, name and comment of the uploaded files and download them locally |
| 37 | + for upload in uploadsApi.read_uploads('experiments', exp.id): |
| 38 | + print(upload.id, upload.real_name, upload.comment, upload.state) |
| 39 | + # get and save file |
| 40 | + with open(DOWNLOADED_FILENAME, 'wb') as file: |
| 41 | + # the _preload_content flag is necessary so the api_client doesn't try and deserialize the response |
| 42 | + file.write(uploadsApi.read_upload('experiments', EXPERIMENT_ID, upload.id, format='binary', _preload_content=False).data) |
| 43 | + |
| 44 | +# --- part 3: delete the downloaded file --- |
| 45 | +def cleanup(): |
43 | 46 | # delete all the files where the name is 'README.md' |
44 | | - if upload.real_name == 'README.md': |
45 | | - uploadsApi.delete_upload('experiments', 256, upload.id) |
| 47 | + for upload in uploadsApi.read_uploads('experiments', exp.id): |
| 48 | + if upload.real_name == SOURCE_FILE: |
| 49 | + uploadsApi.delete_upload('experiments', EXPERIMENT_ID, upload.id) |
| 50 | + # cleanup: delete the downloaded file |
| 51 | + try: |
| 52 | + os.remove(DOWNLOADED_FILENAME) |
| 53 | + except FileNotFoundError: |
| 54 | + pass |
46 | 55 |
|
47 | | -# cleanup: delete the downloaded file |
48 | | -os.remove('README.downloaded_with_api.md') |
| 56 | +upload_file_to_experiment() |
| 57 | +download_matching_uploads_locally() |
| 58 | +cleanup() |
0 commit comments