Beispiel #1
0
def update_lti_configurations():
    canvas = Canvas(
        base_url=app.config['CANVAS_API_URL'],
        access_token=app.config['CANVAS_ACCESS_TOKEN'],
    )
    successes = []
    errors = []
    for tool_name, tool_id in app.config.get('CANVAS_LTI_EXTERNAL_TOOL_IDS',
                                             {}).items():
        xml_string = resolve_xml_template(f'{tool_name}.xml')
        external_tool = ExternalTool(
            canvas._Canvas__requester,
            {
                'account_id': app.config['CANVAS_BERKELEY_ACCOUNT_ID'],
                'id': tool_id,
            },
        )
        response = None
        try:
            response = external_tool.edit(
                config_type='by_xml',
                config_xml=xml_string,
                consumer_key=app.config['CANVAS_LTI_KEY'],
                shared_secret=app.config['CANVAS_LTI_SECRET'],
            )
        except Exception as e:
            app.logger.error(
                f'Failed to update external tool {tool_name} due to error: {str(e)}'
            )
            app.logger.exception(e)
        if response and response.name:
            successes.append(response.name)
        else:
            errors.append(tool_name)
    return successes, errors
Beispiel #2
0
    def create_external_tool(self, name, privacy_level, consumer_key,
                             shared_secret, **kwargs):
        """
        Create an external tool in the current account.

        :calls: `POST /api/v1/accounts/:account_id/external_tools \
        <https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.create>`_

        :param name: The name of the tool
        :type name: str
        :param privacy_level: What information to send to the external
            tool. Options are "anonymous", "name_only", "public"
        :type privacy_level: str
        :param consumer_key: The consumer key for the external tool
        :type consumer_key: str
        :param shared_secret: The shared secret with the external tool
        :type shared_secret: str
        :rtype: :class:`canvasapi.external_tool.ExternalTool`
        """
        from canvasapi.external_tool import ExternalTool

        response = self._requester.request('POST',
                                           'accounts/{}/external_tools'.format(
                                               self.id),
                                           _kwargs=combine_kwargs(**kwargs))
        response_json = response.json()
        response_json.update({'account_id': self.id})

        return ExternalTool(self._requester, response_json)
Beispiel #3
0
    def get_external_tool(self, tool_id):
        """
        :calls: `GET /api/v1/accounts/:account_id/external_tools/:external_tool_id \
        <https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.show>`_

        :rtype: :class:`canvasapi.external_tool.ExternalTool`
        """
        from canvasapi.external_tool import ExternalTool

        response = self._requester.request(
            'GET',
            'accounts/{}/external_tools/{}'.format(self.id, tool_id),
        )
        tool_json = response.json()
        tool_json.update({'account_id': self.id})

        return ExternalTool(self._requester, tool_json)
Beispiel #4
0
    def __init__(self, tool: ExternalTool) -> None:
        super().__init__()
        self.session = requests.Session()

        url = tool.get_sessionless_launch_url()
        resp = self.session.get(url)
        form_entries = re.findall(
            r'<input type="hidden" name="(.*?)" id=".*" value="(.*?)" />',
            resp.text)
        if form_entries is None:
            raise Exception("Unable to locate form keys")
        resp = self.session.post(
            tool.url, {k: html.unescape(v)
                       for (k, v) in form_entries})

        self.base_url = resp.url.split('?', 1)[0]
        self.body = resp.text
Beispiel #5
0
    def get_external_tool(self, tool):
        """
        :calls: `GET /api/v1/accounts/:account_id/external_tools/:external_tool_id \
        <https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.show>`_

        :param tool: The object or ID of the tool
        :type tool: :class:`canvasapi.external_tool.ExternalTool` or int

        :rtype: :class:`canvasapi.external_tool.ExternalTool`
        """
        from canvasapi.external_tool import ExternalTool

        tool_id = obj_or_id(tool, "tool", (ExternalTool,))

        response = self._requester.request(
            'GET',
            'accounts/{}/external_tools/{}'.format(self.id, tool_id),
        )
        tool_json = response.json()
        tool_json.update({'account_id': self.id})

        return ExternalTool(self._requester, tool_json)
 def test_parent_type_no_id(self, m):
     tool = ExternalTool(self.canvas._Canvas__requester, {'id': 1})
     with self.assertRaises(ValueError):
         tool.parent_type