d2l_sdk 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,57 @@
1
+ require_relative 'requests'
2
+
3
+ ########################
4
+ # LOGGING:##############
5
+ ########################
6
+
7
+ # retrieve all current log messages
8
+ # with MANY parameters possible for filtering.
9
+ # REQUIRED PARAMS: date_range_start; date_range_end
10
+ # logLevel is CSV formatted, so simple delimit each value with a comma
11
+ def get_all_logs(date_range_start, date_range_end, search = '', log_level = '',
12
+ logger_assembly = '', user_id = 0, message_group_id = 0,
13
+ include_traces = nil, org_unit_id = 0, bookmark = '')
14
+ path = "/d2l/api/lp/#{$lp_ver}/logging/"
15
+ path += "?dateRangeStart=#{date_range_start}"
16
+ path += "&dateRangeEnd=#{date_range_end}"
17
+ path += "&search=#{search}" if search != ''
18
+ path += "&logLevel=#{log_level}" if log_level != ''
19
+ path += "&loggerAssembly=#{logger_assembly}" if logger_assembly != ''
20
+ path += "&userId=#{user_id}" if user_id != 0
21
+ path += "&messageGroupId=#{message_group_id}" if message_group_id != 0
22
+ path += "&includeTraces=#{include_traces}" if include_traces != nil
23
+ path += "&orgUnitId=#{org_unit_id}" if org_unit_id != 0
24
+ path += "&bookmark=#{bookmark}" if bookmark != ''
25
+ ap path
26
+ _get(path)
27
+ # returns paged result set of Message data blocks
28
+ end
29
+
30
+ # retrieve all current log arranged in message groups
31
+ # REQUIRED PARAMS: date_range_start; date_range_end
32
+ # logLevel is CSV formatted, so simple delimit each value with a comma
33
+ def get_all_message_group_logs(date_range_start, date_range_end, search = '',
34
+ log_level = '', logger_assembly = '', user_id = 0,
35
+ message_group_id = 0, org_unit_id = 0,
36
+ bookmark = '')
37
+ path = "/d2l/api/lp/#{$lp_ver}/logging/grouped/"
38
+ path += "?dateRangeStart=#{date_range_start}"
39
+ path += "&dateRangeEnd=#{date_range_end}"
40
+ path += "&search=#{search}" if search != ''
41
+ path += "&logLevel=#{log_level}" if log_level != ''
42
+ path += "&loggerAssembly=#{logger_assembly}" if logger_assembly != ''
43
+ path += "&userId=#{user_id}" if user_id != 0
44
+ path += "&messageGroupId=#{message_group_id}" if message_group_id != 0
45
+ path += "&orgUnitId=#{org_unit_id}" if org_unit_id != 0
46
+ path += "&bookmark=#{bookmark}" if bookmark != ''
47
+ _get(path)
48
+ # returns paged result set of MessageGroupSummary data blocks
49
+ end
50
+
51
+ # retrieve identified log message
52
+ def get_log_message(log_message_id, include_traces = nil)
53
+ path = "/d2l/api/lp/#{$lp_ver}/logging/#{log_message_id}/"
54
+ path += "?includeTraces=#{include_traces}" if include_traces != nil
55
+ _get(path)
56
+ # returns Message JSON block
57
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'requests'
2
+ require 'json-schema'
3
+ #################
4
+ # News:##########
5
+ #################
6
+
7
+ # if since not specified, only includes most 'recent' feed items
8
+ # if since specified but until is not, all items since 'since' are fetched
9
+ # if since and until are specified, all items between these two dates are fetched
10
+ # if since > until, an empty feed list is returned
11
+ # purpose: fetch the feed for the current user context
12
+ def get_current_user_feed(since = "", _until = "")
13
+ path = "/d2l/api/lp/#{$lp_ver}/feed/"
14
+ # if since is specified, then until can be. Until is not required though.
15
+ if since != ""
16
+ path += "?since=#{since}"
17
+ path += "&until=#{_until}" if _until != ""
18
+ end
19
+ _get(path)
20
+ end
@@ -0,0 +1,383 @@
1
+ require_relative 'requests'
2
+ require 'json-schema'
3
+ ########################
4
+ # Org Units:############
5
+ ########################
6
+
7
+
8
+ # gets all descendents of a particular org unit, as referenced by the
9
+ # "org_unit_id" argument. A get request is then performed by a preformatted
10
+ # path.
11
+ def get_org_unit_descendants(org_unit_id, ou_type_id = 0)
12
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{org_unit_id}/descendants/"
13
+ path += "?ouTypeId=#{ou_type_id}" if ou_type_id != 0
14
+ _get(path)
15
+ # return JSON array of OrgUnit data blocks
16
+ end
17
+
18
+ # gets a paged result of the org unit's descendants. The descendants are
19
+ # first referenced by a preformatted path; then if there is a defined bookmark,
20
+ # the bookmark parameter is appended to the path.
21
+ #
22
+ # return: JSON array of org unit descendants (paged)
23
+ def get_paged_org_unit_descendants(org_unit_id, ou_type_id = 0, bookmark = '')
24
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{org_unit_id}/descendants/paged/"
25
+ path += "?ouTypeId=#{ou_type_id}" if ou_type_id != 0
26
+ path += "?bookmark=#{bookmark}" if bookmark != ''
27
+ _get(path)
28
+ # return paged json of org_unit descendants
29
+ end
30
+
31
+ # gets all parents of a particular org unit, as referenced by the
32
+ # "org_unit_id" argument. A get request is then performed by a preformatted
33
+ # path.
34
+ def get_org_unit_parents(org_unit_id, ou_type_id = 0)
35
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{org_unit_id}/parents/"
36
+ path += "?ouTypeId=#{ou_type_id}" if ou_type_id != 0
37
+ _get(path)
38
+ # return json of org_unit parents
39
+ end
40
+
41
+ # performs a post method to assign a parent to a particular child org unit.
42
+ # This is done by first referencing all the parents of the +child_ou+ and then
43
+ # POSTing the id of another org unit that is to be added to the parents.
44
+ def add_parent_to_org_unit(parent_ou_id, child_ou_id)
45
+ # Must follow structure of data
46
+ # (course <-- semester <== org -->custom dept--> dept -->templates--> courses)
47
+ # Refer to valence documentation for further structural understanding..
48
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{child_ou_id}/parents/"
49
+ _post(path, parent_ou_id)
50
+ end
51
+
52
+ # Gets all org unit ancestors. Simply, this method references all of the
53
+ # ancestors of the particular org unit and then returns them in a JSON array.
54
+ #
55
+ # return: JSON array of org_unit ancestors.
56
+ def get_org_unit_ancestors(org_unit_id, ou_type_id = 0)
57
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{org_unit_id}/ancestors/"
58
+ path += "?ouTypeId=#{ou_type_id}" if ou_type_id != 0
59
+ _get(path)
60
+ # return json of org_unit ancestors
61
+ end
62
+
63
+ # gets all children of a particular org unit, as referenced by the
64
+ # "org_unit_id" argument. A get request is then performed by a preformatted
65
+ # path.
66
+ def get_org_unit_children(org_unit_id, ou_type_id = 0)
67
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{org_unit_id}/children/"
68
+ path += "?ouTypeId=#{ou_type_id}" if ou_type_id != 0
69
+ _get(path)
70
+ # return json of org_unit children
71
+ end
72
+
73
+ # Gets all children of the org unit, but in a paged result. These are first
74
+ # referenced via the org_unit_id argument, and then a bookmark is appended
75
+ # if there is one specified. This is then returned as a json array.
76
+ #
77
+ # return: JSON array of org unit children.
78
+ def get_paged_org_unit_children(org_unit_id, bookmark = '')
79
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{org_unit_id}/children/paged/"
80
+ path += "?bookmark=#{bookmark}" if bookmark != ''
81
+ _get(path)
82
+ # return json of org_unit children
83
+ end
84
+
85
+ # gets all properties of a particular org unit, as referenced by the
86
+ # "org_unit_id" argument. A get request is then performed by a preformatted
87
+ # path.
88
+ def get_org_unit_properties(org_unit_id)
89
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{org_unit_id}"
90
+ _get(path)
91
+ # return json of org_unit properties
92
+ end
93
+
94
+ # This deletes the relationship between a parent ou and a child ou by
95
+ # performing a delete method from the parent's children and specifying this
96
+ # child through its id.
97
+ def delete_relationship_of_child_with_parent(parent_ou_id, child_ou_id)
98
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{parent_ou_id}/children/#{child_ou_id}"
99
+ _delete(path)
100
+ end
101
+
102
+ # This deletes the relationship between a child ou and a parent ou by
103
+ # performing a delete method from the child's parents and specifying this
104
+ # parent through its id.
105
+ def delete_relationship_of_parent_with_child(parent_ou_id, child_ou_id)
106
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{child_ou_id}/parents/#{parent_ou_id}"
107
+ _delete(path)
108
+ end
109
+
110
+ # This retrieves a paged result of all the childless org units within the
111
+ # organization. As this is paged, it only retrieves the first 100 from the
112
+ # beginning of the request. If bookmark is not specified, then it only retrieves
113
+ # the first 100 results.
114
+ #
115
+ # return: JSON array of childless org units.
116
+ def get_all_childless_org_units(org_unit_type = '', org_unit_code = '', org_unit_name = '',
117
+ bookmark = '')
118
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/childless/"
119
+ path += "?orgUnitType=#{org_unit_type}" if org_unit_type != ''
120
+ path += "?orgUnitCode=#{org_unit_code}" if org_unit_code != ''
121
+ path += "?orgUnitName=#{org_unit_name}" if org_unit_name != ''
122
+ path += "?bookmark=#{bookmark}" if bookmark != ''
123
+ _get(path)
124
+ # ONLY RETRIEVES FIRST 100
125
+ end
126
+
127
+ def get_properties_of_all_org_units(org_unit_type = '', org_unit_code = '', org_unit_name = '',
128
+ bookmark = '')
129
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/"
130
+ path += "?orgUnitType=#{org_unit_type}" if org_unit_type != ''
131
+ path += "?orgUnitCode=#{org_unit_code}" if org_unit_code != ''
132
+ path += "?orgUnitName=#{org_unit_name}" if org_unit_name != ''
133
+ path += "?bookmark=#{bookmark}" if bookmark != ''
134
+ _get(path)
135
+ # ONLY RETRIEVES FIRST 100 after bookmark
136
+ # returns: paged result of OrgUnitProperties blocks
137
+ end
138
+
139
+ # Retrieves a paged result of all orphaned org units within the organization.
140
+ # This is a paged result, so only for the first 100 from the beginning bookmark
141
+ # are retrieved. Simply put, if the bookmark is not defined, it only gets the
142
+ # first 100 orphans.
143
+ #
144
+ # return: JSON array of orphaned org units.
145
+ def get_all_orphans(org_unit_type = '', org_unit_code = '', org_unit_name = '',
146
+ bookmark = '')
147
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/orphans/"
148
+ path += "?orgUnitType=#{org_unit_type}" if org_unit_type != ''
149
+ path += "?orgUnitCode=#{org_unit_code}" if org_unit_code != ''
150
+ path += "?orgUnitName=#{org_unit_name}" if org_unit_name != ''
151
+ path += "?bookmark=#{bookmark}" if bookmark != ''
152
+ _get(path)
153
+ end
154
+
155
+ # Adds a child to the org unit by using org_unit_id to reference the soon-to-be
156
+ # parent of the child_org_unit and referencing the soon-to-be child through the
157
+ # child_org_unit_id argument. Then, a path is created to reference the children
158
+ # of the soon-to-be parent and executing a post http method that adds the child.
159
+ #
160
+ # TL;DR, this adds a child org_unit to the children of an org_unit.
161
+ def add_child_org_unit(org_unit_id, child_org_unit_id)
162
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{org_unit_id}/children/"
163
+ _post(path, child_org_unit_id)
164
+ end
165
+
166
+ # Retrieves a paged result of all recycled org units. Thus, only the first 100
167
+ # are retrieved since the first referenced org unit. As such, if the bookmark is
168
+ # not defined, then it only retrieves the first 100.
169
+ #
170
+ # return: JSON array of recycled org units.
171
+ def get_recycled_org_units(bookmark = '')
172
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/recyclebin/"
173
+ path += "?bookmark=#{bookmark}" if bookmark != ''
174
+ _get(path)
175
+ # GETS ONLY FIRST 100
176
+ end
177
+
178
+ # An org unit is recycled by executing a POST http method and recycling it. The
179
+ # path for the recycling is created using the org_unit_id argument and then the
180
+ # post method is executed afterwards.
181
+ def recycle_org_unit(org_unit_id)
182
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/recyclebin/#{org_unit_id}/recycle"
183
+ _post(path, {})
184
+ end
185
+
186
+ # deletes a particular org unit. This is done via referencing the org unit by
187
+ # its id and performing a delete method.
188
+ def delete_recycled_org_unit(org_unit_id)
189
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/recyclebin/#{org_unit_id}"
190
+ _delete(path)
191
+ end
192
+
193
+ # Restores a recycled org unit. This is done by referencing the org unit by its
194
+ # id in the recycling bin and then appending '/restore'. This is then used in a
195
+ # post method that performs the restoring process.
196
+ def restore_recycled_org_unit(org_unit_id)
197
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/recyclebin/#{org_unit_id}/restore"
198
+ _post(path, {})
199
+ end
200
+
201
+ # Checks whether the created org unit data conforms to the valence api for the
202
+ # org unit data JSON object. If it does conform, then nothing happens and it
203
+ # simply returns true. If it does not conform, then the JSON validator raises
204
+ # an exception.
205
+ def check_org_unit_data_validity(org_unit_data)
206
+ schema = {
207
+ 'type' => 'object',
208
+ 'required' => %w(Type Name Code Parents),
209
+ 'properties' => {
210
+ 'Type' => { 'type' => 'integer' },
211
+ 'Name' => { 'type' => 'string' },
212
+ 'Code' => { 'type' => 'string' },
213
+ 'Parents' => { 'type' => 'array',
214
+ 'items' => { 'type' => 'integer', 'minItems' => 1 }
215
+ }
216
+ }
217
+ }
218
+ JSON::Validator.validate!(schema, org_unit_data, validate_schema: true)
219
+ end
220
+
221
+ # Functions considered for basic added functionality to api, not sure if needed.
222
+ def create_custom_org_unit(org_unit_data)
223
+ # Requires the type to have the correct parent. This will work fine in this
224
+ # sample, as the department (101) can have the parent Organiation (6606)
225
+ payload = { 'Type' => 101, # Number:D2LID
226
+ 'Name' => 'custom_ou_name', # String
227
+ 'Code' => 'custom_ou_code', # String
228
+ 'Parents' => [6606], # Number:D2LID
229
+ }.merge!(org_unit_data)
230
+ check_org_unit_data_validity(payload)
231
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/"
232
+ # Requires: OrgUnitCreateData JSON block
233
+ _post(path, payload)
234
+ # returns: OrgUnit JSON data block
235
+ end
236
+
237
+ # Checks whether the updated org unit data conforms to the valence api for the
238
+ # org unit data JSON object. If it does conform, then nothing happens and it
239
+ # simply returns true. If it does not conform, then the JSON validator raises
240
+ # an exception.
241
+ def check_org_unit_updated_data_validity(org_unit_data)
242
+ schema = {
243
+ 'type' => 'object',
244
+ 'required' => %w(Identifier Name Code Path Type),
245
+ 'properties' => {
246
+ 'Identifier' => { 'type' => 'string' },
247
+ 'Name' => { 'type' => 'string' },
248
+ 'Code' => { 'type' => 'string' },
249
+ 'Path' => { 'type' => 'string' },
250
+ 'Type' => {
251
+ 'required' => %w(Id Code Name),
252
+ 'properties' => {
253
+ 'Id' => { 'type' => 'integer' },
254
+ 'Code' => { 'type' => 'string' },
255
+ 'Name' => { 'type' => 'string' }
256
+ }
257
+ }
258
+ }
259
+ }
260
+ JSON::Validator.validate!(schema, org_unit_data, validate_schema: true)
261
+ end
262
+
263
+ def update_org_unit(org_unit_id, org_unit_data)
264
+ previous_data = get_org_unit_properties(org_unit_id)
265
+ payload = { # Can only update NAME, CODE, and PATH variables
266
+ 'Identifier' => org_unit_id.to_s, # String: D2LID // DO NOT CHANGE
267
+ 'Name' => previous_data['Name'], # String
268
+ # String #YearNUM where NUM{sp:01,su:06,fl:08} | nil
269
+ 'Code' => previous_data['Code'],
270
+ # String: /content/enforced/IDENTIFIER-CODE/
271
+ 'Path' => "/content/enforced/#{org_unit_id}-#{previous_data['Code']}/",
272
+ 'Type' => previous_data['Type']
273
+ # example:
274
+ # { # DO NOT CHANGE THESE
275
+ # 'Id' => 5, # <number:D2LID>
276
+ # 'Code' => 'Semester', # <string>
277
+ # 'Name' => 'Semester', # <string>
278
+ # }
279
+ }.merge!(org_unit_data)
280
+ check_org_unit_updated_data_validity(payload)
281
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/#{org_unit_id}"
282
+ puts '[-] Attempting put request (updating orgunit)...'
283
+ # requires: OrgUnitProperties JSON block
284
+ _put(path, payload)
285
+ puts '[+] Semester update completed successfully'.green
286
+ # returns: OrgUnitProperties JSON data block
287
+ end
288
+
289
+ # Retrieves the organization info. Only gets a small amount of information,
290
+ # but may be useful in some instances.
291
+ def get_organization_info
292
+ path = "/d2l/api/lp/#{$lp_ver}/organization/info"
293
+ _get(path)
294
+ # return: Organization JSON block
295
+ end
296
+
297
+ # Retrieves the org units that are a particular id. This is done by obtaining
298
+ # all of the children of the organization and then filtering by this id.
299
+ #
300
+ # return: JSON array of all org units of an outype.
301
+ def get_all_org_units_by_type_id(outype_id)
302
+ path = "/d2l/api/lp/#{$lp_ver}/orgstructure/6606/children/?ouTypeId=#{outype_id}"
303
+ _get(path)
304
+ end
305
+
306
+ def check_create_org_unit_type_data_validity(org_unit_type_data)
307
+ schema = {
308
+ 'type' => 'object',
309
+ 'required' => %w(Code Name Description SortOrder),
310
+ 'properties' => {
311
+ 'Code' => { 'type' => 'string' },
312
+ 'Name' => { 'type' => 'string' },
313
+ 'Description' => { 'type' => 'string' },
314
+ 'SortOrder' => { 'type' => 'integer'}
315
+ }
316
+ }
317
+ JSON::Validator.validate!(schema, org_unit_type_data, validate_schema: true)
318
+ end
319
+
320
+ def create_custom_outype(create_org_unit_type_data)
321
+ payload =
322
+ {
323
+ 'Code' => '',
324
+ 'Name' => '',
325
+ 'Description' => '',
326
+ 'SortOrder' => 0
327
+ }.merge!(create_org_unit_type_data)
328
+ #validate schema
329
+ check_create_org_unit_type_data_validity(payload)
330
+ path = "/d2l/api/lp/#{$lp_ver}/outypes/"
331
+ _post(path, payload)
332
+ # returns OrgUnitType JSON data block
333
+ end
334
+
335
+ # This retrieves information about a partituclar org unit type, referenced via
336
+ # the outype_id argument. This is then returned as a JSON object.
337
+ def get_outype(outype_id)
338
+ path = "/d2l/api/lp/#{$lp_ver}/outypes/#{outype_id}"
339
+ _get(path)
340
+ end
341
+
342
+ # retrieves all outypes that are known and visible. This is returned as a JSON
343
+ # array of orgunittype data blocks.
344
+ def get_all_outypes
345
+ path = "/d2l/api/lp/#{$lp_ver}/outypes/"
346
+ _get(path)
347
+ end
348
+
349
+ # update a particular org unit type (with POST for some reason)
350
+ def update_outype(outype_id, create_org_unit_type_data)
351
+ payload =
352
+ {
353
+ 'Code' => '',
354
+ 'Name' => '',
355
+ 'Description' => '',
356
+ 'SortOrder' => 0
357
+ }.merge!(create_org_unit_type_data)
358
+ #validate schema
359
+ check_create_org_unit_type_data_validity(payload)
360
+ path = "/d2l/api/lp/#{$lp_ver}/outypes/#{outype_id}"
361
+ _post(path, payload)
362
+ # returns OrgUnitType JSON data block
363
+ end
364
+
365
+ # Delete a particular org unit type
366
+ def delete_outype(outype_id)
367
+ path = "/d2l/api/lp/#{$lp_ver}/outypes/#{outype_id}"
368
+ _delete(path)
369
+ end
370
+
371
+ # retrieve org unit type of department org units
372
+ def get_department_outype
373
+ path = "/d2l/api/lp/#{$lp_ver}/outypes/department"
374
+ _get(path)
375
+ # returns OrgUnitType JSON data block
376
+ end
377
+
378
+ # retrieve org unit type of semester org units
379
+ def get_semester_outype
380
+ path = "/d2l/api/lp/#{$lp_ver}/outypes/semester"
381
+ _get(path)
382
+ # returns OrgUnitType JSON data block
383
+ end