-
Notifications
You must be signed in to change notification settings - Fork 169
/
external_tool.py
124 lines (99 loc) · 4.42 KB
/
external_tool.py
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
from __future__ import absolute_import, division, print_function, unicode_literals
from six import python_2_unicode_compatible
from canvasapi.canvas_object import CanvasObject
from canvasapi.exceptions import CanvasException
from canvasapi.util import combine_kwargs
@python_2_unicode_compatible
class ExternalTool(CanvasObject):
def __str__(self):
return "{} ({})".format(self.name, self.id)
@property
def parent_id(self):
"""
Return the id of the course or account that spawned this tool.
:rtype: int
"""
if hasattr(self, 'course_id'):
return self.course_id
elif hasattr(self, 'account_id'):
return self.account_id
else:
raise ValueError("ExternalTool does not have a course_id or account_id")
@property
def parent_type(self):
"""
Return whether the tool was spawned from a course or account.
:rtype: str
"""
if hasattr(self, 'course_id'):
return 'course'
elif hasattr(self, 'account_id'):
return 'account'
else:
raise ValueError("ExternalTool does not have a course_id or account_id")
def get_parent(self):
"""
Return the object that spawned this tool.
:rtype: :class:`canvasapi.account.Account` or :class:`canvasapi.account.Course`
"""
from canvasapi.account import Account
from canvasapi.course import Course
response = self._requester.request(
'GET',
'{}s/{}'.format(self.parent_type, self.parent_id)
)
if self.parent_type == 'account':
return Account(self._requester, response.json())
elif self.parent_type == 'course':
return Course(self._requester, response.json())
def delete(self):
"""
Remove the specified external tool.
:calls: `DELETE /api/v1/courses/:course_id/external_tools/:external_tool_id
<https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.destroy>`_
or `DELETE /api/v1/accounts/:account_id/external_tools/:external_tool_id
<https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.destroy>`_
:rtype: :class:`canvasapi.external_tool.ExternalTool`
"""
response = self._requester.request(
'DELETE',
'{}s/{}/external_tools/{}'.format(self.parent_type, self.parent_id, self.id)
)
return ExternalTool(self._requester, response.json())
def edit(self, **kwargs):
"""
Update the specified external tool.
:calls: `PUT /api/v1/courses/:course_id/external_tools/:external_tool_id
<https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.update>`_
or `PUT /api/v1/accounts/:account_id/external_tools/:external_tool_id
<https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.update>`_
:rtype: :class:`canvasapi.external_tool.ExternalTool`
"""
response = self._requester.request(
'PUT',
'{}s/{}/external_tools/{}'.format(self.parent_type, self.parent_id, self.id),
_kwargs=combine_kwargs(**kwargs)
)
response_json = response.json()
if 'name' in response_json:
super(ExternalTool, self).set_attributes(response_json)
return ExternalTool(self._requester, response_json)
def get_sessionless_launch_url(self, **kwargs):
"""
Return a sessionless launch url for an external tool.
:calls: `GET /api/v1/courses/:course_id/external_tools/sessionless_launch
<https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.generate_sessionless_launch>`_
or `GET /api/v1/accounts/:account_id/external_tools/sessionless_launch \
<https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.generate_sessionless_launch>`_
:rtype: str
"""
kwargs['id'] = self.id
response = self._requester.request(
'GET',
'{}s/{}/external_tools/sessionless_launch'.format(self.parent_type, self.parent_id),
_kwargs=combine_kwargs(**kwargs)
)
try:
return response.json()['url']
except KeyError:
raise CanvasException('Canvas did not respond with a valid URL')