[Building Sakai] I made a Python RESTful interface to Sakai: SakaiPy

Paul Lukasewych plukasew at uwo.ca
Mon Nov 24 08:18:00 PST 2014


I've used this successfully once or twice with Python 3:

import urllib.request                                                                         
import http.cookiejar

def encodeParams(params):                                                                     
    return urllib.parse.urlencode(params).encode("utf-8")                                    
                                                                                                
def addFormHeader(request):                                                                   
    request.add_header("Content-Type", "application/x-www-form-
urlencoded;charset=" + encoding)
                                                                                                
def printResponse(connection):                                                                
    print(connection.geturl())                                                                
    code = connection.getcode()                                                               
    print("Status: " + str(code))                                                             
    response = connection.read().decode(encoding)                                             
    print(response)                                                                           
    return response

 request = urllib.request.Request("http://localhost:8080/direct/session")                                      
addFormHeader(request) # we need this for a POST request
 
cookieJar = http.cookiejar.CookieJar()
opener = 
urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookieJar))
 
username = "admin"                                                                            
password = "admin"
params = encodeParams({"_username": username, "_password": password})
 
try:
    connection = opener.open(request, data=params) # data is passed in, Python  
will make this a POST
    printResponse(connection)
except urllib.error.HTTPError as e:
    print(e)

Paul Lukasewych
Applications Development Team
Information Technology Services
Support Services Building
Western University
(519) 661-2111 x80513
plukasew at uwo.ca

On November 24, 2014 11:08:35 William Karavites wrote:
> Just confirmed it DOESN'T work on Python3 (for me at least).
> 
> The module it uses to login in to the system (mechanize) is not supported
> for Python3 yet. If there is another way to login and grab the cookie, I'd
> be more than happy to use it. So it looks like it is only 2.x supported.
> I'll have to fix the versions @ the PyPi page.
> 
> -Will
> 
> ------------------------------------
> William Karavites
> Application Programmer
> OIT/OIRT- Rutgers University
> Office: 848-445-8726
> Cell: 732-822-9405
> willkara at rutgers.edu
> http://willkara.com
> ------------------------------------
> 
> ----- Original Message -----
> 
> From: "William Karavites" <willkara at oit.rutgers.edu>
> To: "Paul Lukasewych" <plukasew at uwo.ca>
> Cc: sakai-dev at collab.sakaiproject.org
> Sent: Monday, November 24, 2014 10:52:05 AM
> Subject: Re: [Building Sakai] I made a Python RESTful interface to Sakai:
> SakaiPy
> 
> It SHOULD work in Python 3 though I'm trying to make it so that'll work in
> 2.x & 3.x. It would be great to have it be Python version agnostic, but if
> there's a piece of functionality that requires it to be in Python3, so be
> it.
> 
> I haven't been able to do much testing on different Python versions yet so
> I'll have to get back to you on that one.
> 
> ------------------------------------
> William Karavites
> Application Programmer
> OIT/OIRT- Rutgers University
> Office: 848-445-8726
> Cell: 732-822-9405
> willkara at rutgers.edu
> http://willkara.com
> ------------------------------------
> 
> ----- Original Message -----
> 
> From: "Paul Lukasewych" <plukasew at uwo.ca>
> To: sakai-dev at collab.sakaiproject.org
> Cc: "William Karavites" <willkara at oit.rutgers.edu>
> Sent: Monday, November 24, 2014 10:49:20 AM
> Subject: Re: [Building Sakai] I made a Python RESTful interface to Sakai:
> SakaiPy
> 
> This looks handy. Any plans to port it to Python 3?
> 
> Paul Lukasewych
> Applications Development Team
> Information Technology Services
> Support Services Building
> Western University
> (519) 661-2111 x80513
> plukasew at uwo.ca
> 
> On November 21, 2014 21:53:44 William Karavites wrote:
> > Hello everyone,
> > 
> > As I've recently been teaching myself Python I thought it would be a great
> > idea to try and incorporate Sakai with that. I recently had finished the
> > first iteration of what I call, SakaiPy, the Python RESTful interface to
> > Sakai.
> > 
> > It's a slow work in progress but I want it to be a fully capable interface
> > to any/all of the RESTful endpoints on Sakai. As of now, it simply returns
> > basic JSON structures but my goal is to have it be a full interface to
> > getting information out of and into Sakai.
> > 
> > The way I have it set up now, it should be able to be used on any Sakai
> > instance (just follow the instructions).
> > 
> > 
> > I have a blog post here talking about it:
> > http://willkara.com/sakaipy-the-sakai-python-restful-interface/
> > 
> > and its GitHub repo is available here if anyone wants to improve it
> > (please
> > do): https://github.com/willkara/SakaiPy
> > 
> > Please let me know what you think and if there are any improvements to be
> > made!
> > 
> > -William Karavites
> > 
> > ------------------------------------
> > William Karavites
> > Application Programmer
> > OIT/OIRT- Rutgers University
> > Office: 848-445-8726
> > Cell: 732-822-9405
> > willkara at rutgers.edu
> > http://willkara.com
> > ------------------------------------
> 
> _______________________________________________
> sakai-dev mailing list
> sakai-dev at collab.sakaiproject.org
> http://collab.sakaiproject.org/mailman/listinfo/sakai-dev
> 
> TO UNSUBSCRIBE: send email to sakai-dev-unsubscribe at collab.sakaiproject.org
> with a subject of "unsubscribe"



More information about the sakai-dev mailing list