adds script to resolve chained requests
This commit is contained in:
43
src/chained_requests.py
Normal file
43
src/chained_requests.py
Normal 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)
|
||||
Reference in New Issue
Block a user