Compare commits

..

2 Commits

Author SHA256 Message Date
e40173f264 adds script to resolve chained requests 2026-01-27 23:23:09 +01:00
b771fedf49 changes Header.dump from method to attribute 2026-01-27 22:40:32 +01:00
3 changed files with 45 additions and 6 deletions

43
src/chained_requests.py Normal file
View File

@@ -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)

View File

@@ -6,10 +6,7 @@ class Header:
'''Init method, apikey suggested but not required (empty by default).''' '''Init method, apikey suggested but not required (empty by default).'''
self.auth = {"Authorization" : apikey} self.auth = {"Authorization" : apikey}
self.content = {"Content-Type" : "application/json"} self.content = {"Content-Type" : "application/json"}
def dump(self): self.dump = {**self.auth, **self.content}
'''Dumps the header in form of a dictionary.'''
dump = {**self.auth, **self.content}
return dump
if __name__=="__main__": if __name__=="__main__":
head = Header("MyApiKey-123456789abcdef") head = Header("MyApiKey-123456789abcdef")

View File

@@ -41,7 +41,6 @@ def get_sample_layers_data(elabid):
return result return result
apikey = getpass("Paste API key here: ") # consider replacing with .env file apikey = getpass("Paste API key here: ") # consider replacing with .env file
header = Header(apikey) header = Header(apikey).dump
header = header.dump()
result = get_sample_layers_data(1108) # edit id at will in case of deletion of remote source result = get_sample_layers_data(1108) # edit id at will in case of deletion of remote source
print(json.dumps(result)) print(json.dumps(result))