Skip to content

Commit 08e3189

Browse files
committed
Merge branch 'master' of github.com:elabftw/elabapi-python
* 'master' of github.com:elabftw/elabapi-python: examples: JSON-ify 'metadata' explicitly (#48) feat: fetch openapi.yaml file from release assets (#49)
2 parents 71ae089 + b60fdcc commit 08e3189

2 files changed

Lines changed: 60 additions & 8 deletions

File tree

examples/02-patch-metadata-per-category.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/bin/env python
2+
import json
3+
from json import JSONDecodeError
4+
25
import elabapi_python
36
from client import api_client
47

@@ -11,10 +14,45 @@
1114
# create an instance of Items api
1215
itemsApi = elabapi_python.ItemsApi(api_client)
1316

14-
metadata = '{"elabftw": {"extra_fields_groups": [{"id": 1,"name": "Drug settings"},{"id": 2,"name": "Mice info"}]},"extra_fields": {"Drug addition": {"type": "datetime-local","value": "","group_id": 1,"required": true,"description": "Time when drug is added"},"Drug concentration": {"type": "number","unit": "mM","units": ["mM","μM","nM"],"value": "","group_id": 1,"required": true},"Mouse sex": {"type": "select","value": "Male","options": ["Male","Female"],"group_id": 2}}}'
17+
metadata = {
18+
"elabftw": {
19+
"extra_fields_groups": [
20+
{"id": 1, "name": "Drug settings"},
21+
{"id": 2, "name": "Mice info"},
22+
]
23+
},
24+
"extra_fields": {
25+
"Drug addition": {
26+
"type": "datetime-local",
27+
"value": "",
28+
"group_id": 1,
29+
"required": True,
30+
"description": "Time when drug is added",
31+
},
32+
"Drug concentration": {
33+
"type": "number",
34+
"unit": "mM",
35+
"units": ["mM", "μM", "nM"],
36+
"value": "",
37+
"group_id": 1,
38+
"required": True,
39+
},
40+
"Mouse sex": {
41+
"type": "select",
42+
"value": "Male",
43+
"options": ["Male", "Female"],
44+
"group_id": 2,
45+
},
46+
},
47+
}
1548

1649
for item in itemsApi.read_items(cat=RESOURCE_CATEGORY_ID, limit=9999):
1750
# skip items with metadata already
1851
if not item.metadata:
19-
print(f'Patching item {item.id}')
20-
itemsApi.patch_item(item.id, body={'metadata': metadata})
52+
print(f"Patching item {item.id}")
53+
try:
54+
itemsApi.patch_item(item.id, body={"metadata": json.dumps(metadata)})
55+
except JSONDecodeError as e:
56+
raise ValueError(
57+
"'metadata' passed to API request body contains invalid JSON."
58+
) from e

helper.sh

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,26 @@ else
2121
generator_flag="-g"
2222
fi
2323

24-
# where to grab the definition file
25-
openapi_yaml_url="https://raw.githubusercontent.com/elabftw/elabftw/master/apidoc/v2/openapi.yaml"
2624
# folder with the generated python code
2725
lib="generated"
2826
html="html"
2927

28+
# we fetch the openapi.yaml file from release
29+
gh_api_release_url="https://api.github.com/repos/elabftw/elabftw/releases/latest"
30+
31+
browser_url="$(
32+
curl -fsSL \
33+
-H "Accept: application/vnd.github+json" \
34+
-H "User-Agent: elabftw-script" \
35+
"$gh_api_release_url" \
36+
| jq -r '.assets[] | select(.name == "openapi.yaml") | .browser_download_url'
37+
)"
38+
39+
if [ -z "$browser_url" ] || [ "$browser_url" = "null" ]; then
40+
echo "openapi.yaml not found in latest release assets" >&2
41+
exit 1
42+
fi
43+
3044
function cleanup {
3145
rm -rfv "$lib"
3246
rm -rfv "$html"
@@ -35,17 +49,17 @@ function cleanup {
3549
# generate the lib from remote spec
3650
function generate {
3751
cleanup
38-
docker run --user "$(id -u)":"$(id -u)" --rm -v "${PWD}":/local "$docker_image" generate -i "$openapi_yaml_url" "$generator_flag" python -o /local/"$lib" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
52+
docker run --user "$(id -u)":"$(id -u)" --rm -v "${PWD}":/local "$docker_image" generate -i "$browser_url" "$generator_flag" python -o /local/"$lib" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
3953
}
4054

4155
function generate-html {
4256
cleanup
43-
docker run --user "$(id -u)":"$(id -u)" --rm -v "${PWD}":/local "$docker_image" generate -i "$openapi_yaml_url" "$generator_flag" html2 -o /local/"$html" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
57+
docker run --user "$(id -u)":"$(id -u)" --rm -v "${PWD}":/local "$docker_image" generate -i "$browser_url" "$generator_flag" html2 -o /local/"$html" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
4458
}
4559

4660
# don't use user/group ids in GH actions
4761
function generate-ci {
48-
docker run --rm -v "${PWD}":/local "$docker_image" generate -i "$openapi_yaml_url" "$generator_flag" python -o /local/"$lib" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
62+
docker run --rm -v "${PWD}":/local "$docker_image" generate -i "$browser_url" "$generator_flag" python -o /local/"$lib" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
4963
# fix permissions
5064
sudo chown -R "$(id -u)":"$(id -gn)" "$lib"
5165
}

0 commit comments

Comments
 (0)