-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path001-manipulating-metadata.py
executable file
·49 lines (42 loc) · 1.8 KB
/
001-manipulating-metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
###################################################################################################
# In this example script, we show you how to manage extra fields through the "metadata" attribute #
###################################################################################################
from pprint import pprint
import elabapi_python
# use the locally defined client.py module to get the api_client object, fully configured and ready to be used to instantiate api objects
from client import api_client
# Note:
# In order to make it easier to run only specific parts, the parts are grouped in functions that are called at the end of the script
######################################################
# Part 1: creating a Resource with specific metadata #
######################################################
# Doc: https://doc.elabftw.net/api/v2/#/Items/post-item
# Start by creating our api object to interact with /items endpoint
items_client = elabapi_python.ItemsApi(api_client)
# Now define our metadata: in this case, two fields of type number, with units
metadata = {
"extra_fields": {
"Laser power": {
"type": "number",
"value": "50",
"units": ["mW", "W", "MW"],
"unit": "MW",
},
"Illumination time": {
"type": "number",
"value": "85",
"units": ["min", "sec", "hour"],
"unit": "min",
},
},
}
# now create a Resource with this metadata
data = {
"title": "Resource with metadata created from API",
"metadata": metadata,
}
response_data, status_code, headers = items_client.post_item_with_http_info(body=data)
item_id = int(headers.get('Location').split('/').pop())
if status_code == 201:
print(f"[*] We created a Resource with ID: {item_id}")