Compare commits
2 Commits
da074b027b
...
e40173f264
| Author | SHA256 | Date | |
|---|---|---|---|
| e40173f264 | |||
| b771fedf49 |
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)
|
||||
@@ -6,10 +6,7 @@ class Header:
|
||||
'''Init method, apikey suggested but not required (empty by default).'''
|
||||
self.auth = {"Authorization" : apikey}
|
||||
self.content = {"Content-Type" : "application/json"}
|
||||
def dump(self):
|
||||
'''Dumps the header in form of a dictionary.'''
|
||||
dump = {**self.auth, **self.content}
|
||||
return dump
|
||||
self.dump = {**self.auth, **self.content}
|
||||
|
||||
if __name__=="__main__":
|
||||
head = Header("MyApiKey-123456789abcdef")
|
||||
|
||||
@@ -41,7 +41,6 @@ def get_sample_layers_data(elabid):
|
||||
return result
|
||||
|
||||
apikey = getpass("Paste API key here: ") # consider replacing with .env file
|
||||
header = Header(apikey)
|
||||
header = header.dump()
|
||||
header = Header(apikey).dump
|
||||
result = get_sample_layers_data(1108) # edit id at will in case of deletion of remote source
|
||||
print(json.dumps(result))
|
||||
Reference in New Issue
Block a user