summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-03-13 16:17:34 (GMT)
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-03-13 16:17:34 (GMT)
commit812920a2077860091bd003956fdf4296d187b329 (patch)
tree29af52cec5113e4ab19d4488f7b2b480b7ea1ec4
parent3ee814c43398756b1756628ab8e1f9d09e1acec6 (diff)
downloadpykolab-812920a2077860091bd003956fdf4296d187b329.tar.gz
Split out connect.py and request.py
-rw-r--r--pykolab/wap_client/__init__.py31
-rw-r--r--pykolab/wap_client/connect.py9
-rw-r--r--pykolab/wap_client/request.py29
3 files changed, 56 insertions, 13 deletions
diff --git a/pykolab/wap_client/__init__.py b/pykolab/wap_client/__init__.py
index 5462961..c7c4115 100644
--- a/pykolab/wap_client/__init__.py
+++ b/pykolab/wap_client/__init__.py
@@ -16,6 +16,9 @@ session_id = None
conn = None
+from connect import connect
+from request import request
+
def authenticate(username=None, password=None):
global session_id
@@ -92,23 +95,23 @@ def get_group_input():
def get_user_input():
user_types = user_types_list()
- if len(user_types.keys()) > 1:
- for key in user_types.keys():
+ if user_types['count'] > 1:
+ for key in user_types['list'].keys():
if not key == "status":
- print "%s) %s" % (key,user_types[key]['name'])
+ print "%s) %s" % (key,user_types['list'][key]['name'])
user_type_id = utils.ask_question("Please select the user type")
- elif len(user_types.keys()) > 0:
+ elif user_types['count'] > 0:
print "Automatically selected the only user type available"
- user_type_id = user_types.keys()[0]
+ user_type_id = user_types['list'].keys()[0]
else:
print "No user types available"
sys.exit(1)
- if user_types.has_key(user_type_id):
- user_type_info = user_types[user_type_id]['attributes']
+ if user_types['list'].has_key(user_type_id):
+ user_type_info = user_types['list'][user_type_id]['attributes']
else:
print "No such user type"
sys.exit(1)
@@ -117,6 +120,8 @@ def get_user_input():
'user_type_id': user_type_id
}
+ print user_type_info
+
for attribute in user_type_info['form_fields'].keys():
params[attribute] = utils.ask_question(attribute)
@@ -170,8 +175,8 @@ def request(method, api_uri, params=None, headers={}):
response = conn.getresponse()
data = response.read()
- #print method, api_uri, params
- #print data
+ print method, api_uri, params
+ print data
try:
response_data = json.loads(data)
@@ -180,7 +185,7 @@ def request(method, api_uri, params=None, headers={}):
print "Response data is not JSON"
sys.exit(1)
- #print response_data
+ print response_data
if response_data['status'] == "OK":
del response_data['status']
@@ -253,8 +258,8 @@ def user_form_value_generate_mail(params=None):
return request('POST', 'user_form_value.generate_mail', params)
-def user_form_value_generate_password(*args, **kw):
- return request('GET', 'user_form_value.generate_password')
+def form_value_generate_password(*args, **kw):
+ return request('GET', 'form_value.generate_password')
def user_form_value_generate_uid(params=None):
if params == None:
@@ -265,7 +270,7 @@ def user_form_value_generate_uid(params=None):
return request('POST', 'user_form_value.generate_uid', params)
def user_form_value_generate_userpassword(*args, **kw):
- result = user_form_value_generate_password()
+ result = form_value_generate_password()
return { 'userpassword': result['password'] }
def user_info():
diff --git a/pykolab/wap_client/connect.py b/pykolab/wap_client/connect.py
new file mode 100644
index 0000000..1c4c7ad
--- /dev/null
+++ b/pykolab/wap_client/connect.py
@@ -0,0 +1,9 @@
+def connect():
+ global conn
+
+ if conn == None:
+ conn = httplib.HTTPConnection(API_HOSTNAME, API_PORT)
+ conn.connect()
+
+ return conn
+
diff --git a/pykolab/wap_client/request.py b/pykolab/wap_client/request.py
new file mode 100644
index 0000000..0613333
--- /dev/null
+++ b/pykolab/wap_client/request.py
@@ -0,0 +1,29 @@
+def request(method, api_uri, params=None, headers={}):
+ global session_id
+
+ if not session_id == None:
+ headers["X-Session-Token"] = session_id
+
+ conn = connect()
+ conn.request(method.upper(), "%s/%s" % (API_BASE,api_uri), params, headers)
+ response = conn.getresponse()
+ data = response.read()
+
+ print method, api_uri, params
+ print data
+
+ try:
+ response_data = json.loads(data)
+ except ValueError, e:
+ # Some data is not JSON
+ print "Response data is not JSON"
+ sys.exit(1)
+
+ print response_data
+
+ if response_data['status'] == "OK":
+ del response_data['status']
+ return response_data['result']
+ else:
+ return response_data['result']
+