Skip to content

Commit 3a94a52

Browse files
authored
Explicitly provide tokens in API calls from instrument server (#654)
1 parent 88d3edb commit 3a94a52

File tree

17 files changed

+224
-71
lines changed

17 files changed

+224
-71
lines changed

src/murfey/client/analyser.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Analyser(Observer):
3434
def __init__(
3535
self,
3636
basepath_local: Path,
37+
token: str,
3738
environment: MurfeyInstanceEnvironment | None = None,
3839
force_mdoc_metadata: bool = False,
3940
limited: bool = False,
@@ -49,6 +50,7 @@ def __init__(
4950
self._batch_store: dict = {}
5051
self._environment = environment
5152
self._force_mdoc_metadata = force_mdoc_metadata
53+
self._token = token
5254
self.parameters_model: (
5355
Type[ProcessingParametersSPA] | Type[ProcessingParametersTomo] | None
5456
) = None
@@ -60,6 +62,7 @@ def __init__(
6062
self._murfey_config = (
6163
get_machine_config_client(
6264
str(environment.url.geturl()),
65+
self._token,
6366
instrument_name=environment.instrument_name,
6467
demo=environment.demo,
6568
)
@@ -123,32 +126,34 @@ def _find_context(self, file_path: Path) -> bool:
123126
# CLEM workflow checks
124127
# Look for LIF and XLIF files
125128
if file_path.suffix in (".lif", ".xlif"):
126-
self._context = CLEMContext("leica", self._basepath)
129+
self._context = CLEMContext("leica", self._basepath, self._token)
127130
return True
128131
# Look for TIFF files associated with CLEM workflow
129132
# Leica's autosave mode seems to name the TIFFs in the format
130133
# PostionXX--ZXX--CXX.tif
131134
if all(
132135
pattern in file_path.name for pattern in ("--Z", "--C")
133136
) and file_path.suffix in (".tiff", ".tif"):
134-
self._context = CLEMContext("leica", self._basepath)
137+
self._context = CLEMContext("leica", self._basepath, self._token)
135138
return True
136139

137140
# Tomography and SPA workflow checks
138141
if "atlas" in file_path.parts:
139-
self._context = AtlasContext("epu", self._basepath)
142+
self._context = AtlasContext("epu", self._basepath, self._token)
140143
return True
141144

142145
if "Metadata" in file_path.parts or file_path.name == "EpuSession.dm":
143-
self._context = SPAMetadataContext("epu", self._basepath)
146+
self._context = SPAMetadataContext("epu", self._basepath, self._token)
144147
return True
145148
elif (
146149
"Batch" in file_path.parts
147150
or "SearchMaps" in file_path.parts
148151
or "Thumbnails" in file_path.parts
149152
or file_path.name == "Session.dm"
150153
):
151-
self._context = TomographyMetadataContext("tomo", self._basepath)
154+
self._context = TomographyMetadataContext(
155+
"tomo", self._basepath, self._token
156+
)
152157
return True
153158

154159
split_file_stem = file_path.stem.split("_")
@@ -164,7 +169,9 @@ def _find_context(self, file_path: Path) -> bool:
164169
]:
165170
if not self._context:
166171
logger.info("Acquisition software: EPU")
167-
self._context = SPAModularContext("epu", self._basepath)
172+
self._context = SPAModularContext(
173+
"epu", self._basepath, self._token
174+
)
168175
self.parameters_model = ProcessingParametersSPA
169176
return True
170177

@@ -178,7 +185,9 @@ def _find_context(self, file_path: Path) -> bool:
178185
):
179186
if not self._context:
180187
logger.info("Acquisition software: tomo")
181-
self._context = TomographyContext("tomo", self._basepath)
188+
self._context = TomographyContext(
189+
"tomo", self._basepath, self._token
190+
)
182191
self.parameters_model = ProcessingParametersTomo
183192
return True
184193
return False
@@ -213,14 +222,18 @@ def _analyse(self):
213222
or transferred_file.name == "EpuSession.dm"
214223
and not self._context
215224
):
216-
self._context = SPAMetadataContext("epu", self._basepath)
225+
self._context = SPAMetadataContext(
226+
"epu", self._basepath, self._token
227+
)
217228
elif (
218229
"Batch" in transferred_file.parts
219230
or "SearchMaps" in transferred_file.parts
220231
or transferred_file.name == "Session.dm"
221232
and not self._context
222233
):
223-
self._context = TomographyMetadataContext("tomo", self._basepath)
234+
self._context = TomographyMetadataContext(
235+
"tomo", self._basepath, self._token
236+
)
224237
self.post_transfer(transferred_file)
225238
else:
226239
dc_metadata = {}

src/murfey/client/context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ class Context:
3636
user_params: List[ProcessingParameter] = []
3737
metadata_params: List[ProcessingParameter] = []
3838

39-
def __init__(self, name: str, acquisition_software: str):
39+
def __init__(self, name: str, acquisition_software: str, token: str):
4040
self._acquisition_software = acquisition_software
41+
self._token = token
4142
self.name = name
4243
self.data_collection_parameters: dict = {}
4344

src/murfey/client/contexts/atlas.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313

1414
class AtlasContext(Context):
15-
def __init__(self, acquisition_software: str, basepath: Path):
16-
super().__init__("Atlas", acquisition_software)
15+
def __init__(self, acquisition_software: str, basepath: Path, token: str):
16+
super().__init__("Atlas", acquisition_software, token)
1717
self._basepath = basepath
1818

1919
def post_transfer(
@@ -36,12 +36,13 @@ def post_transfer(
3636
source = _get_source(transferred_file, environment)
3737
if source:
3838
transferred_atlas_name = _atlas_destination(
39-
environment, source, transferred_file
39+
environment, source, transferred_file, self._token
4040
) / transferred_file.relative_to(source.parent)
4141
capture_post(
4242
base_url=str(environment.url.geturl()),
4343
router_name="session_control.spa_router",
4444
function_name="make_atlas_jpg",
45+
token=self._token,
4546
session_id=environment.murfey_session,
4647
data={"path": str(transferred_atlas_name)},
4748
)

src/murfey/client/contexts/clem.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020

2121

2222
def _file_transferred_to(
23-
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
23+
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path, token: str
2424
) -> Optional[Path]:
2525
"""
2626
Returns the Path of the transferred file on the DLS file system.
2727
"""
2828
machine_config = get_machine_config_client(
2929
str(environment.url.geturl()),
30+
token,
3031
instrument_name=environment.instrument_name,
3132
demo=environment.demo,
3233
)
@@ -89,8 +90,8 @@ def _find_elements_recursively(
8990

9091

9192
class CLEMContext(Context):
92-
def __init__(self, acquisition_software: str, basepath: Path):
93-
super().__init__("CLEM", acquisition_software)
93+
def __init__(self, acquisition_software: str, basepath: Path, token: str):
94+
super().__init__("CLEM", acquisition_software, token)
9495
self._basepath = basepath
9596
# CLEM contexts for "auto-save" acquisition mode
9697
self._tiff_series: Dict[str, List[str]] = {} # {Series name : TIFF path list}
@@ -128,6 +129,7 @@ def post_transfer(
128129
environment=environment,
129130
source=source,
130131
file_path=transferred_file,
132+
token=self._token,
131133
)
132134
if not destination_file:
133135
logger.warning(
@@ -320,6 +322,7 @@ def post_transfer(
320322
environment=environment,
321323
source=source,
322324
file_path=transferred_file,
325+
token=self._token,
323326
)
324327
if not destination_file:
325328
logger.warning(
@@ -356,6 +359,7 @@ def register_lif_file(
356359
base_url=str(environment.url.geturl()),
357360
router_name="clem.router",
358361
function_name="register_lif_file",
362+
token=self._token,
359363
session_id=environment.murfey_session,
360364
data={"lif_file": quote(str(lif_file), safe="")},
361365
)
@@ -381,6 +385,7 @@ def process_lif_file(
381385
base_url=str(environment.url.geturl()),
382386
router_name="clem.router",
383387
function_name="process_raw_lifs",
388+
token=self._token,
384389
session_id=environment.murfey_session,
385390
data={"lif_file": quote(str(lif_file), safe="")},
386391
)
@@ -404,6 +409,7 @@ def register_tiff_file(
404409
base_url=str(environment.url.geturl()),
405410
router_name="clem.router",
406411
function_name="register_tiff_file",
412+
token=self._token,
407413
session_id=environment.murfey_session,
408414
data={"tiff_file": quote(str(tiff_file), safe="")},
409415
)
@@ -429,6 +435,7 @@ def process_tiff_series(
429435
base_url=str(environment.url.geturl()),
430436
router_name="clem.router",
431437
function_name="process_raw_tiffs",
438+
token=self._token,
432439
session_id=environment.murfey_session,
433440
data=tiff_dataset,
434441
)

src/murfey/client/contexts/fib.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def _number_from_name(name: str) -> int:
3232

3333

3434
class FIBContext(Context):
35-
def __init__(self, acquisition_software: str, basepath: Path):
36-
super().__init__("FIB", acquisition_software)
35+
def __init__(self, acquisition_software: str, basepath: Path, token: str):
36+
super().__init__("FIB", acquisition_software, token)
3737
self._basepath = basepath
3838
self._milling: Dict[int, List[MillingProgress]] = {}
3939
self._lamellae: Dict[int, Lamella] = {}
@@ -95,6 +95,7 @@ def post_transfer(
9595
base_url=str(environment.url.geturl()),
9696
router_name="workflow.correlative_router",
9797
function_name="make_gif",
98+
token=self._token,
9899
year=datetime.now().year,
99100
visit_name=environment.visit,
100101
session_id=environment.murfey_session,

src/murfey/client/contexts/spa.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626

2727

2828
def _file_transferred_to(
29-
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
29+
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path, token: str
3030
):
3131
machine_config = get_machine_config_client(
3232
str(environment.url.geturl()),
33+
token,
3334
instrument_name=environment.instrument_name,
3435
demo=environment.demo,
3536
)
@@ -111,8 +112,8 @@ class SPAModularContext(Context):
111112
ProcessingParameter("motion_corr_binning", "Motion Correction Binning"),
112113
]
113114

114-
def __init__(self, acquisition_software: str, basepath: Path):
115-
super().__init__("SPA", acquisition_software)
115+
def __init__(self, acquisition_software: str, basepath: Path, token: str):
116+
super().__init__("SPA", acquisition_software, token)
116117
self._basepath = basepath
117118
self._processing_job_stash: dict = {}
118119
self._foil_holes: Dict[int, List[int]] = {}
@@ -229,6 +230,7 @@ def gather_metadata(
229230
base_url=str(environment.url.geturl()),
230231
router_name="session_control.router",
231232
function_name="machine_info_by_instrument",
233+
token=self._token,
232234
instrument_name=environment.instrument_name,
233235
)
234236
if server_config_response is None:
@@ -302,6 +304,7 @@ def _position_analysis(
302304
base_url=str(environment.url.geturl()),
303305
router_name="session_info.router",
304306
function_name="get_dc_groups",
307+
token=self._token,
305308
session_id=environment.murfey_session,
306309
)
307310
.json()
@@ -339,14 +342,17 @@ def _position_analysis(
339342
else metadata_source_as_str
340343
)
341344
image_path = (
342-
_file_transferred_to(environment, metadata_source, Path(gs.image))
345+
_file_transferred_to(
346+
environment, metadata_source, Path(gs.image), self._token
347+
)
343348
if gs.image
344349
else ""
345350
)
346351
capture_post(
347352
base_url=str(environment.url.geturl()),
348353
router_name="session_control.spa_router",
349354
function_name="register_grid_square",
355+
token=self._token,
350356
session_id=environment.murfey_session,
351357
gsid=grid_square,
352358
data={
@@ -385,14 +391,17 @@ def _position_analysis(
385391
else metadata_source_as_str
386392
)
387393
image_path = (
388-
_file_transferred_to(environment, metadata_source, Path(fh.image))
394+
_file_transferred_to(
395+
environment, metadata_source, Path(fh.image), self._token
396+
)
389397
if fh.image
390398
else ""
391399
)
392400
capture_post(
393401
base_url=str(environment.url.geturl()),
394402
router_name="session_control.spa_router",
395403
function_name="register_foil_hole",
404+
token=self._token,
396405
session_id=environment.murfey_session,
397406
gs_name=grid_square,
398407
data={
@@ -416,6 +425,7 @@ def _position_analysis(
416425
base_url=str(environment.url.geturl()),
417426
router_name="session_control.spa_router",
418427
function_name="register_foil_hole",
428+
token=self._token,
419429
session_id=environment.murfey_session,
420430
gs_name=grid_square,
421431
data={
@@ -444,6 +454,7 @@ def post_transfer(
444454
if environment:
445455
machine_config = get_machine_config_client(
446456
str(environment.url.geturl()),
457+
self._token,
447458
instrument_name=environment.instrument_name,
448459
demo=environment.demo,
449460
)
@@ -470,13 +481,14 @@ def post_transfer(
470481

471482
if environment:
472483
file_transferred_to = _file_transferred_to(
473-
environment, source, transferred_file
484+
environment, source, transferred_file, self._token
474485
)
475486
if not environment.movie_counters.get(str(source)):
476487
movie_counts_get = capture_get(
477488
base_url=str(environment.url.geturl()),
478489
router_name="session_control.router",
479490
function_name="count_number_of_movies",
491+
token=self._token,
480492
)
481493
if movie_counts_get is not None:
482494
environment.movie_counters[str(source)] = count(
@@ -493,6 +505,7 @@ def post_transfer(
493505
base_url=str(environment.url.geturl()),
494506
router_name="file_io_instrument.router",
495507
function_name="write_eer_fractionation_file",
508+
token=self._token,
496509
visit_name=environment.visit,
497510
session_id=environment.murfey_session,
498511
data={
@@ -552,6 +565,7 @@ def post_transfer(
552565
base_url=str(environment.url.geturl()),
553566
router_name="workflow.spa_router",
554567
function_name="request_spa_preprocessing",
568+
token=self._token,
555569
visit_name=environment.visit,
556570
session_id=environment.murfey_session,
557571
data={

0 commit comments

Comments
 (0)