Skip to content

Commit

Permalink
#135: Add Python ICAT decorator to ensure valid session ID
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Jun 22, 2020
1 parent 93d2305 commit 168ba94
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions common/python_icat_helpers.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 168ba94

Please sign in to comment.