ucfopen / canvasapi

Python API wrapper for Instructure's Canvas LMS. Easily manage courses, users, gradebooks, and more.
https://pypi.python.org/pypi/canvasapi
MIT License
560 stars 174 forks source link

Add API coverage for LTI resource links #662

Open jonespm opened 2 months ago

jonespm commented 2 months ago

What resource needs additional coverage? Courses lti_resource links, which is a new API which is in beta and will be released soon.

What endpoints need to be covered? Add support for these https://canvas.instructure.com/doc/api/lti_resource_links.html

GET /api/v1/courses/:course_id/lti_resource_links
GET /api/v1/courses/:course_id/lti_resource_links/:id
PUT /api/v1/courses/:course_id/lti_resource_links/:id
jonespm commented 2 months ago

I tested this for the first GET call and looks to work

import json
import argparse
import os
from canvasapi import Canvas
from canvasapi.canvas_object import CanvasObject
from canvasapi.paginated_list import PaginatedList
from canvasapi.util import combine_kwargs
from canvasapi.course import Course

class LTIResourceLink(CanvasObject):
    def __init__(self, requester, attributes):
        # Initialize an LTIResourceLink object.
        super(LTIResourceLink, self).__init__(requester, attributes)

class ExtendedCourse(Course):
    def get_lti_resource_links(self, **kwargs):
        """
        Retrieve LTI resource links for this course as a PaginatedList.

        :return: A PaginatedList of LTI resource links.
        :rtype: :class:`canvasapi.paginated_list.PaginatedList`
        """
        endpoint = f"courses/{self.id}/lti_resource_links"

        return PaginatedList(
            LTIResourceLink,
            self._requester,
            "GET",
            endpoint,
            kwargs=combine_kwargs(**kwargs)
        )

API_URL = os.getenv('CANVAS_API_URL')
API_KEY = os.getenv('CANVAS_API_KEY')

if not API_URL or not API_KEY:
    print("Error: Please set the CANVAS_API_URL and CANVAS_API_KEY environment variables.")
    exit(1)

# Step 3: Initialize the Canvas API
canvas = Canvas(API_URL, API_KEY)

# Usage example, set the course_id you want
course_id = 123456
course = ExtendedCourse(canvas._Canvas__requester, {'id': course_id})

lti_resource_links = course.get_lti_resource_links()
for link in lti_resource_links:
    print(link)urce links: {response.status_code}")