Files
parser-eLabFTW-NFFA-DI/src/APIHandler.py

31 lines
1.2 KiB
Python

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}.")