summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkballou <kballou@devnulllabs.io>2015-04-10 14:47:34 -0600
committerkballou <kballou@devnulllabs.io>2015-04-10 18:03:43 -0600
commit243acb83f520cb5cf18de2095d63e08ff086969c (patch)
tree1b55aaa316a36613380412b95525df88abae19a8
parentcb884b11cdcf75a8b5ad82fe16069df6b2c1b1e2 (diff)
downloadpylibchorus-243acb83f520cb5cf18de2095d63e08ff086969c.tar.gz
pylibchorus-243acb83f520cb5cf18de2095d63e08ff086969c.tar.xz
Refactor: Move ChorusSession to pylibchorus
In moving ChorusSession to the top level of pylibchorus, we are also moving/exposing API functions.
-rw-r--r--pylibchorus/__init__.py31
-rw-r--r--pylibchorus/chorus_client.py77
2 files changed, 67 insertions, 41 deletions
diff --git a/pylibchorus/__init__.py b/pylibchorus/__init__.py
index 7e473c2..67fdd10 100644
--- a/pylibchorus/__init__.py
+++ b/pylibchorus/__init__.py
@@ -2,5 +2,36 @@
'''PyLibChorus -- Python Chorus API Library'''
import logging
+from pylibchorus.chorus_client import login
+from pylibchorus.chorus_client import logout
+from pylibchorus.chorus_client import check_login_status
LOG = logging.getLogger(__name__)
+
+#pylint: disable=R0903
+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'''
+
+ LOG.debug("Opening Chorus Session")
+ post = login(
+ self.config.get('alpine', 'username'),
+ self.config.get('alpine', 'password'),
+ self)
+ json = post.json()
+
+ self.sid = json['response']['session_id']
+ self.cookies = post.cookies
+ return self
+
+ def __exit__(self, _type, _value, _traceback):
+ '''Close chorus session'''
+ LOG.debug("Closing Chorus Session")
+ logout(self)
diff --git a/pylibchorus/chorus_client.py b/pylibchorus/chorus_client.py
index ff90e77..dbacea9 100644
--- a/pylibchorus/chorus_client.py
+++ b/pylibchorus/chorus_client.py
@@ -9,47 +9,42 @@ 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, session):
+ '''POST login request to chorus server'''
+ return _perform_http_method_(
+ session.config.get('alpine', 'host'), _login_(username, password))
+
+def logout(session):
+ '''DELETE login request to chorus server'''
+ return _perform_http_method_(
+ session.config.get('alpine', 'host'),
+ _logout_(session.sid, session.cookies))
+
+def check_login_status(session):
+ '''GET login request to chorus server'''
+ return _perform_http_method_(
+ session.config.get('alpine', 'host'),
+ _check_login_(session.sid, session.cookies))
+
+def _get_url_(host, endpoint=""):
+ '''Return the host and path for the chorus instance'''
+ return "http://%s/%s" % (host, endpoint)
+
+def _perform_http_method_(host, request_data):
+ '''Perform IO operation to Chorus Server using request_data object'''
+ methods = {'GET': requests.get,
+ 'POST': requests.post,
+ 'DELETE': requests.delete,}
+ method = methods[request_data['method']]
+ response = method(_get_url_(host, request_data['url']),
+ params=request_data['params'],
+ headers=request_data['headers'],
+ cookies=request_data['cookies'],
+ data=request_data['data'])
+ LOG.info("Request: %s status code: %d",
+ request_data['url'],
+ response.status_code)
+ return response
def _login_(username, password):
'''Create Request Data for ChorusSession'''