-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#135: Add Python ICAT decorator to ensure valid session ID
- Loading branch information
1 parent
93d2305
commit 168ba94
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|