-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelabftw_api_client.py
144 lines (118 loc) · 5.02 KB
/
elabftw_api_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright (C) 2024 University of Münster
# elabftw-usersync is free software; you can redistribute it and/or modify it under the terms of the MIT License; see LICENSE file for more details.
"""This module provides a class for interacting with an ElabFTW server."""
import sys
import requests
import json
class ElabFTW:
"""Class for interacting with an ElabFTW server."""
session = None
host_url = None
def __init__(self, host_url, apikey, user_agent_string="elabftw_api_access"):
"""
Initialize an instance of the ElabFTW class.
Args:
host_url (str): The URL of the ElabFTW server.
apikey (str): The API key used for authentication.
"""
print("Initializing ElabFTW class with user_agent_string: " + user_agent_string)
# normalize self.host_url to remove everything after the last TLD ending
# e.g. https://elabftw.example.com/login -> https://elabftw.example.com
self.host_url = host_url.rsplit("/", 1)[0]
self.session = requests.Session()
self.session.headers.update(
{"Authorization": apikey, "User-Agent": user_agent_string}
)
self.all_users = None
self.user_data_list = None
def check_connection(self):
"""Check if the connection to ElabFTW is working.
Return True if the connection is working, otherwise raises a critical error and exits the script.
"""
try:
resp = self.session.get(self.host_url + "/api/v2/info")
except requests.exceptions.ConnectionError:
print("Error connecting to ElabFTW: Connection refused")
sys.exit(1)
else:
if resp.status_code != 200:
print(
"Error connecting to ElabFTW: "
+ str(resp.status_code)
+ " "
+ resp.text
)
sys.exit(1)
else:
return f"Connection to ElabFTW at {self.host_url} successful"
def get_ressources(self):
"""Get all items from the ElabFTW server.
Returns:
list: A list of dictionaries containing the items.
"""
resp = self.session.get(self.host_url + "/api/v2/items")
return resp.json()
def get_ressource_categories(self):
"""Get all categories from the ElabFTW server.
Returns:
list: A list of dictionaries containing the categories.
"""
resp = self.session.get(self.host_url + "/api/v2/items_types")
return resp.json()
def create_ressource(
self, category_id=None, tags=None, title=None, body=None, metadata=None
):
"""
Create a new item on the ElabFTW server.
Args:
category_id (int, optional): The category ID of the item to be created.
tags (list, optional): A list of tags for the item to be created.
Returns:
dict: A dictionary containing the data of the created item.
"""
item_data = {"category_id": 0, "tags": []}
if category_id is not None:
item_data["category_id"] = category_id
if tags is not None:
item_data["tags"] = tags
resp = self.session.post(self.host_url + "/api/v2/items", json=item_data)
if resp.status_code == 201:
id = int(resp.headers["location"].split("/")[-1])
html_url = (f"{self.host_url}/database.php?mode=view&id={id}",)
metadata_json = json.dumps(metadata)
data = {"title": title, "body": body, "metadata": metadata_json}
if self.update_ressource(id, data):
print(f"Item created: {html_url}")
return id
else:
print(f"Error creating item: {resp.text}")
return None
def update_ressource(self, item_id, item_data):
"""
Update an existing item on the ElabFTW server.
Args:
item_id (int): The ID of the item to be updated.
item_data (dict): A dictionary containing the data of the item to be updated. Allowed keys are:
- title (str)
- body (str)
- tags (list)
- etc.
Returns:
dict: A dictionary containing the data of the updated item.
"""
url = f"{self.host_url}/api/v2/items/{item_id}"
resp = self.session.patch(url, json=item_data)
return resp.json()
def create_ressource_category(self, category_title):
"""
Create a new category on the ElabFTW server.
Args:
category_title (str): The title of the category to be created.
Returns:
dict: A dictionary containing the data of the created category.
"""
category_data = {"title": category_title}
resp = self.session.post(self.host_url + "/api/v2/items_types", json=category_data)
if resp.status_code == 201:
print(f"Category created: {category_title}")
return resp.headers["location"].split("/")[-1]