I'm a Java developer currently trying to write my own Python library. I have most of the functionality (that I want) down, I'm just having trouble with some Python syntax intricacies. I thought I had working code but when I tried an example I got this error:
/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/willkara/Development/GitHub/SakaiPy/Examples/Example.py
Traceback (most recent call last):
File "/Users/willkara/Development/GitHub/SakaiPy/Examples/Example.py", line 14, in <module>
site = Sak.getSakaiSite('1e420647-1dce-4dbb-a789-9dfae3ccc8d8')
File "/Users/willkara/Development/GitHub/SakaiPy/SakaiPy/SakaiPy.py", line 28, in getSakaiSite
return SakaiSite.SakaiSite(self, self.requester, siteid)
TypeError: __init__() takes 3 positional arguments but 4 were given
My goal is to have this base object SakaiPy that is the main class for the library that handles most of the high level logic.
I want user to be able to do this at the end after user imports the project.
SakaiPyObject = SakaiPy(connectioninfo)
site = SakaiPyObject.getSakaiSite('site id goes here')
announcements = site.getAnnouncements()
I have most of classes & functions setup, I just am having trouble with some of the differences between Python and Java in how they handle constructors/methods/imports.
This is my code for the SakaiPy object:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
from SakaiPy import RequestHandler
from SakaiPy.SakaiTools import SakaiSite
loginURL = "/direct/session?_username={0}&_password={1}"
session = requests.Session()
class SakaiPy(object):
def __init__(self, connectioninfo):
"""Generate the session cookie"""
self.baseURL = connectioninfo['baseURL']
# Generate a session cookie & store it in the requesting session
session.post(self.baseURL + loginURL.format(
connectioninfo['username'],
connectioninfo['password']
))
self.requester = RequestHandler.RequestHandler(session, self.baseURL)
def getSakaiSite(self, siteid):
return SakaiSite.SakaiSite(self, self.requester, siteid)
For the RequestHandler object:
#!/usr/bin/python
# -*- coding: utf-8 -*-
class RequestHandler(object):
def __init__(self, session, url):
self.baseUrl = url
self.session = session
"""This class handles the login/cookie mechanisms and request generation."""
def executeRequest(self, url):
"""Returns the JSON response from the specified URL."""
response = self.session.get(self.baseURL + url)
"""If it is a good response, then return the content in json form for the Sakai Object.
If it is a bad response, raise an exception.
"""
if response.status_code == 200:
return response.json()
else:
response.raise_for_status()
And for the SakaiSite object:
#!/usr/bin/python
# -*- coding: utf-8 -*-
class SakaiSite(object):
def __init__(self, rq, siteid):
self.requester = rq
self.siteid = siteid
def getMemership(self):
return self.requester.executeRequest('/direct/membership/site/{0}.json'.format(self.siteid))
def getRoster(self):
return self.requester.executeRequest('/direct/roster/site/{0}.json'.format(self.siteid))
def getGradebook(self):
return self.requester.executeRequest('/direct/gradebook/site/{0}.json'.format(self.siteid))
def getNews(self):
return self.requester.executeRequest('/direct/news/site/{0}.json'.format(self.siteid))
def getCalendar(self):
return self.requester.executeRequest('/direct/calendar/site/{0}.json'.format(self.siteid))
def getAnnouncements(self):
return self.requester.executeRequest('/direct/announcement/site/{0}.json'.format(self.siteid))
def getForums(self):
return self.requester.executeRequest('/direct/forums/site/{0}.json'.format(self.siteid))
def getContents(self):
return self.requester.executeRequest('/direct/content/site/{0}.json'.format(self.siteid))
and finally, here is the demo code I have:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from SakaiPy import SakaiPy
"""Say I want to get a list of all of the Calendars I have for a specific site. I'll write all the code first then explain each part."""
authInfo = {}
authInfo['baseURL'] = "https://sakai.rutgers.edu"
authInfo['username'] = "username"
authInfo['password'] = "password"
Sak = SakaiPy.SakaiPy(authInfo)
site = Sak.getSakaiSite('siteid')
site.getAnnouncements()