Skip to main content

Manage sessions for Dundas api.

Project description

Manage sessions for Dundas.

Description

Dundas has a very complete REST API. You need a user to use it, and if you forget to log out, you will burn through your elastic hours very quickly.

In short, Dundas is very friendly and lets you have more users than paid for logged in at the same time (elastic hours), but you should not abuse it (if you burn through them, you are blocked).

Always be sure to be logged out, even in case of exception or multiple path is a pain. This is the idea behind this module. You will not need to remember yourself to log out, it will be done for you, in all cases if you so wish.

Why this module is useful

It currently does 3 things for you.

If you use dundas.Session within a context manager, the context manager wil log you in and out automagically, no matter what happens. You can use the session object as a normal object as well as long as you do not forget to log in and out yourself.

Each and every call to the API needs to have the same sessionId parameter. This module creates shortcuts for you for get, post and delete, to make your life easier. You do not need to repeat the host, api path prefix or sessionId every single time.

Some API calls are ported and might have helper methods. I am updating the module based on what I need and use, so I do not expect to have everything ported on my own.

Installation

Simply with pip:

python3 -m pip install pydundas

or, assuming you do not have permission to store the module globally:

python3 -m pip install --user pydundas

The module should be able to work with python2 as well, but it is untested and as python2 will be end of life'd in a few months anyway I did not look into it.

Examples

You can see all the examples as one python file.

You can use pydundas as a context manager or as a normal python object. The context manager makes it impossible for you to forget to log out.

All the examples below assume a url, user and pwd variables.

Happy flow with context manager

with Session(user=user, pwd=pwd, url=url) as d:
    print(d.get('Server').text)

Output (example):

[{"name":"winterfell","serverGroupId":1,"lastSeenTime":"2019-03-29T09:33:38.880327Z","__classType":"dundas.configuration.ServerInfo"}]

When the variable d comes out of scope, so outside the with statement, you will be automagically logged out.

Change loglevel

with Session(user=user, pwd=pwd, url=url, loglevel='debug') as d:
    print(d.get('Server').text)

You will have the same output as before, with extra statements:

Logging in.
Logged in.
[{"name":"winterfell","serverGroupId":1,"lastSeenTime":"2019-03-29T09:33:38.880327Z","__classType":"dundas.configuration.ServerInfo"}]
Logging out.
Logged out.

Note that you can access the logger yourself to tune it to your heart's content.

# Logger object are persistent. Let's restore loglevel to warning.
Session.setLogLevel('warning')

# Actually work on the logger
logger = logging.getLogger('pydundas.dundas')

Read credentials from a yaml file

If you have a yaml file with a user, pwd and url key, then you can read it from pydundas:

user: arya
pwd: 'valar morghulis'
url: winterfell.got
from pydundas import creds_from_yaml
creds=creds_from_yaml('credentials.yaml')
with Session(**creds) as d:
    print(d.get('Server').text)

Exception within the context manager are properly handled

with Session(user=user, pwd=pwd, url=url) as d:
        d.get('you/know/nothing')

output:

404 Client Error: Not Found for url: https://winterfell.got/api/you/know/nothing?sessionId=fbeb7897-5981-412b-a981-7783f88894bd

You are still automagically logged out.

Wrong credentials

with Session(user=user, pwd='valar dohaeris', url=url) as d:
        d.get('Server')

will give you:

{"logOnFailureReason":"UnrecognizedCredentials","message":"The provided user credentials were not recognized."}

Full control without context manager

You can do it, but do not forget to log in/out yourself:

d = Session(user=user, pwd=pwd, url=url)
d.login()
print(d.get('Server').text)
d.logout()

You will get, as with the first example:

[{"name":"winterfell","serverGroupId":1,"lastSeenTime":"2019-03-29T09:33:38.880327Z","__classType":"dundas.configuration.ServerInfo"}]

No context manager, object reuse

No context manager and you reuse a logged-out Dundas session object. Nothing prevents you to log in again:

# d comes from the previous example, for instance.
d.login()
print(d.get('Server').text)
d.logout()

with the same output as previously.

No context manager, forget to log in

d = Session(user=user, pwd=pwd, url=url)
# Oops, no login!
print(d.get('Server').text)
d.logout()

You will get:

440 Client Error:  for url: https://reports.webpower.io/api/Server

The same would happen if you reuse an object after logging out.

No context manager, forget to log out

I'm not that mean and I won't burn through your elastic hours, but be careful and that's why context the manager is awesome.

API calls

For example, to find the ID of a project:

from pydundas import Api, Session, creds_from_yaml

with Session(**creds_from_yaml('credentials.yaml')) as d:
    a=Api(d)
    project = a.project()
    print(project.getProjectIDByName('DP'))

Develop

You can either use conda or virtualenv. Most relevant commands are in the Makefile. First edit the first line of the makefile to choose if you want to use conda or virtualenv.

# Build an environment with all dependencies
make devinit

# Tests
make pep8
make unittest

# Build a package
make package

# Clean up everything
make purge

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydundas-1.3.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydundas-1.3.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file pydundas-1.3.1.tar.gz.

File metadata

  • Download URL: pydundas-1.3.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pydundas-1.3.1.tar.gz
Algorithm Hash digest
SHA256 3d3948cb7ad237e42eca42c19a3360bbefdf134e01ed26cb3763a920a0aa9958
MD5 e1bef839329849b8aa43e3a796c7ae64
BLAKE2b-256 6a400248fc6ac823dfbf63c4ef56b410bbe96b06285935a3e2e909b66e109552

See more details on using hashes here.

File details

Details for the file pydundas-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: pydundas-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pydundas-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2ed88e342377f4c81f3032214a1b49dd7b0babefe340cf54cbfe870420aae973
MD5 216022a533c1ae40b5a70e17183a2f4a
BLAKE2b-256 4fe2dc15f96f957a50173c1ff4ae73652c78259ae1abdd9b8f953fc744867259

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page