summaryrefslogtreecommitdiff
path: root/pylibchorus/__init__.py
blob: 4c6117132ed0debac6e71924749cccfdf47909a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
'''PyLibChorus -- Python Chorus API Library'''

import logging
from pylibchorus.chorus_api import get
from pylibchorus.chorus_api import post
from pylibchorus.chorus_api import put
from pylibchorus.chorus_api import delete

LOG = logging.getLogger(__name__)

#pylint: disable=R0903
class ChorusSession(object):
    '''Chorus User Session Object'''

    def __init__(self, config):
        self.config = config
        self.sid = ''
        self.cookies = {}

    def __enter__(self):
        '''create session and return sid and cookies'''

        LOG.debug("Opening Chorus Session")
        data = {
            'username': self.config.get('alpine', 'username'),
            'password': self.config.get('alpine', 'password'),
        }
        code, json = post('/sessions', self, data)

        if code != 201:
            raise RuntimeError("Chorus Session Login Failed")

        self.sid = json['response']['session_id']
        return self

    def __exit__(self, _type, _value, _traceback):
        '''Close chorus session'''
        LOG.debug("Closing Chorus Session")
        delete('/sessions', self)