elabapi-python
elabapi-python copied to clipboard
Struggling to modify an item with patch_item function
Hello, we are just starting using eLabTFW in our lab and I'm currently facing an issue with importing items in resources database from a csv file via API
Step 1.
I'm reproducing kindly provided examples in python to create and modify items.
import elabapi_python
Step 2. Configuring the API client with my key
Step 3.
Creating an item, using category 15 in our settings:
targetCategory = 15
response = itemsApi.post_item_with_http_info(body={'category_id': targetCategory, 'tags': ['some tag', 'another tag']})
locationHeaderInResponse = response[2].get('Location')
print(f'The newly created item is here: {locationHeaderInResponse}')
Having a nice response: https://eln.ourcompany.url/api/v2/items/41
itemId = int(locationHeaderInResponse.split('/').pop())
print(itemId)
Also smoothly split to get the id = 41
Step 4. But then even simple modifications fail... I cannot figure out why.
---------------------------------------------------------------------------
[309]# Authentication setting
[309]auth_settings = ['token'] # noqa: E501
--> [312]return self.api_client.call_api(
[313]/items/{id}', 'PATCH',
[314]path_params,
[315]query_params,
[316]header_params,
[317]body=body_params,
[318]post_params=form_params,
[319]files=local_var_files,
[320]response_type='Item', # noqa: E501
[321]auth_settings=auth_settings,
...
ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'content-type': 'application/json', 'transfer-encoding': 'chunked', 'cache-control': 'max-age=0, private, must-revalidate, no-cache, private', 'set-cookie': 'PHPSESSID=...; path=/; secure; HttpOnly; SameSite=Lax', 'date': '... GMT', 'strict-transport-security': 'max-age=63072000', 'x-xss-protection': '0', 'x-content-type-options': 'nosniff', 'content-security-policy': "default-src 'self' data:; script-src 'self' ; connect-src 'self' blob: https://get.elabftw.net/; img-src 'self' data: blob:; style-src 'self' 'unsafe-inline'; font-src 'self' data:; object-src 'self'; base-uri 'none'; frame-ancestors 'none'", 'referrer-policy': 'no-referrer', 'permissions-policy': "autoplay 'none'; camera 'self'; document-domain 'none'; encrypted-media 'none'; fullscreen 'self'; geolocation 'none'; microphone 'self'; midi 'none'; payment 'none'; vr 'none'", 'vary': 'Accept-Encoding', 'server': 'c0c', 'access-control-allow-credentials': 'true', 'access-control-expose-headers': 'Location, Content-Encoding, Content-Disposition, Cache-Control'})
HTTP response body: b'{"code":400,"message":" Bad Request","description":"Incorrect submodel for database: available models are: comments, experiments_links, items_links, revisions, steps, tags, uploads."}'
Understanding what prevents the items from being modified would be great. Thank you for the support!
Why don't you provide the full (simplified) script, or at least step 4. You post the error but not the code triggering the error.
Oh, sorry. I didn't post the most important info:) The code that triggered the error is simply changing title, body, and rating to test the performance.
itemsApi.patch_item(itemId, body={'title': 'The new title', 'body': 'Main content text', 'rating': 5})
More sophisticated functions also fail. Extraction info from csv files works well, the issue occurs when altering an item.
itemsApi.patch_item(itemId, body={'title': row['Name'], 'body': getBodyFromRow(row), 'custom_id': row['ID'], 'metadata': getMetadataFromRow(row)})
Well, I'm not sure what is happening. I've just tried running this example and it works fine.
Can you make sure you run the latest elabapi_python version on the latest 5.0.4 elabftw ?
I have version elabapi_python 5.0.2 available via pip https://pypi.org/project/elabapi-python/#history
and our institution version at https://eln.company.name/ is eLabFTW 5.0.4
I tried with demo verison
Generated an item #182 , but again nothing was modified with patch_item
itemsApi.patch_item(itemId, body={'title': 'The new title', 'body': 'Main content text', 'rating': 5})
Well, I also just tried it on the demo. It works fine, so I'm not sure what your problem is.
Try this:
#!/usr/bin/env python
import time
import datetime
import elabapi_python
from elabapi_python.rest import ApiException
#########################
# CONFIG #
#########################
API_HOST_URL = 'https://demo.elabftw.net/api/v2'
# replace with your api key
API_KEY = '3-3bf66d75d1753bdb560a6fd0aa0f7891188ad6df88c753174e146dbefa641c737b6cbe121a8469fb2cef3'
#########################
# END CONFIG #
#########################
# Configure the api client
configuration = elabapi_python.Configuration()
configuration.api_key['api_key'] = API_KEY
configuration.api_key_prefix['api_key'] = 'Authorization'
configuration.host = API_HOST_URL
configuration.debug = False
configuration.verify_ssl = False
# create an instance of the API class
api_client = elabapi_python.ApiClient(configuration)
# fix issue with Authorization header not being properly set by the generated lib
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)
# Load items api
itemsApi = elabapi_python.ItemsApi(api_client)
itemId = 180
itemsApi.patch_item(itemId, body={'title': 'The new title', 'body': 'Main content text', 'rating': 5})
omg, we may close the issue.
I realized I made a silly mistake using
API_HOST_URL = 'https://demo.elabftw.net/api/v2/items'
instead of
API_HOST_URL = 'https://demo.elabftw.net/api/v2'
Thank you for your support and super prompt replies!