Permalink
Browse files

code style

  • Loading branch information...
1 parent 17ea7d0 commit a84da3f87bfc1f36e56f39e9dad896932b9480a4 @emirotin emirotin committed Nov 1, 2011
Showing with 58 additions and 75 deletions.
  1. +58 −75 src/py_etherpad.py
View
@@ -2,41 +2,36 @@
import urllib2
import json
-
class EtherpadLiteClient:
+ API_VERSION = 1
- API_VERSION = 1
-
- CODE_OK = 0
+ CODE_OK = 0
CODE_INVALID_PARAMETERS = 1
- CODE_INTERNAL_ERROR = 2
- CODE_INVALID_FUNCTION = 3
- CODE_INVALID_API_KEY = 4
+ CODE_INTERNAL_ERROR = 2
+ CODE_INVALID_FUNCTION = 3
+ CODE_INVALID_API_KEY = 4
TIMEOUT = 20
apiKey = ""
baseUrl = "http://localhost:9001/api"
- def __init__(self,apiKey = None, baseUrl = None):
+ def __init__(self, apiKey=None, baseUrl=None):
if apiKey:
- self.apiKey = apiKey
+ self.apiKey = apiKey
if baseUrl:
self.baseUrl = baseUrl
- '''
- # No validation of url
- raise Exception("[:self.baseUrl] is not a valid URL")
- '''
+ # No validation of url
+ #raise Exception("[:self.baseUrl] is not a valid URL")
-
- def call(self,function, arguments = {}):
+ def call(self, function, arguments=None):
#Create a dictionary of all parameters
- params = arguments
+ params = arguments or {}
params.update({'apikey': self.apiKey})
- query= urllib.urlencode(params, True)
+ query = urllib.urlencode(params, True)
url = self.baseUrl + "/" + str(self.API_VERSION) + "/" + function + "?" + query
@@ -46,30 +41,24 @@ def call(self,function, arguments = {}):
response = opener.open(request, timeout=self.TIMEOUT)
result = response.read()
response.close()
- except urllib2.HTTPError as e:
+ except urllib2.HTTPError:
raise
- #raise RequestError("Failed to send request: %e" str(e))
-
-
+ #raise RequestError("Failed to send request: %e" str(e))
result = json.loads(result)
- if (result == None):
+ if result is None:
raise ValueError("JSON response could not be decoded")
return self.handleResult(result)
-
- def handleResult(self,result):
+ def handleResult(self, result):
if not 'code' in result:
raise Exception("API response has no code")
-
if not 'message' in result:
raise Exception("API response has no message")
-
if not 'data' in result:
result['data'] = None
-
if result['code'] == self.CODE_OK:
return result['data']
elif result['code'] == self.CODE_INVALID_PARAMETERS or result['code'] == self.CODE_INVALID_API_KEY:
@@ -82,8 +71,6 @@ def handleResult(self,result):
raise Exception("An unexpected error occurred whilst handling the response")
-
-
# GROUPS
# Pads can belong to a group. There will always be public pads that doesnt belong to a group (or we give this group the id 0)
@@ -93,51 +80,51 @@ def createGroup(self):
# this functions helps you to map your application group ids to etherpad lite group ids
- def createGroupIfNotExistsFor(self,groupMapper):
+ def createGroupIfNotExistsFor(self, groupMapper):
return self.call("createGroupIfNotExistsFor", {
"groupMapper": groupMapper
- })
+ })
# deletes a group
- def deleteGroup(self,groupID):
+ def deleteGroup(self, groupID):
return self.call("deleteGroup", {
"groupID": groupID
- })
+ })
# returns all pads of this group
- def listPads(self,groupID):
+ def listPads(self, groupID):
return self.call("listPads", {
"groupID": groupID
- })
+ })
# creates a new pad in this group
- def createGroupPad(self,groupID, padName, text):
+ def createGroupPad(self, groupID, padName, text):
return self.call("createGroupPad", {
"groupID": groupID,
"padName": padName,
"text": text
- })
+ })
# AUTHORS
# Theses authors are bind to the attributes the users choose (color and name).
# creates a new author
- def createAuthor(self,name):
+ def createAuthor(self, name):
return self.call("createAuthor", {
"name": name
- })
+ })
# this functions helps you to map your application author ids to etherpad lite author ids
- def createAuthorIfNotExistsFor(self,authorMapper, name):
+ def createAuthorIfNotExistsFor(self, authorMapper, name):
return self.call("createAuthorIfNotExistsFor", {
"authorMapper": authorMapper,
"name": name
- })
+ })
# SESSIONS
@@ -146,59 +133,59 @@ def createAuthorIfNotExistsFor(self,authorMapper, name):
# a cookie to the client and is valid until a certian date.
# creates a new session
- def createSession(self,groupID, authorID, validUntil):
+ def createSession(self, groupID, authorID, validUntil):
return self.call("createSession", {
"groupID": groupID,
"authorID": authorID,
"validUntil": validUntil
- })
+ })
# deletes a session
- def deleteSession(self,sessionID):
+ def deleteSession(self, sessionID):
return self.call("deleteSession", {
"sessionID": sessionID
- })
+ })
# returns informations about a session
- def getSessionInfo(self,sessionID):
+ def getSessionInfo(self, sessionID):
return self.call("getSessionInfo", {
"sessionID": sessionID
- })
+ })
# returns all sessions of a group
- def listSessionsOfGroup(self,groupID):
+ def listSessionsOfGroup(self, groupID):
return self.call("listSessionsOfGroup", {
"groupID": groupID
- })
+ })
# returns all sessions of an author
- def listSessionsOfAuthor(self,authorID):
+ def listSessionsOfAuthor(self, authorID):
return self.call("listSessionsOfAuthor", {
"authorID": authorID
- })
+ })
# PAD CONTENT
# Pad content can be updated and retrieved through the API
# returns the text of a pad
# should take optional rev
- def getText(self,padID):
+ def getText(self, padID):
return self.call("getText", {
"padID": padID
- })
+ })
# sets the text of a pad
- def setText(self,padID, text):
+ def setText(self, padID, text):
return self.call("setText", {
"padID": padID,
"text": text
- })
+ })
# PAD
@@ -207,63 +194,59 @@ def setText(self,padID, text):
# forbidden for normal pads to include a in the name.
# creates a new pad
- def createPad(self,padID, text):
+ def createPad(self, padID, text):
return self.call("createPad", {
"padID": padID,
"text": text
- })
+ })
# returns the number of revisions of this pad
- def getRevisionsCount(self,padID):
+ def getRevisionsCount(self, padID):
return self.call("getRevisionsCount", {
"padID": padID
- })
+ })
# deletes a pad
- def deletePad(self,padID):
+ def deletePad(self, padID):
return self.call("deletePad", {
"padID": padID
- })
+ })
# returns the read only link of a pad
- def getReadOnlyID(self,padID):
+ def getReadOnlyID(self, padID):
return self.call("getReadOnlyID", {
"padID": padID
- })
+ })
# sets a boolean for the public status of a pad
- def setPublicStatus(self,padID, publicStatus):
+ def setPublicStatus(self, padID, publicStatus):
return self.call("setPublicStatus", {
"padID": padID,
"publicStatus": publicStatus
- })
+ })
# return true of false
- def getPublicStatus(self,padID):
+ def getPublicStatus(self, padID):
return self.call("getPublicStatus", {
"padID": padID
- })
+ })
# returns ok or a error message
- def setPassword(self,padID, password):
+ def setPassword(self, padID, password):
return self.call("setPassword", {
"padID": padID,
"password": password
- })
+ })
# returns true or false
- def isPasswordProtected(self,padID):
+ def isPasswordProtected(self, padID):
return self.call("isPasswordProtected", {
"padID": padID
- })
-
-
-
-
+ })

0 comments on commit a84da3f

Please sign in to comment.