From e40173f264a0e65ad35b7921a78816b27fedb52597c99c838fbdaf97691e110d Mon Sep 17 00:00:00 2001 From: PioApocalypse Date: Tue, 27 Jan 2026 23:23:09 +0100 Subject: [PATCH] adds script to resolve chained requests --- src/chained_requests.py | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/chained_requests.py diff --git a/src/chained_requests.py b/src/chained_requests.py new file mode 100644 index 0000000..bc9f9de --- /dev/null +++ b/src/chained_requests.py @@ -0,0 +1,43 @@ +import os, json, requests +from getpass import getpass +from classes import Header + +''' +Starting from the sample's page we'll use this code to pull and return all data from the entries related to the sample and its fabrication. +''' + +class Entrypoint: + ''' + Use: Entrypoint(elabid) + + The entrypoint is the starting point of the process of resolving the data chain. The entrypoint must be a sample. + + Currently, the elabid of the sample must be provided. In the future a different method will eventually be implemented. + ''' + def __init__(self, elabid): + ''' + Constructor method. + self.sample_data is a dictionary of all data on the sample's eLab entry. + self.linked_experiments is a sub-dictionary which only includes links to related experiments. + self.batch_elabid is an integer (or NoneType if there's an error) containing the elabid of the associated substrate batch entry. + ''' + header = Header(apikey).dump + self.sample_data = requests.get( + headers = header, + url = f"https://elabftw.fisica.unina.it/api/v2/items/{elabid}", + verify=True + ).json() + self.linked_experiments = self.sample_data.get("related_experiments_links") or None + try: + self.batch_elabid = self.sample_data["metadata_decoded"]["extra_fields"]["Substrate batch"]["value"] + except KeyError as k: # if no metadata exists - which means there's a problem with the entry + self.batch_elabid = None + +if __name__=="__main__": + print("===== DEBUG MODE! =====") + apikey = getpass("Paste API key here: ") + elabid = input("Input elabid here [default = 1108]: ") or 1108 + chain = Entrypoint(elabid) + print(json.dumps(chain.sample_data)) + print(json.dumps(chain.linked_experiments)) + print(chain.batch_elabid) \ No newline at end of file