Skip to content
Snippets Groups Projects
Commit 9c735683 authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Added and tested exception handling for unlisted accessor ID

parent b33a5047
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@ from typing import Union
from fastapi import FastAPI, HTTPException
from .accessors import generate_file_accessor
from .session import session, InvalidPathError
from .session import session, AccessorIdError, InvalidPathError
app = FastAPI(debug=True)
......@@ -64,9 +64,19 @@ def list_active_models():
def list_accessors():
return session.list_accessors()
def _session_accessor(func, acc_id):
try:
return func(acc_id)
except AccessorIdError as e:
raise HTTPException(404, f'Did not find accessor with ID {acc_id}')
@app.get('/accessors/{accessor_id}')
def get_accessor(accessor_id: str):
return session.get_accessor_info(accessor_id)
return _session_accessor(session.get_accessor_info, accessor_id)
@app.get('/accessors/delete/{accessor_id}')
def delete_accessor(accessor_id: str):
return _session_accessor(session.del_accessor, accessor_id)
@app.put('/accessors/read_from_file')
def read_accessor_from_file(filename: str, accessor_id: Union[str, None] = None):
......@@ -76,7 +86,5 @@ def read_accessor_from_file(filename: str, accessor_id: Union[str, None] = None)
acc = generate_file_accessor(fp)
return session.add_accessor(acc, accessor_id=accessor_id)
@app.get('/accessors/delete/{accessor_id}')
def delete_accessor(accessor_id: str):
return session.del_accessor(accessor_id)
......@@ -125,6 +125,8 @@ class _Session(object):
"""
Get information about a single accessor
"""
if acc_id not in self.accessors.keys():
raise AccessorIdError(f'No accessor with ID {acc_id} is registered')
return self.list_accessors()[acc_id]
@staticmethod
......
......@@ -206,4 +206,8 @@ class TestApiFromAutomatedClient(TestServerTestCase):
# delete and check that its 'loaded' state changes
self.assertTrue(self._get(f'accessors/{acc_id}').json()['loaded'])
self.assertEqual(self._get(f'accessors/delete/{acc_id}').json(), acc_id)
self.assertFalse(self._get(f'accessors/{acc_id}').json()['loaded'])
\ No newline at end of file
self.assertFalse(self._get(f'accessors/{acc_id}').json()['loaded'])
# and try a non-existent accessor ID
resp_wrong_acc = self._get('accessors/auto_123456')
self.assertEqual(resp_wrong_acc.status_code, 404)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment