|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +############### |
| 4 | +# DESCRIPTION # |
| 5 | +############## |
| 6 | +# In this script, we will create a resource category and patch it |
| 7 | +############## |
| 8 | + |
| 9 | +# the python library for elabftw |
| 10 | +import elabapi_python |
| 11 | + |
| 12 | +######################### |
| 13 | +# CONFIG # |
| 14 | +######################### |
| 15 | +# replace with the URL of your instance |
| 16 | +API_HOST_URL = 'https://elab.local:3148/api/v2' |
| 17 | +# replace with your api key |
| 18 | +API_KEY = 'apiKey4Test' |
| 19 | +######################### |
| 20 | +# END CONFIG # |
| 21 | +######################### |
| 22 | + |
| 23 | +# Configure the api client |
| 24 | +configuration = elabapi_python.Configuration() |
| 25 | +configuration.api_key['api_key'] = API_KEY |
| 26 | +configuration.api_key_prefix['api_key'] = 'Authorization' |
| 27 | +configuration.host = API_HOST_URL |
| 28 | +configuration.debug = False |
| 29 | +configuration.verify_ssl = False |
| 30 | + |
| 31 | +# create an instance of the API class |
| 32 | +api_client = elabapi_python.ApiClient(configuration) |
| 33 | +# fix issue with Authorization header not being properly set by the generated lib |
| 34 | +api_client.set_default_header(header_name='Authorization', header_value=API_KEY) |
| 35 | + |
| 36 | +#### SCRIPT START ################## |
| 37 | + |
| 38 | +# Load the items types api |
| 39 | +itemsTypesApi = elabapi_python.ItemsTypesApi(api_client) |
| 40 | + |
| 41 | +# create one, we provide a title on creation but it's not mandatory |
| 42 | +response = itemsTypesApi.post_items_types_with_http_info(body={'title': "My freshly created category"}) |
| 43 | +# the response location for this endpoint is a bit different from the rest, it is the full URL: https://elab.example.org/api/v2/items_types/admin.php?tab=4&templateid=15 |
| 44 | +locationHeaderInResponse = response[2].get('Location') |
| 45 | +print(f'The newly created resource category is here: {locationHeaderInResponse}') |
| 46 | +itemId = int(locationHeaderInResponse.split('=').pop()) |
| 47 | +# now change the title, and body and color |
| 48 | +itemsTypesApi.patch_items_type(itemId, body={'title': 'The new title', 'body': 'Main content text', 'color': '#f5c211'}) |
0 commit comments