| Name | ZSI Client for SOAP Controller with Optio's Soaplib and SQLAlchemy |
|---|---|
| Space | Pylons Cookbook |
| Section | Controllers |
| Page | ZSI Client for SOAP Controller with Optio's Soaplib and SQLAlchemy |
| Version | 1.0 |
| Status | Draft |
| Reviewed | False |
| Author(s) | Lysander David |
Introduction
In order to consume soaplib webservices without requiring the distribution
of the python source, it is possible to convert the wsdl for the service
into python using ZSI (http://sourceforge.net/projects/pywebsvcs).
This also assumes that the service specified in
SOAP Controller with Optio's Soaplib and SQLAlchemy is running.
Install ZSI
Download the tar.gz file from sourceforge, extract and install ZSI
1 2 3 | wget http://superb-east.dl.sourceforge.net/sourceforge/pywebsvcs/ZSI-2.1-a1.tar.gz
cd ZSI-2.1-a1
python setup.py install
|
Generate Python from WSDL
Create a working directory, create a directory for the generated files then
generate the python files for the service.
1 2 3 | mkdir -p ~/explore/python/pylons/04_user_manager_soap/gen_lib
cd ~/explore/python/pylons/03_hello_soap_client/gen_lib
wsdl2py -b http://localhost:5000/user_manager.wsdl
|
After running these commands you should see these files
1 2 | UserManagerService_client.py UserManagerService_types.py UserManagerService_server.py |
which have been generated from the wsdl file for the soap service.
Execute the SOAP service using the classes generated from the WSDL
Create a file named 01_add_user.py in the parent directory of the
gen_lib directory in order to test adding a user
01_add_user.py
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 | import sys sys.path.insert(0,'gen_lib') from UserManagerService_client import UserManagerServiceLocator, add_user from pprint import pprint as pp from UserManagerService_types import ns0 loc = UserManagerServiceLocator() svc = loc.getUserManagerService() #req = add_user() perm = ns0.Permission_Def('Permission').pyclass() perm._application = 'admin' perm._feature = 'create' print perm.__dict__ perm_array = ns0.PermissionArray_Def('PermissionArray').pyclass() perm_array._Permission.append(perm) print perm_array.__dict__ user = ns0.User_Def('User').pyclass() user._username = 'buser' user._lastname = 'User' user._firstname = 'Bob' user._permissions = perm_array print user.__dict__ req = add_user() req._user = user resp = svc.add_user(req) print resp._retval.__dict__ |
After executing the file, you should see something like this:
1 2 3 4 5 | python 01_add_user.py {'_application': 'admin', '_feature': 'create', '_id': None} {'_Permission': [<UserManagerService_types.Permission_Holder object at 0x846f50c>]} {'_username': 'buser', '_id': None, '_firstname': 'Bob', '_permissions': <UserManagerService_types.PermissionArray_Holder object at 0x846f86c>, '_lastname': 'User'} {'_username': 'buser', '_id': 1, '_firstname': 'Bob', '_permissions': <UserManagerService_types.PermissionArray_Holder object at 0x8479d6c>, '_lastname': 'User'} |
Create a file named 02_get_user.py in the parent directory of the
gen_lib directory in order to test getting a user
02_get_user.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import sys sys.path.insert(0,'gen_lib') from UserManagerService_client import UserManagerServiceLocator, get_user from pprint import pprint as pp from UserManagerService_types import ns0 loc = UserManagerServiceLocator() svc = loc.getUserManagerService() req = get_user() req._user_id = 1 print req.__dict__ resp = svc.get_user(req) print resp._retval.__dict__ |
After executing the file, you should see something like this:
1 2 3 | python 02_get_user.py {'_user_id': 1} {'_username': 'buser', '_id': 1, '_firstname': 'Bob', '_permissions': <UserManagerService_types.PermissionArray_Holder object at 0xb78aa60c>, '_lastname': 'User'} |
Create a file named 01_add_user.py in the parent directory of the
gen_lib directory in order to test modifying a user
03_modify_user.py
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 | import sys sys.path.insert(0,'gen_lib') from UserManagerService_client import UserManagerServiceLocator, get_user, modify_user from pprint import pprint as pp from UserManagerService_types import ns0 loc = UserManagerServiceLocator() svc = loc.getUserManagerService() get_req = get_user() get_req._user_id = 1 print get_req.__dict__ resp = svc.get_user(get_req) print resp._retval.__dict__ new_user = resp._retval new_user._username = 'notbuser' modify_req = modify_user() modify_req._user = new_user print modify_req.__dict__ resp = svc.modify_user(modify_req) print resp.__dict__ |
Open sqlite3 in another window and type this to verify the current state of the database
1 2 3 4 5 6 | sqlite3 development.db SQLite version 3.3.17 Enter ".help" for instructions sqlite> select * from users; 1|buser|Bob|User sqlite> .q |
After executing 03_modify_user.py you should see something like this:
1 2 3 4 5 | python 03_modify_user.py {'_user_id': 1} {'_username': 'buser', '_id': 1, '_firstname': 'Bob', '_permissions': <UserManagerService_types.PermissionArray_Holder object at 0x843662c>, '_lastname': 'User'} {'_user': <UserManagerService_types.User_Holder object at 0x843658c>} {} |
Open sqlite3 in another window and type this to verify the current state of the database
1 2 3 4 5 6 | sqlite3 development.db SQLite version 3.3.17 Enter ".help" for instructions sqlite> select * from users; 1|notbuser|Bob|User sqlite> .q |
Add several other users by running 01_add_user.py two more times
then create 04_list_users.py with these contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import sys sys.path.insert(0,'gen_lib') from UserManagerService_client import UserManagerServiceLocator, list_users from pprint import pprint as pp from UserManagerService_types import ns0 loc = UserManagerServiceLocator() svc = loc.getUserManagerService() req = list_users() print req.__dict__ resp = svc.list_users(req) print resp._retval.__dict__ for user in resp._retval._User: print user.__dict__ |
After executing 04_list_users.py you should see something like this:
1 2 3 4 5 6 | python 04_list_users.py {} {'_User': [<UserManagerService_types.User_Holder object at 0xb786bdac>, <UserManagerService_types.User_Holder object at 0xb786bdcc>, <UserManagerService_types.User_Holder object at 0xb786be6c>]} {'_username': 'notbuser', '_id': 1, '_firstname': 'Bob', '_permissions': <UserManagerService_types.PermissionArray_Holder object at 0xb786be4c>, '_lastname': 'User'} {'_username': 'buser', '_id': 2, '_firstname': 'Bob', '_permissions': <UserManagerService_types.PermissionArray_Holder object at 0xb786bf4c>, '_lastname': 'User'} {'_username': 'buser', '_id': 3, '_firstname': 'Bob', '_permissions': <UserManagerService_types.PermissionArray_Holder object at 0xb77bf06c>, '_lastname': 'User'} |
Sample Code
The sample code can be found in 04_user_manager_soap.tar.gz