Skip to content
Snippets Groups Projects
Commit 0799f676 authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-60: added open/close methods, use them in __enter__/__exit__ methods....

TMSS-60: added open/close methods, use them in __enter__/__exit__ methods. Added factory method to create a TMSSSession from the credentials in a DBCredentials file.
parent b55d2990
Branches
Tags
1 merge request!154Resolve TMSS-60 and TMSS-171 and TMSS-198
import logging
import requests
import os
import logging
logger = logging.getLogger(__file__)
# usage example:
#
# with TMSSsession('paulus', 'pauluspass', 'http://localhost') as session:
# with TMSSsession('paulus', 'pauluspass', 'localhost', 8000) as session:
# response = session.get(url='http://localhost/api/task_draft/')
# print(response)
......@@ -16,20 +17,52 @@ class TMSSsession(object):
OPENID = "openid"
BASICAUTH = "basicauth"
def __init__(self, username, password, host, authentication_method=OPENID):
def __init__(self, username, password, host, port: int=8000, authentication_method=OPENID):
self.session = requests.session()
self.username = username
self.password = password
self.host = host
self.base_url = "http://%s:%d/api" % (host, port)
self.authentication_method = authentication_method
@staticmethod
def create_from_dbcreds_for_ldap(dbcreds_name: str=None):
'''Factory method to create a TMSSSession object which uses the credentials in the ~/.lofar/dbcredentials/<dbcreds_name>.ini file
(mis)use the DBCredentials to get a url with user/pass for tmss
the contents below are used to contruct a url like this: http://localhost:8000/api
[database:TMSS]
host=localhost
user=<username>
password=<password>
type=http
port=8000
'''
if dbcreds_name is None:
dbcreds_name = os.environ.get("TMSS_CLIENT_DBCREDENTIALS", "TMSSClient")
from lofar.common.dbcredentials import DBCredentials
dbcreds = DBCredentials().get(dbcreds_name)
return TMSSsession(username=dbcreds.user, password=dbcreds.password,
host=dbcreds.host,
port=dbcreds.port,
authentication_method=TMSSsession.BASICAUTH)
def __enter__(self):
self.open()
# return the request session for use within the context
return self.session
def __exit__(self, type, value, traceback):
self.close()
def open(self):
'''open the request session and login'''
self.session.__enter__()
self.session.verify = False
if self.authentication_method == self.OPENID:
# get authentication page of OIDC through TMSS redirect
response = self.session.get(self.host + '/oidc/authenticate/', allow_redirects=True)
response = self.session.get(self.base_url.replace('/api', '/oidc/authenticate/'), allow_redirects=True)
csrftoken = self.session.cookies['csrftoken']
# post user credentials to login page, also pass csrf token
......@@ -45,14 +78,17 @@ class TMSSsession(object):
if self.authentication_method == self.BASICAUTH:
self.session.auth = (self.username, self.password)
# return the authenticated session as user context
return self.session
def __exit__(self, type, value, traceback):
def close(self):
'''close the request session and logout'''
try:
# logout user
self.session.get(self.host + '/api/logout/', allow_redirects=True)
self.session.__exit__(self, type, value, traceback)
self.session.get(self.base_url + '/logout/', allow_redirects=True)
self.session.close()
except:
pass
def set_subtask_status(self, subtask_id: int, status: str) -> requests.Response:
'''set the status for the given subtask'''
return self.session.patch(url='%s/subtask/%s/' % (self.base_url, subtask_id),
json={'state': "%s/subtask_state/%s/" % (self.base_url, status)})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment