Seite 1 von 1

elab / elabapi-python

Verfasst: Mittwoch 20. September 2023, 15:49
von sophe_9
Hallo zusammen,

ich habe ein Problem mit dem Laborbuch elabftw:

Ich kann den Titel, Kommentare und den Body ändern, aber keine Tags wählen oder neu erzeugen.

Hier ändern ich den Titel und es funktioniert:

Code: Alles auswählen


import elabapi_python
from elabapi_python.rest import ApiException

#########################
#         CONFIG        #
#########################
API_HOST_URL = 'https://elab.local:3148/api/v2'
API_KEY = 'a2ca4231a0a834ed30c19be8'
#########################
#      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

api_client = elabapi_python.ApiClient(configuration)
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)
experimentsApi = elabapi_python.ExperimentsApi(api_client)

elab_id=407
data = { 'title': 'test FM1' }

response = experimentsApi.patch_experiment(elab_id, body=data)

Wenn ich allerdings

Code: Alles auswählen

data = { 'title': 'test ' , 'tags':'versuch' }
eingebe, erhalte ich folgenden Fehler:

ApiException: (400)
Reason: Bad Request

Kann mir jemand weiterhelfen?

Re: elab / elabapi-python

Verfasst: Mittwoch 20. September 2023, 16:29
von Sirius3
Willst Du wirklich Deinen API-KEY hier öffentlich posten?
Laut Dokumentation legt man Tags so an: https://doc.elabftw.net/api/v2/#/Tags/post-tag



Also was in der Art:

Code: Alles auswählen

response = requests.post(f"{API_HOST_URL}/experiments/{experiment_id}/tags", json={"tag": "test FM1"}, headers=headers)

Re: elab / elabapi-python

Verfasst: Mittwoch 20. September 2023, 17:19
von sophe_9
Es ist nicht der richtige API-KEY.

Leider hilft mir der Link https://doc.elabftw.net/api/v2/#/Tags/post-tag nicht weiter, dort wird eine json struktur beschrieben.

Hier gibt es es ganz gute Beispiele https://doc.elabftw.net/api/elabapi-htm ... gs-postTag. Daran habe ich mich bis jetzt orientiert.

Code: Alles auswählen

import elabapi_python
from elabapi_python.rest import ApiException

#########################
#         CONFIG        #
#########################
API_HOST_URL = 'https://elab.local:3148/api/v2'
API_KEY = 'a2ca4231a0a834ed30c19be8'
#########################
#      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

api_client = elabapi_python.ApiClient(configuration)
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)
tagsApi = elabapi_python.TagsApi(api_client)

entityType = entityType_example # String | Entity type  ?????????????????
id = 56 # Integer | ID of the entity
body =  # Id_tags_body | Parameters for creating a tag. (optional)

response = tagsApi.post_tag(entityType, id, body=body)

Ich verstehe den entityType nicht. Was muss ich genau konfigurieren

Re: elab / elabapi-python

Verfasst: Mittwoch 20. September 2023, 18:34
von __blackjack__
@sophe_9: Das steht unter dem Link der angeblich nicht weiterhilft.

Re: elab / elabapi-python

Verfasst: Mittwoch 20. September 2023, 18:38
von Sirius3
Was Du da als Modul benutzt, ist nur eine sehr dünne Schicht über der eigentlichen REST-API. Deshalb mußt Du schon lernen, die API-Dokumentation zu lesen, sonst kommst Du nicht weiter.

Re: elab / elabapi-python

Verfasst: Donnerstag 21. September 2023, 08:26
von sophe_9
Ihr wisst also selber nicht, wo mein Fehler liegt.

So wie es in dem Link beschrieben ist, funktioniert es nicht. Habe mich vorher natürlich damit beschäftigt.

Habt ihr mit dem Laborbuch denn schon mal gearbeitet?

Re: elab / elabapi-python

Verfasst: Donnerstag 21. September 2023, 08:36
von __blackjack__
@sophe_9: Was hast Du denn ausprobiert nachdem Du den Link gelesen hast und was ist daraufhin passiert? Welchen der Werte für "entityType" aus der Dokumentation hast Du gewählt?

Re: elab / elabapi-python

Verfasst: Donnerstag 21. September 2023, 14:06
von sophe_9

Code: Alles auswählen

#!/usr/bin/env python
import time
import elabapi_python
from elabapi_python.rest import ApiException
# necessary imports for this example
import csv
import json

# replace with your api key
API_HOST_URL = 'https://elabftw.ptb.de/api/v2'
# replace with your api key
API_KEY = 'a2c...meinkey.....4ed30c19be8'

# 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 = True

elab_id=407

api_client = elabapi_python.ApiClient(configuration)
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)

experimentsApi = elabapi_python.ExperimentsApi(api_client)
exp = experimentsApi.get_experiment(elab_id)

print('='*72)
print('\n ******** Experiment-Body ', exp.body)
print('\n ******** Experiment-Tags ', exp.tags)

data = { 'title': ' Versuch 2' }


# === Modifizieren der Einträge (hier Eintrag "body") ===
response = experimentsApi.patch_experiment(elab_id, body=data)

print('\n ******** Neuer Experiment-Body ', exp.body)

print('='*72)
Return:
=======

******** Experiment-Body Versuch

******** Experiment-Tags TLV|FM1|7.5-1V-23-37

******** Neuer Experiment-Body Versuch
====================================================
Der entityType ist ein String und daher habe ich den String verwendet der mir angezeigt wurde:
entityType = 'TLV' # String | Entity type

Re: elab / elabapi-python

Verfasst: Donnerstag 21. September 2023, 14:39
von Sirius3
Warum sollte der Entititätstyp eines Tags für die Entität "Experimente" ein konkretes Tag "TLV" sein?

Auf der Swagger-Seite ist der Wert für dieses Feld nicht einmal frei wählbar, sondern da gibt es ein Dropdown mit fixen Werten:
Entity type

Available values : experiments, items, experiments_templates, items_types[/code]