forked from tomassedovic/etherpad-lite-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.coffee
113 lines (97 loc) · 2.75 KB
/
main.coffee
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
http = require 'http'
https = require 'https'
url = require 'url'
querystring = require 'querystring'
_ = require 'underscore'
retriever = null
exports.connect = (options={}) ->
unless 'apikey' of options
throw new Error('You must specify etherpad-lite apikey')
api = {}
api.options = _.extend {}, options
api.options.host ||= 'localhost'
api.options.port ||= 9001
retriever = http
if api.options.port is 443 or api.options.ssl
retriever = https
api.call = (functionName, functionArgs, callback) ->
rootPath = api.options.rootPath or '/api/1.2.12/'
apiOptions = _.extend { 'apikey': @options.apikey }, functionArgs
httpOptions = _.extend @options, {path: rootPath + functionName + '?' + querystring.stringify apiOptions}
chunks = []
req = retriever.get httpOptions, (res) ->
res.on 'data', (data) ->
chunks.push(data)
res.on 'end', () ->
try
response = JSON.parse chunks.join('')
catch error
callback { code: -1, message: 'cannot parse the API response' }, null
return
if response.code is 0 and response.message is 'ok'
callback null, response.data
else
callback { code: response.code, message: response.message}, null
req.on 'error', (error) ->
callback { code: -1, message: (error.message or error) }, null
# https://raw.githubusercontent.com/ether/etherpad-lite/develop/src/node/handler/APIHandler.js
apiFunctions = [
'createGroup'
'createGroupIfNotExistsFor'
'deleteGroup'
'listPads'
'listAllPads'
'createDiffHTML'
'createPad'
'createGroupPad'
'createAuthor'
'createAuthorIfNotExistsFor'
'listPadsOfAuthor'
'createSession'
'deleteSession'
'getSessionInfo'
'listSessionsOfGroup'
'listSessionsOfAuthor'
'getText'
'setText'
'getHTML'
'setHTML'
'getAttributePool'
'getRevisionsCount'
'getSavedRevisionsCount'
'listSavedRevisions'
'saveRevision'
'getRevisionChangeset'
'getLastEdited'
'deletePad'
'copyPad'
'movePad'
'getReadOnlyID'
'getPadID'
'setPublicStatus'
'getPublicStatus'
'setPassword'
'isPasswordProtected'
'listAuthorsOfPad'
'padUsersCount'
'getAuthorName'
'padUsers'
'sendClientsMessage'
'listAllGroups'
'checkToken'
'appendChatMessage'
'getChatHistory'
'getChatHistory'
'getChatHead'
'restoreRevision'
]
for functionName in apiFunctions
do (functionName) ->
api[functionName] = (args, callback) ->
if arguments.length is 1 and _.isFunction(args)
callback = args
args = {}
callback = (->) unless callback?
api.call(functionName, args, callback)
return null
return api