4.1. Authentication¶
The session module is an interface to authentication via the
dyn.tm REST API. As noted in the advanced section, DynectSession‘s
are implemented as Singleton types, which means that, in most cases, you don’t need to keep track
of your DynectSession instances after you create them. However,
there are several examples of ways in which you can use these session instances which
will be outlined below.
-
class
dyn.tm.session.DynectSession(customer, username, password, host='api.dynect.net', port=443, ssl=True, api_version='current', auto_auth=True, key=None, history=False)[source]¶ Base object representing a DynectSession Session
-
__init__(customer, username, password, host='api.dynect.net', port=443, ssl=True, api_version='current', auto_auth=True, key=None, history=False)[source]¶ Initialize a Dynect Rest Session object and store the provided credentials
Parameters: - host – DynECT API server address
- port – Port to connect to DynECT API server
- ssl – Enable SSL
- api_version – version of the api to use
- customer – DynECT customer name
- username – DynECT Customer’s username
- password – User’s password
- auto_auth – declare whether or not to automatically log in
- key – A valid AES-256 password encryption key to be used when encrypting your password
- history – A boolean flag determining whether or not you would like to store a record of all API calls made to review later
-
permissions¶ Permissions of the currently logged in user
-
update_password(new_password)[source]¶ Update the current users password
Parameters: new_password – The new password to use
-
uri_root= '/REST'¶
-
4.1.1. The Basics¶
For basic usage, you need not do anything more than simply
>>> from dyn.tm.session import DynectSession
>>> DynectSession('customer', 'user', 'password')
4.1.2. Permissions¶
Using a DynectSession instance, you can also verify
the current permissions associated with your session by simply checking the
permissions property of your DynectSession instance.
>>> from dyn.tm.session import DynectSession
>>> s = DynectSession('customer', 'user', 'password')
>>> s.permissions
[u'ZoneGet', u'ZoneUpdate', u'ZoneCreate', u'ZoneDelete', ...
4.1.3. Additional Features¶
The majority of these features exist mainly to provide a cleaner interface to working with sessions as Singleton types.
4.1.3.1. DynectSession as a Context Manager¶
As of version 1.2.0 you have the ability to use a DynectSession as a context manager, like so
>>> from dyn.tm.session import DynectSession
>>> with DynectSession('customer', 'user', 'password') as s:
... return s.permissions
This feature is particularly useful if you’re looking to manage multiple user accounts programatically.
4.1.3.2. Overriding Sessions¶
As of version 1.2.0 you have the ability to override an existing DynectSession with the use of the new_session class method like so
>>> from dyn.tm.session import DynectSession
>>> s = DynectSession('customer', 'user', 'password')
>>> s = DynectSession.new_session('customer', 'another_user', 'password')
4.1.3.3. Getting Sessions¶
If you don’t want to track your current DynectSession, but want to be able to access your current one later, you can make use of the get_session class method like so
>>> from dyn.tm.session import DynectSession
>>> DynectSession('customer', 'user', 'password')
>>> DynectSession.get_session().username
'user'
4.1.3.4. Session History¶
As of version 1.3.0 users can now optionally allow DynectSessions to store a history of API calls that are made. This can be particularly useful for debugging, as well as for use when contacting Support.
>>> >>> from dyn.tm.session import DynectSession
>>> s = DynectSession('customer', 'user', 'password', history=True)
>>> s.history
... [('2014-10-14T11:15:17.351740',
... '/REST/Session/',
... 'POST',
... {'customer_name': 'customer', 'password': '*****', 'user_name': 'user'},
... u'success')]
Please note that if you do not specify history as True when you log in, that your history will not be recorded and s.history will return None