Skip to content

Commit e7cc187

Browse files
committed
improve examples in 00. see elabftw/elabftw#4844
1 parent a3d69e8 commit e7cc187

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

examples/00.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
#!/usr/bin/env python
22
import time
3+
import json
34
import elabapi_python
45
from elabapi_python.rest import ApiException
56

67
# replace with your api key
78
my_api_key = 'apiKey4Test'
89

9-
# Config
10+
# START CONFIG
1011
configuration = elabapi_python.Configuration()
1112
configuration.api_key['api_key'] = my_api_key
1213
configuration.api_key_prefix['api_key'] = 'Authorization'
1314
configuration.host = 'https://elab.local:3148/api/v2'
14-
configuration.debug = True
15+
configuration.debug = False
1516
configuration.verify_ssl = False
1617

1718
# create an instance of the API class
1819
api_client = elabapi_python.ApiClient(configuration)
1920
# fix issue with Authorization header not being properly set by the generated lib
2021
api_client.set_default_header(header_name='Authorization', header_value=my_api_key)
22+
# END CONFIG
2123

2224
# create an instance of Items
2325
items = elabapi_python.ItemsApi(api_client)
26+
2427
# display items with default settings
25-
print(items.read_items())
26-
# display 50 items with a category id of 8
27-
print(len(items.read_items(limit=50, cat=8)))
28+
itemsList = items.read_items()
29+
print(type(itemsList)) # <-- python list
30+
print(itemsList)
31+
32+
# fetch 50 items with a category id of 8
33+
itemsList = items.read_items(limit=50, cat=8)
34+
print(f'Number of results: {len(itemsList)}')
35+
36+
# if you want raw json, use _preload_content=False
37+
# get the response object directly
38+
response = items.read_items(_preload_content=False)
39+
print(response.data.decode('utf-8')) # <-- JSON string
40+
# transform it into python object from JSON
41+
data = response.json()
42+
# and pretty print the firt three elements in JSON
43+
print(json.dumps(data[0:3], indent=2))

0 commit comments

Comments
 (0)