Skip to content

Commit 28fb0a4

Browse files
committed
add more examples
1 parent aef2675 commit 28fb0a4

File tree

6 files changed

+130
-4
lines changed

6 files changed

+130
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
generated/
2+
openapi.yaml

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,27 @@ pip install --user elabapi-python
1212

1313
See the [examples folder](./examples).
1414

15+
# Dev
16+
1517
## Using the helper script
1618

1719
~~~bash
1820
# generate the library
1921
./helper.sh generate
22+
# generate from local file: openapi.yaml must be in current dir
23+
./helper.sh generate-from-local
2024
# build packages
2125
./helper.sh build
2226
# publish new version (after editing config.json, with twine installed)
2327
./helper.sh publish
2428
~~~
2529

26-
## License
30+
## Installing the library for dev
31+
32+
~~~bash
33+
cd generated && python setup.py develop --user
34+
~~~
35+
36+
# License
2737

2838
MIT, see [license file](./LICENSE).

examples/01.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
import time
3+
import datetime
4+
import elabapi_python
5+
from elabapi_python.rest import ApiException
6+
7+
#########################
8+
# CONFIG #
9+
#########################
10+
API_HOST_URL = 'https://elab.local:3148/api/v2'
11+
# replace with your api key
12+
API_KEY = 'apiKey4Test'
13+
# number of days to look back
14+
PERIOD_IN_DAYS = 7
15+
#########################
16+
# END CONFIG #
17+
#########################
18+
19+
# Configure the api client
20+
configuration = elabapi_python.Configuration()
21+
configuration.api_key['api_key'] = API_KEY
22+
configuration.api_key_prefix['api_key'] = 'Authorization'
23+
configuration.host = API_HOST_URL
24+
configuration.debug = False
25+
configuration.verify_ssl = False
26+
27+
# create an instance of the API class
28+
api_client = elabapi_python.ApiClient(configuration)
29+
# fix issue with Authorization header not being properly set by the generated lib
30+
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)
31+
32+
# create an instance of Experiments and another for Uploads
33+
experimentsApi = elabapi_python.ExperimentsApi(api_client)
34+
uploadsApi = elabapi_python.UploadsApi(api_client)
35+
36+
# calculate the date
37+
today = datetime.date.today()
38+
date_from = today - datetime.timedelta(days = PERIOD_IN_DAYS)
39+
40+
# look for experiments that are timestamped
41+
for exp in experimentsApi.read_experiments(extended=f'timestamped:yes timestamped_at:>{date_from}'):
42+
for upload in uploadsApi.read_uploads('experiments', exp.id):
43+
# get and save binary file
44+
now = datetime.datetime.now()
45+
with open(f'{exp.id}-{exp.elabid}-{now.strftime("%Y-%m-%d_%H-%M-%S")}-timestamp-archive.zip', 'wb') as zipfile:
46+
# the _preload_content flag is necessary so the api_client doesn't try and deserialize the response
47+
zipfile.write(uploadsApi.read_upload('experiments', exp.id, upload.id, format='binary', _preload_content=False).data)
48+

examples/02.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
import time
3+
import datetime
4+
import elabapi_python
5+
from elabapi_python.rest import ApiException
6+
7+
#########################
8+
# CONFIG #
9+
#########################
10+
API_HOST_URL = 'https://elab.local:3148/api/v2'
11+
# replace with your api key
12+
API_KEY = 'apiKey4Test'
13+
# number of days to look back
14+
PERIOD_IN_DAYS = 7
15+
#########################
16+
# END CONFIG #
17+
#########################
18+
19+
# Configure the api client
20+
configuration = elabapi_python.Configuration()
21+
configuration.api_key['api_key'] = API_KEY
22+
configuration.api_key_prefix['api_key'] = 'Authorization'
23+
configuration.host = API_HOST_URL
24+
configuration.debug = False
25+
configuration.verify_ssl = False
26+
27+
# create an instance of the API class
28+
api_client = elabapi_python.ApiClient(configuration)
29+
# fix issue with Authorization header not being properly set by the generated lib
30+
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)
31+
32+
# Synchronize metadata of the item_type with existing items
33+
# See: https://github.com/elabftw/elabftw/issues/3524
34+
35+
# create an instance of Items api
36+
itemsApi = elabapi_python.ItemsApi(api_client)
37+
38+
for item in itemsApi.read_items(cat=9):
39+
# skip items with metadata already
40+
if not item.metadata:
41+
print(f'Patching item {item.id}')
42+
itemsApi.patch_item(item.id, body={'metadata': '{"new":"metadata"}'})
43+

examples/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Examples for using this library
2+
3+
Work in progress!
4+
5+
# 00.py
6+
7+
Read all items.
8+
9+
# 01.py
10+
11+
Download all the timestamp archive zip created in the past 7 days.
12+
13+
# 02.py
14+
15+
Look for all items of a particular category and patch the metadata if it is empty.

helper.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ openapi_yaml_url="https://raw.githubusercontent.com/elabftw/elabftw/hypernext/ap
88
# folder with the generated python code
99
lib="generated"
1010

11-
function generate {
12-
# clean up first
11+
function cleanup {
1312
rm -rfv "$lib"
14-
# now generate the lib
13+
}
14+
15+
# generate the lib from remote hypernext spec
16+
function generate {
17+
cleanup
1518
docker run --user "$(id -u)":"$(id -gn)" --rm -v "${PWD}":/local "$docker_image" generate -i "$openapi_yaml_url" -l python -o /local/"$lib" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
1619
}
1720

21+
# generate the lib from a local file in current directory
22+
function generate-from-local {
23+
cleanup
24+
docker run --user "$(id -u)":"$(id -gn)" --rm -v "${PWD}":/local "$docker_image" generate -i /local/openapi.yaml -l python -o /local/"$lib" -c /local/config.json --git-user-id elabftw --git-repo-id elabapi-python
25+
}
26+
1827
function build {
1928
cd "$lib" || exit 1
2029
python setup.py sdist bdist_egg bdist_wheel

0 commit comments

Comments
 (0)