uses dotenv to store api key and other important variables

if a value is not found in .env it will be prompted, but not checked
next step is user docs
This commit is contained in:
2026-05-13 12:31:26 +02:00
parent 686f869d10
commit ee96100a73
2 changed files with 18 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
import os, requests
from dotenv import load_dotenv
from getpass import getpass
import elabapi_python as elabapi
@@ -20,10 +21,13 @@ class APIHandler:
"""
# TO-DO: remove static url.
def __init__(
self, api_key="", ELABFTW_API_URL="https://elabftw.fisica.unina.it/api/v2"
):
def __init__(self, api_key="", ELABFTW_API_URL=None):
"""Init method, api_key suggested but not required (empty by default)."""
if not ELABFTW_API_URL:
load_dotenv()
ELABFTW_API_URL = os.getenv("ELABFTW_API_URL") or input(
"Enter a valid eLabFTW API URL (ends with '/api/v2)': "
)
self.api_key = api_key
self.auth = {"Authorization": api_key}
self.content = {"Content-Type": "application/json"}

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python3
import os, json, requests, h5py
import numpy as np
import dotenv
from dotenv import load_dotenv
from getpass import getpass
from APIHandler import APIHandler
from classes import *
@@ -91,7 +91,7 @@ def call_layers_from_list(elabid_list):
+ str(e)
+ f"\nPlease solve the problem before retrying."
+ "\n\n"
+ f"Last resource attempted to call: {ELABFTW_API_URL}/experiments/{elabid}"
+ f"Last resource attempted to call at base url: /experiments/{elabid}"
)
return list_of_layers # list of Layer-class objects
@@ -870,15 +870,18 @@ def build_nexus_file(pld_fabrication, output_path="output/nffa-di_unnamed.h5"):
if __name__ == "__main__":
# TO-DO: place the API base URL somewhere else.
ELABFTW_API_URL = "https://elabftw.fisica.unina.it/api/v2"
api_key = getpass("Paste API key here: ")
elabid = input("Enter elabid of your starting sample [default = 1111]: ") or 1111
load_dotenv()
api_key = os.getenv("api_key") or getpass("Paste API key here: ")
elabid = (
os.getenv("elabid")
or input("Enter elabid of your starting sample [default = 1111]: ")
or 1111
)
handler = APIHandler(api_key)
data = handler.get_entry_from_elabid(elabid)
sample = Entrypoint(data)
sample_name = sample.name.strip().replace(" ", "_")
operative_unit = "Napoli"
operative_unit = os.getenv("operative_unit") or None
if sample.proposal:
sample_proposal = call_proposal_from_elabid(sample.proposal)
else:
@@ -890,7 +893,7 @@ if __name__ == "__main__":
fn_base = (
"nffa-di_"
+ (f"{sample_proposal}_" if sample_proposal else "")
+ operative_unit
+ (f"{operative_unit}_" if operative_unit else "")
+ "_"
+ sample_name
)