From 168ba94b07806e8e15c240a369fbeac74e70a443 Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Mon, 22 Jun 2020 16:33:40 +0000 Subject: [PATCH] #135: Add Python ICAT decorator to ensure valid session ID --- common/python_icat_helpers.py | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/common/python_icat_helpers.py b/common/python_icat_helpers.py index e69de29b..15188523 100644 --- a/common/python_icat_helpers.py +++ b/common/python_icat_helpers.py @@ -0,0 +1,41 @@ +from functools import wraps +import logging +from datetime import datetime, timedelta + + +from icat.exception import ICATSessionError +from common.exceptions import AuthenticationError + +log = logging.getLogger() + +def requires_session_id(method): + """ + Decorator for Python ICAT backend methods that looks out for session errors when using the API. + The API call runs and an ICATSessionError may be raised due to an expired session, invalid + session ID etc. + It expects that session_id is the second argument supplied to the function + :param method: The method for the backend operation + :raises AuthenticationError, if a valid session_id is not provided with the request + """ + + @wraps(method) + def wrapper_requires_session(*args, **kwargs): + try: + return method(*args, **kwargs) + except ICATSessionError: + raise AuthenticationError("Forbidden") + + return wrapper_requires_session + + +def queries_records(method): + """ + Docstring + """ + + @wraps(method) + def wrapper_gets_records(*args, **kwargs): + pass + + return wrapper_gets_records +