To demonstrate writing a client, we will write a python script that adds a few users to Sakai, and then creates a site for them. First, the entire contents of the example script.
import os import sys from SOAPpy import WSDL loginsoap = WSDL.SOAPProxy("http://localhost:8080/sakai-axis/SakaiLogin.jws?wsdl") scriptsoap = WSDL.SOAPProxy("http://localhost:8080/sakai-axis/SakaiScript.jws?wsdl") sessionid = loginsoap.login("admin", "admin") print scriptsoap.addNewUser(sessionid, "teacher", "Bob", "Smith", "bob@pedagogy.edu", "", "password" ) print scriptsoap.addNewUser(sessionid, "student1", "Laura", "Hope", "laura@pedagogy.edu", "", "password" print scriptsoap.addNewUser(sessionid, "student2", "Sam", "Goodman", "bob@pedagogy.edu", "", "password" ) print scriptsoap.addNewSite(sessionid, "psych101", "Psych 101 ", "Psychology", \ "Psych", "", "", True, "access", True, True, "", "" ) print scriptsoap.addNewPageToSite(sessionid, "psych101", "Announcements", 0) print scriptsoap.addNewToolToPage(sessionid, "psych101", "Announcements", "Announcements", "sakai.announcements", "") print scriptsoap.addMemberToSiteWithRole(sessionid, "psych101", "teacher", "maintain") print scriptsoap.addMemberToSiteWithRole(sessionid, "psych101", "student1", "access") print scriptsoap.addMemberToSiteWithRole(sessionid, "psych101", "student2", "access")
Running this script on the command line will show results something like:
sgithens@thumbtack$ python QuickExample.py success success success success success success success success sgithens@thumbtack$
Results of QuickExample.py
Writing a script like the one above typically requires the following steps:
Creating SOAP Proxies
Authenticating, getting a Session ID
Performing the work
In our case, we create two soap proxies. One of them is to the SakaiLogin web service that lets us login and returns a Session Id. The second is to the SakaiScript web service containing the methods we need to perform useful work. For Sakai 2.1, these are the primary web services use you'll use (unless you create your own). The code to create these proxies is:
loginsoap = WSDL.SOAPProxy("http://localhost:8080/sakai-axis/SakaiLogin.jws?wsdl") scriptsoap = WSDL.SOAPProxy("http://localhost:8080/sakai-axis/SakaiScript.jws?wsdl")
Next we authenticate to get a Session Id. A Session Id isn't strictly necessary to use Sakai Web Services. The majority of web service methods will take a Session Id since most parts of Sakai are restricted to users with certain permissions. To get a Session Id we use the SakaiLogin webservice:
sessionid = loginsoap.login("admin", "admin")
We use the admin user account in order to have permission for adding Users and Sites to the system.
With our SOAP Proxies and Session ID in hand, we can now use methods from SakaiScript to add Users and Sites to our Sakai installation. Adding Users:
print scriptsoap.addNewUser(sessionid, "teacher", "Bob", "Smith", "bob@pedagogy.edu", "", "password" ) print scriptsoap.addNewUser(sessionid, "student1", "Laura", "Hope", "laura@pedagogy.edu", "", "password" print scriptsoap.addNewUser(sessionid, "student2", "Sam", "Goodman", "bob@pedagogy.edu", "", "password" )
This adds 3 users to the system and prints the results (hopefully success). The Session Id is from our login web service. Try using this script and substituting "admin" with a normal Sakai user and their password. It will fail. The methods also takes parameters for User ID, Firstname, Lastname, email, type, and password. We can leave the type blank. Some Sakai installations use this field for special purposes.
The next few lines create a Site, and a Page with a Tool for the site.
print scriptsoap.addNewSite(sessionid, "psych101", "Psych 101 ", "Psychology", \ "Psych", "", "", True, "access", True, True, "", "" ) print scriptsoap.addNewPageToSite(sessionid, "psych101", "Announcements", 0) print scriptsoap.addNewToolToPage(sessionid, "psych101", "Announcements", "Announcements", "sakai.announcements", "")
The first thing to note is that we leave a lot of parameters blank. They are most or less optional for the purposes of this excercise, and some of their descriptions are out of the scope of a 20 minute getting started guide.
The first call adds a new Site with the ID "psych101". The other strings offer various labels for the course. See the Javadoc'd entries on SakaiScript.jws for a full description of the parameters. (note: I am still JavaDoc'ing it).
Next we add a new page. A page is one of the entries that shows up on the Sidebar in Sakai. We then add an Announcement tool to the page. For both methods we use the Site ID, "pysch101" to indicate which Site we are adding pages and tools to. For Pages and Tools though, the names of the ID's are not always so clear, so we use the human readable label, "Announcements" to add them. This is not the cleanest approach, but it works for now. Just be sure not to make multiple Pages with the same label, or multiple tools on a page with the same label. "sakai.announcements" is the type of tool we are adding.
Lastly, we add the users to the newly created site.
print scriptsoap.addMemberToSiteWithRole(sessionid, "psych101", "student1", "access") print scriptsoap.addMemberToSiteWithRole(sessionid, "psych101", "student2", "access")
The second parameter is the Id of the Site we created, the 3rd that of the user that is being added to the site. The last parameter contains the name of the role for the new user. For this exercise we give intructors the "maintain" role, and students the "access" role.