Skip to content

Commit 2287de9

Browse files
Refactor API posts to send more information on failed posts (#651)
This updates the capture_post/get/delete calls to take in the router name and function instead of a url. The url is then constructed. By doing this any failed calls can send the function name on for reuse.
1 parent 5bf67cc commit 2287de9

File tree

20 files changed

+793
-443
lines changed

20 files changed

+793
-443
lines changed

src/murfey/client/contexts/atlas.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22
from pathlib import Path
33
from typing import Optional
44

5-
import requests
6-
75
from murfey.client.context import Context
86
from murfey.client.contexts.spa import _get_source
97
from murfey.client.contexts.spa_metadata import _atlas_destination
108
from murfey.client.instance_environment import MurfeyInstanceEnvironment
11-
from murfey.util.api import url_path_for
12-
from murfey.util.client import authorised_requests, capture_post
9+
from murfey.util.client import capture_post
1310

1411
logger = logging.getLogger("murfey.client.contexts.atlas")
1512

16-
requests.get, requests.post, requests.put, requests.delete = authorised_requests()
17-
1813

1914
class AtlasContext(Context):
2015
def __init__(self, acquisition_software: str, basepath: Path):
@@ -44,8 +39,11 @@ def post_transfer(
4439
environment, source, transferred_file
4540
) / transferred_file.relative_to(source.parent)
4641
capture_post(
47-
f"{str(environment.url.geturl())}{url_path_for('session_control.spa_router', 'make_atlas_jpg', session_id=environment.murfey_session)}",
48-
json={"path": str(transferred_atlas_name)},
42+
base_url=str(environment.url.geturl()),
43+
router_name="session_control.spa_router",
44+
function_name="make_atlas_jpg",
45+
session_id=environment.murfey_session,
46+
data={"path": str(transferred_atlas_name)},
4947
)
5048
logger.info(
5149
f"Submitted request to create JPG image of atlas {str(transferred_atlas_name)!r}"

src/murfey/client/contexts/clem.py

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
from murfey.client.context import Context
1515
from murfey.client.instance_environment import MurfeyInstanceEnvironment
16-
from murfey.util.api import url_path_for
1716
from murfey.util.client import capture_post, get_machine_config_client
1817

1918
# Create logger object
@@ -353,19 +352,14 @@ def register_lif_file(
353352
register the LIF file in the database correctly as part of the CLEM workflow.
354353
"""
355354
try:
356-
# Construct URL to post to post the request to
357-
url = f"{environment.url.geturl()}{url_path_for('clem.router', 'register_lif_file', session_id=environment.murfey_session)}?lif_file={quote(str(lif_file), safe='')}"
358-
# Validate
359-
if not url:
360-
logger.error(
361-
"URL could not be constructed from the environment and file path"
362-
)
363-
return False
364-
365-
# Send the message
366-
capture_post(url)
355+
capture_post(
356+
base_url=str(environment.url.geturl()),
357+
router_name="clem.router",
358+
function_name="register_lif_file",
359+
session_id=environment.murfey_session,
360+
data={"lif_file": quote(str(lif_file), safe="")},
361+
)
367362
return True
368-
369363
except Exception as e:
370364
logger.error(
371365
f"Error encountered when registering the LIF file in the database: {e}"
@@ -383,19 +377,14 @@ def process_lif_file(
383377
"""
384378

385379
try:
386-
# Construct the URL to post the request to
387-
url = f"{environment.url.geturl()}{url_path_for('clem.router', 'process_raw_lifs', session_id=environment.murfey_session)}?lif_file={quote(str(lif_file), safe='')}"
388-
# Validate
389-
if not url:
390-
logger.error(
391-
"URL could not be constructed from the environment and file path"
392-
)
393-
return False
394-
395-
# Send the message
396-
capture_post(url)
380+
capture_post(
381+
base_url=str(environment.url.geturl()),
382+
router_name="clem.router",
383+
function_name="process_raw_lifs",
384+
session_id=environment.murfey_session,
385+
data={"lif_file": quote(str(lif_file), safe="")},
386+
)
397387
return True
398-
399388
except Exception as e:
400389
logger.error(f"Error encountered processing LIF file: {e}")
401390
return False
@@ -411,17 +400,14 @@ def register_tiff_file(
411400
"""
412401

413402
try:
414-
url = f"{environment.url.geturl()}{url_path_for('clem.router', 'register_tiff_file', session_id=environment.murfey_session)}?tiff_file={quote(str(tiff_file), safe='')}"
415-
if not url:
416-
logger.error(
417-
"URL could not be constructed from the environment and file path"
418-
)
419-
return False
420-
421-
# Send the message
422-
capture_post(url)
403+
capture_post(
404+
base_url=str(environment.url.geturl()),
405+
router_name="clem.router",
406+
function_name="register_tiff_file",
407+
session_id=environment.murfey_session,
408+
data={"tiff_file": quote(str(tiff_file), safe="")},
409+
)
423410
return True
424-
425411
except Exception as e:
426412
logger.error(
427413
f"Error encountered when registering the TIFF file in the database: {e}"
@@ -439,18 +425,14 @@ def process_tiff_series(
439425
"""
440426

441427
try:
442-
# Construct URL for Murfey server to communicate with
443-
url = f"{environment.url.geturl()}{url_path_for('clem.router', 'process_raw_tiffs', session_id=environment.murfey_session)}"
444-
if not url:
445-
logger.error(
446-
"URL could not be constructed from the environment and file path"
447-
)
448-
return False
449-
450-
# Send the message
451-
capture_post(url, json=tiff_dataset)
428+
capture_post(
429+
base_url=str(environment.url.geturl()),
430+
router_name="clem.router",
431+
function_name="process_raw_tiffs",
432+
session_id=environment.murfey_session,
433+
data=tiff_dataset,
434+
)
452435
return True
453-
454436
except Exception as e:
455437
logger.error(f"Error encountered processing the TIFF series: {e}")
456438
return False

src/murfey/client/contexts/fib.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
from pathlib import Path
66
from typing import Dict, List, NamedTuple, Optional
77

8-
import requests
98
import xmltodict
109

1110
from murfey.client.context import Context
1211
from murfey.client.instance_environment import MurfeyInstanceEnvironment
13-
from murfey.util.api import url_path_for
14-
from murfey.util.client import authorised_requests
12+
from murfey.util.client import capture_post
1513

1614
logger = logging.getLogger("murfey.client.contexts.fib")
1715

18-
requests.get, requests.post, requests.put, requests.delete = authorised_requests()
19-
2016

2117
class Lamella(NamedTuple):
2218
name: str
@@ -95,9 +91,14 @@ def post_transfer(
9591
environment.default_destinations[self._basepath]
9692
).name
9793
# post gif list to gif making API call
98-
requests.post(
99-
f"{environment.url.geturl()}{url_path_for('workflow.correlative_router', 'make_gif', year=datetime.now().year, visit_name=environment.visit, session_id=environment.murfey_session)}",
100-
json={
94+
capture_post(
95+
base_url=str(environment.url.geturl()),
96+
router_name="workflow.correlative_router",
97+
function_name="make_gif",
98+
year=datetime.now().year,
99+
visit_name=environment.visit,
100+
session_id=environment.murfey_session,
101+
data={
101102
"lamella_number": lamella_number,
102103
"images": gif_list,
103104
"raw_directory": raw_directory,

src/murfey/client/contexts/spa.py

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from pathlib import Path
66
from typing import Any, Dict, List, Optional, OrderedDict, Tuple
77

8-
import requests
98
import xmltodict
109

1110
from murfey.client.context import Context, ProcessingParameter
@@ -14,13 +13,7 @@
1413
MurfeyID,
1514
MurfeyInstanceEnvironment,
1615
)
17-
from murfey.util.api import url_path_for
18-
from murfey.util.client import (
19-
authorised_requests,
20-
capture_get,
21-
capture_post,
22-
get_machine_config_client,
23-
)
16+
from murfey.util.client import capture_get, capture_post, get_machine_config_client
2417
from murfey.util.spa_metadata import (
2518
foil_hole_data,
2619
foil_hole_from_file,
@@ -31,8 +24,6 @@
3124

3225
logger = logging.getLogger("murfey.client.contexts.spa")
3326

34-
requests.get, requests.post, requests.put, requests.delete = authorised_requests()
35-
3627

3728
def _file_transferred_to(
3829
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
@@ -235,7 +226,10 @@ def gather_metadata(
235226
binning_factor = 1
236227
if environment:
237228
server_config_response = capture_get(
238-
f"{str(environment.url.geturl())}{url_path_for('session_control.router', 'machine_info_by_instrument', instrument_name=environment.instrument_name)}"
229+
base_url=str(environment.url.geturl()),
230+
router_name="session_control.router",
231+
function_name="machine_info_by_instrument",
232+
instrument_name=environment.instrument_name,
239233
)
240234
if server_config_response is None:
241235
return None
@@ -304,8 +298,11 @@ def _position_analysis(
304298
Optional[float],
305299
] = (None, None, None, None, None, None, None)
306300
data_collection_group = (
307-
requests.get(
308-
f"{environment.url.geturl()}{url_path_for('session_info.router', 'get_dc_groups', session_id=environment.murfey_session)}"
301+
capture_get(
302+
base_url=str(environment.url.geturl()),
303+
router_name="session_info.router",
304+
function_name="get_dc_groups",
305+
session_id=environment.murfey_session,
309306
)
310307
.json()
311308
.get(str(source), {})
@@ -327,7 +324,6 @@ def _position_analysis(
327324
local_atlas_path,
328325
grid_square=str(grid_square),
329326
)[str(grid_square)]
330-
gs_url = f"{str(environment.url.geturl())}{url_path_for('session_control.spa_router', 'register_grid_square', session_id=environment.murfey_session, gsid=grid_square)}"
331327
gs = grid_square_data(
332328
grid_square_metadata_file,
333329
grid_square,
@@ -348,8 +344,12 @@ def _position_analysis(
348344
else ""
349345
)
350346
capture_post(
351-
gs_url,
352-
json={
347+
base_url=str(environment.url.geturl()),
348+
router_name="session_control.spa_router",
349+
function_name="register_grid_square",
350+
session_id=environment.murfey_session,
351+
gsid=grid_square,
352+
data={
353353
"tag": str(source),
354354
"readout_area_x": gs.readout_area_x,
355355
"readout_area_y": gs.readout_area_y,
@@ -368,7 +368,6 @@ def _position_analysis(
368368
)
369369
foil_hole = foil_hole_from_file(transferred_file)
370370
if foil_hole not in self._foil_holes[grid_square]:
371-
fh_url = f"{str(environment.url.geturl())}{url_path_for('session_control.spa_router', 'register_foil_hole', session_id=environment.murfey_session, gs_name=grid_square)}"
372371
if environment.murfey_session is not None:
373372
fh = foil_hole_data(
374373
grid_square_metadata_file,
@@ -391,8 +390,12 @@ def _position_analysis(
391390
else ""
392391
)
393392
capture_post(
394-
fh_url,
395-
json={
393+
base_url=str(environment.url.geturl()),
394+
router_name="session_control.spa_router",
395+
function_name="register_foil_hole",
396+
session_id=environment.murfey_session,
397+
gs_name=grid_square,
398+
data={
396399
"name": foil_hole,
397400
"x_location": fh.x_location,
398401
"y_location": fh.y_location,
@@ -410,8 +413,12 @@ def _position_analysis(
410413
)
411414
else:
412415
capture_post(
413-
fh_url,
414-
json={
416+
base_url=str(environment.url.geturl()),
417+
router_name="session_control.spa_router",
418+
function_name="register_foil_hole",
419+
session_id=environment.murfey_session,
420+
gs_name=grid_square,
421+
data={
415422
"name": foil_hole,
416423
"tag": str(source),
417424
},
@@ -467,7 +474,9 @@ def post_transfer(
467474
)
468475
if not environment.movie_counters.get(str(source)):
469476
movie_counts_get = capture_get(
470-
f"{environment.url.geturl()}{url_path_for('session_control.router', 'count_number_of_movies')}",
477+
base_url=str(environment.url.geturl()),
478+
router_name="session_control.router",
479+
function_name="count_number_of_movies",
471480
)
472481
if movie_counts_get is not None:
473482
environment.movie_counters[str(source)] = count(
@@ -481,8 +490,12 @@ def post_transfer(
481490
eer_fractionation_file = None
482491
if file_transferred_to.suffix == ".eer":
483492
response = capture_post(
484-
f"{str(environment.url.geturl())}{url_path_for('file_io_instrument.router', 'write_eer_fractionation_file', visit_name=environment.visit, session_id=environment.murfey_session)}",
485-
json={
493+
base_url=str(environment.url.geturl()),
494+
router_name="file_io_instrument.router",
495+
function_name="write_eer_fractionation_file",
496+
visit_name=environment.visit,
497+
session_id=environment.murfey_session,
498+
data={
486499
"eer_path": str(file_transferred_to),
487500
"fractionation": self.data_collection_parameters[
488501
"eer_fractionation"
@@ -511,7 +524,6 @@ def post_transfer(
511524
)
512525
foil_hole = None
513526

514-
preproc_url = f"{str(environment.url.geturl())}{url_path_for('workflow.spa_router', 'request_spa_preprocessing', visit_name=environment.visit, session_id=environment.murfey_session)}"
515527
preproc_data = {
516528
"path": str(file_transferred_to),
517529
"description": "",
@@ -537,8 +549,12 @@ def post_transfer(
537549
"foil_hole_id": foil_hole,
538550
}
539551
capture_post(
540-
preproc_url,
541-
json={
552+
base_url=str(environment.url.geturl()),
553+
router_name="workflow.spa_router",
554+
function_name="request_spa_preprocessing",
555+
visit_name=environment.visit,
556+
session_id=environment.murfey_session,
557+
data={
542558
k: None if v == "None" else v
543559
for k, v in preproc_data.items()
544560
},

0 commit comments

Comments
 (0)