Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (67 sloc) 2.75 KB
#!/usr/bin/env python3
Skeleton to use Dundas Rest API, with guaranteed log out.
import contextlib
import logging
import requests
import sys
class DundasSession:
Using __new__ + contextlib.closing() is awesome.
DundasSession can now be used within a context manager, meaning that whatever happens, its close() method
will be called (thus logging out). No need to call logout() explicitely!
Just using __del__ is not guaranteed to work because when __del__ is called, you do not know which objects
are already destroyed, and the session object might well be dead.
def __new__(cls, *args, **kwargs):
o.__init__(*args, **kwargs)
return contextlib.closing(o)
def __init__(self, user, pwd, url):
# For session reuse - TCP connection reuse, keeps cookies.
self.s = requests.session()
self.user = user
self.pwd = pwd
self.url = url
self.api = self.url + '/api/'
self.session_id = None # Will bet set in login()
def login(self):
"""Login and returns the session_id"""
login_data = {
'accountName': self.user,
'password': self.pwd,
'deleteOtherSessions': False,
'isWindowsLogOn': False
}'Logging in.')
r = + 'logon/', json=login_data)
# The following line exceptions out on not 200 return code.
resp = r.json()
if resp['logOnFailureReason'].lower() == "none":
# We're in!'Logged in')
self.session_id = resp['sessionId']
logging.error('Login failed with message: ' + r.text)
def close(self):
"""Automagically called by the context manager."""
def logout(self):
"""If you do not logout, session will stay active, potentially burning through your elastic hours very fast."""
# If session_id is not defined, we did not even log in (or we are already logged out).'Logging out.')
if getattr(self, 'session_id', None):
r = self.s.delete(self.api + 'session/current', params={'sessionId': self.session_id})
del self.session_id'Logged out.')
else:'Was not yet Logged in.')
with DundasSession(user='yourapiuser', pwd='pwd', url='') as dundas:
# Do something smart with your Dundas object.
# No need to log out, this is handled for you via the context manager, even in case of exception or even sys.exit.