first attempt to refactor the test code into something more elegant

This commit is contained in:
2026-02-09 17:50:35 +01:00
parent 352a223d95
commit c49aa23aea
4 changed files with 266 additions and 50 deletions

31
src/APIHandler.py Normal file
View File

@@ -0,0 +1,31 @@
import requests
class APIHandler:
'''
Class to standardize the format of the headers of our http requests.
'''
# TO-DO: remove static url.
def __init__(self, apikey="", ELABFTW_API_URL="https://elabftw.fisica.unina.it/api/v2"):
'''Init method, apikey suggested but not required (empty by default).'''
self.auth = {"Authorization" : apikey}
self.content = {"Content-Type" : "application/json"}
self.dump = {**self.auth, **self.content}
self.elaburl = ELABFTW_API_URL
def get_entry_from_elabid(self, elabid, entryType="items"):
'''
Method which returns a resource's raw data (as dictionary) from its elabid and entry type.
Entry type can be either "experiments" or "items".
'''
# TO-DO: validation and error handling on entryType value.
header = self.dump
response = requests.get(
headers = header,
url = f"{self.elaburl}/{entryType}/{elabid}",
verify=True
)
if response.status_code // 100 in [2,3]:
entry_data = response.json()
return entry_data
else:
raise ConnectionError(f"HTTP request failed with status code: {response.status_code}.")