From ca1db14ab103727c018038fff31885c8e4c7ee0c Mon Sep 17 00:00:00 2001
From: Christopher Rhodes <christopher.rhodes@embl.de>
Date: Tue, 31 Oct 2023 11:42:04 +0100
Subject: [PATCH] Testing change of data directories over API

---
 model_server/api.py | 22 ++++++++++++++++++++--
 tests/test_api.py   | 17 ++++++++++++++++-
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/model_server/api.py b/model_server/api.py
index fdec6903..6e7fb5c3 100644
--- a/model_server/api.py
+++ b/model_server/api.py
@@ -1,7 +1,7 @@
-from fastapi import FastAPI
+from fastapi import FastAPI, HTTPException
 
 from model_server.models import DummyImageToImageModel
-from model_server.session import Session
+from model_server.session import Session, InvalidPathError
 from model_server.validators import validate_workflow_inputs
 from model_server.workflows import infer_image_to_image
 from extensions.ilastik.workflows import infer_px_then_ob_model
@@ -28,6 +28,24 @@ def list_bounce_back(par1=None, par2=None):
 def list_session_paths():
     return session.get_paths()
 
+def change_path(key, path):
+    try:
+        session.set_data_directory(key, path)
+    except InvalidPathError as e:
+        raise HTTPException(
+            status_code=404,
+            detail=e.__str__(),
+        )
+    return session.get_paths()
+
+@app.put('/paths/watch_input')
+def watch_input_path(path: str):
+    return change_path('inbound_images', path)
+
+@app.put('/paths/watch_output')
+def watch_input_path(path: str):
+    return change_path('outbound_images', path)
+
 @app.get('/restart')
 def restart_session(root: str = None) -> dict:
     session.restart(root=root)
diff --git a/tests/test_api.py b/tests/test_api.py
index e8e11056..3299cbe5 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -121,4 +121,19 @@ class TestApiFromAutomatedClient(TestServerBaseClass):
         resp_restart = requests.get(self.uri + 'restart')
         resp_list_1 = requests.get(self.uri + 'models')
         rj1 = resp_list_1.json()
-        self.assertEqual(len(rj1), 0, f'Unexpected models in response: {rj1}')
\ No newline at end of file
+        self.assertEqual(len(rj1), 0, f'Unexpected models in response: {rj1}')
+
+    def test_change_inbound_path(self):
+        resp_inpath = requests.get(
+            self.uri + 'paths'
+        )
+        resp_change = requests.put(
+            self.uri + f'/paths/watch_output',
+            params = {
+                'path': resp_inpath.json()['inbound_images']
+            }
+        )
+        resp_check = requests.get(
+            self.uri + 'paths'
+        )
+        self.assertEqual(resp_check.json()['inbound_images'], resp_check.json()['outbound_images'])
\ No newline at end of file
-- 
GitLab