-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriseApi.gs
222 lines (202 loc) · 7.89 KB
/
riseApi.gs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
function RiseService(username,password){
let authorizeQuery = {
nonce: Math.floor(Math.random()*1000000000),//1.000.000.000,
client_id: "0oaimtuxt2kGE1lgv356", //presumably Articulate's Okta client id
redirect_uri: "https://rise.articulate.com/auth-callback",
response_mode: "fragment",
response_type: "id_token token",
scope: "openid staff profile_name",
state: Utilities.getUuid(),
}
const tokenData = getBearerToken(authorizeQuery)
const bearerToken = tokenData["access_token"]
function getBearerToken(authorizeQuery) {
const newAuthQuery = authCall(authorizeQuery)
const oktaResponse = oktaCall(newAuthQuery)
const tokenResponse = oktaResponse.getAllHeaders()['Location']
const hashQuery = tokenResponse.split("#")[1]
const params = hashQuery.split("&").reduce((prev,current)=>{
const [key,value] = current.split("=")
return { ...prev, [key]: value}
},{})
return params
}
function oktaCall(authorizeQuery){
const url = `https://id.articulate.com/okta-authorize?client_id=${authorizeQuery.client_id}&nonce=${authorizeQuery.nonce}&redirect_uri=${authorizeQuery.redirect_uri}&response_mode=${authorizeQuery.response_mode}&response_type=${authorizeQuery.response_type}&scope=${authorizeQuery.scope}&state=${authorizeQuery.state}&sessionToken=${authorizeQuery.sessionToken}`
const options = {
method: "GET",
muteHttpExceptions:true,
followRedirects:false,
contentType:'application/json;charset=UTF-8',
headers:{
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36',
accept:"application/json, text/plain, */*",
}
// Convert the JavaScript object to a JSON string.
};
return UrlFetchApp.fetch(url, options);
}
function authCall(authorizeQuery){
const queryString = `client_id=${authorizeQuery.client_id}&nonce=${authorizeQuery.nonce}&redirect_uri=${encodeURI(authorizeQuery.redirect_uri)}&response_mode=${authorizeQuery.response_mode}&response_type=${encodeURI(authorizeQuery.response_type)}&scope=${encodeURI(authorizeQuery.scope)}&state=${authorizeQuery.state}`
const authenticationInitiation = UrlFetchApp.fetch(`https://id.articulate.com/oauth2/default/v1/authorize?${queryString}`,{
method:"GET",
followRedirects:false
})
const url = 'https://id.articulate.com/api/v1/authn'
const data = {
"username":username,
"password":password
}
//const resp = call("POST",url,JSON.stringify(data))
const options = {
method: "POST",
muteHttpExceptions:true,
followRedirects:false,
payload: JSON.stringify(data),
contentType:'application/json;charset=UTF-8',
headers:{
accept:"application/json, text/plain, */*",
}
// Convert the JavaScript object to a JSON string.
};
return {...authorizeQuery,sessionToken:JSON.parse(UrlFetchApp.fetch(url, options).getContentText()).sessionToken};
}
function duplicateCourse(courseId,newCourseName){
const url = "https://rise.articulate.com/api/rise-runtime/ducks/rise/courses/DUPLICATE_COURSE"
const data = {
"type": "rise/courses/DUPLICATE_COURSE",
"payload": {
"courseId": courseId,
"title": newCourseName
}
}
const resp = call("POST",url,JSON.stringify(dupData))
const jsonResponse = JSON.parse(resp.getContentText())
const newCourseId = jsonResponse.payload.id
return newCourseId
}
function moveCourseToFolder(courseId,folderId){
const url = "https://rise.articulate.com/api/rise-runtime/ducks/rise/courses/MOVE_TO_FOLDER"
const data = {
"type": "rise/courses/MOVE_TO_FOLDER",
"payload": {
"courseId": courseId,
"tags": folderId
}
}
return call("POST",url,JSON.stringify(data))
}
function getCourses(){
const url = 'https://rise.articulate.com/api/rise-runtime/ducks/rise/data/FETCH_MANAGE_DATA'
//for some reason it wants to get a color passed
const data = {"type":"rise/data/FETCH_MANAGE_DATA","payload":"#1eb0ff"}
const resp = call("POST",url,JSON.stringify(data))
const courses = JSON.parse(resp.getContentText())
return courses.payload
}
function getCourseInformation(courseId){
const url = 'https://rise.articulate.com/api/rise-runtime/ducks/rise/courses/GET_COURSE'
const data = {"type":"rise/courses/GET_COURSE","payload":{"courseId":courseId}}
return JSON.parse(call("POST",url,JSON.stringify(data)).getContentText()).payload
}
function downloadXliff(courseId,courseName,language){
const url = `https://rise.articulate.com/api/rise-runtime/export_course_translation/${courseId}`
const resp = call("GET",url,{})
const blob = resp.getBlob()
const translated = TranslateXLIFF.translateRawFile(language,blob)
const filename = `translated-${language}-${courseName}.xlf`
const newBlob = Utilities.newBlob(translated.xml, 'application/xliff+xml', filename);
return newBlob
}
function deselectAuthor(courseId){
const url1 = "https://rise.articulate.com/api/rise-runtime/ducks/rise/courses/UPDATE_COURSE"
const data1 = {"type":"rise/courses/UPDATE_COURSE","payload":{"id":courseId,"selectedAuthorId":"none"}}
const url2 = "https://rise.articulate.com/api/rise-runtime/ducks/rise/lessons/UPDATE_COURSE_LESSONS"
const data2 = {"type":"rise/lessons/UPDATE_COURSE_LESSONS","payload":{"courseId":courseId,"selectedAuthorId":"none"}}
const resp1 = call("POST",url1,JSON.stringify(data1))
const resp2 = call("POST",url2,JSON.stringify(data2))
return {resp1,resp2}
}
function addCollaborator(courseId,email){
const url = "https://rise.articulate.com/api/rise-runtime/ducks/rise/collaborators/CREATE_COLLABORATOR"
const data = {
"type": "rise/collaborators/CREATE_COLLABORATOR",
"payload": {
"courseId": courseId,
"email": email
}
}
return call("POST",url,JSON.stringify(data))
}
function triggerTranslationImport(key,courseId){
const url = 'https://rise.articulate.com/api/rise-runtime/ducks/rise/courses/IMPORT_TRANSLATION'
const data = {
"type": "rise/courses/IMPORT_TRANSLATION",
"payload": {
"id": courseId,
"key": key
}
}
return call("POST",url,JSON.stringify(data))
}
function uploadToS3(blob,url){
const headers = {
'Access-Control-Request-Method':'PUT',
'Access-Control-Request-Headers':'content-type'
}
//s3Call("OPTIONS",url,{},headers)
return s3Call("PUT",url,blob.getDataAsString(),{})
}
function getYurl(courseId,filename){
const url = 'https://rise.articulate.com/api/rise-runtime/ducks/rise/uploads/GET_YURL'
const data = {
"type": "rise/uploads/GET_YURL",
"payload": {
"assetPath": "translations/",
"courseId": courseId,
"filename": filename
}
}
const resp = call("Post",url,JSON.stringify(data))
return JSON.parse(resp.getContentText())
}
function s3Call(method,url,payload,headers){
var options = {
method: method,
payload: payload,
contentType:'application/xliff+xml',
headers:headers
// Convert the JavaScript object to a JSON string.
};
var response = UrlFetchApp.fetch(url, options);
return response;
}
function call(method,url,payload){
var options = {
method: method,
payload: payload,
contentType:'application/json;charset=UTF-8',
headers:{
accept:"application/json, text/plain, */*",
'Authorization': bearerToken
}
// Convert the JavaScript object to a JSON string.
};
var response = UrlFetchApp.fetch(url, options);
return response;
}
return{
duplicateCourse,
getCourses,
getCourseInformation,
moveCourseToFolder,
downloadXliff,
deselectAuthor,
addCollaborator,
triggerTranslationImport,
uploadToS3,
getYurl,
s3Call,
call,
}
}