diff options
author | kballou <kballou@devnulllabs.io> | 2015-04-07 17:59:23 -0600 |
---|---|---|
committer | kballou <kballou@devnulllabs.io> | 2015-04-09 13:35:21 -0600 |
commit | a3d62066a169c26712c30061232146dd46926ff1 (patch) | |
tree | 24868c2adbb68222cc9ba66c77510b41f34f1851 /pylibchorus/chorus_client.py | |
parent | 5deca4625a64c764937b77d677a15b6ce938f700 (diff) | |
download | pylibchorus-a3d62066a169c26712c30061232146dd46926ff1.tar.gz pylibchorus-a3d62066a169c26712c30061232146dd46926ff1.tar.xz |
Add initial skeleton code
* Includes the `ChorusSession` object that underpins all API calls
Diffstat (limited to 'pylibchorus/chorus_client.py')
-rw-r--r-- | pylibchorus/chorus_client.py | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/pylibchorus/chorus_client.py b/pylibchorus/chorus_client.py new file mode 100644 index 0000000..5c7119f --- /dev/null +++ b/pylibchorus/chorus_client.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +'''Alpine/Chorus Client API Module''' + +import logging +import requests + +LOG = logging.Logger(name=__name__) + +CONTENT_TYPE = 'application/x-www-form-urlencoded' +JSON_CONTENT_TYPE = 'application/json' + +class ChorusSession(object): + '''Chorus User Session Object''' + + def __init__(self, config): + self.config = config + self.sid = None + self.cookies = None + + def __enter__(self): + '''create session and return sid and cookies''' + + request_data = _login_(self.config.get('alpine', 'username'), + self.config.get('alpine', 'password')) + + LOG.debug("Opening Chorus Session") + post = requests.post(self.get_url(request_data['url']), + params=request_data['params'], + data=request_data['data'], + headers=request_data['headers']) + LOG.info("Status code for session open: %d", post.status_code) + json = post.json() + + self.sid = json['response']['session_id'] + self.cookies = post.cookies + return self + + def __exit__(self, _type, _value, _traceback): + '''Close chorus session''' + request_data = _logout_(self.sid, self.cookies) + LOG.debug("Closing Chorus Session") + delete = requests.delete(self.get_url(request_data['url']), + params=request_data['params'], + headers=request_data['headers'], + cookies=request_data['cookies']) + + LOG.info("Status code for close: %d", delete.status_code) + + def get_url(self, path=""): + '''Return the host and path for the chorus instance''' + + return "http://%s/%s" % (self.config.get('alpine', 'host'), path) + +def _login_(username, password): + '''Create Request Data for ChorusSession''' + return { + 'data': { + 'username': username, + 'password': password, + }, + 'headers': { + 'content-type': CONTENT_TYPE, + }, + 'params': { + 'session_id': '', + }, + 'cookies': None, + 'url': '/sessions?session_id=', + 'method': 'POST', + } + +def _logout_(sid, cookies): + '''Create request data for ChorusSession''' + return { + 'data': None, + 'headers': { + 'content-type': CONTENT_TYPE, + }, + 'params': { + 'session_id': sid, + }, + 'cookies': cookies, + 'url': '/sessions', + 'method': 'DELETE', + } |