canvasapi icon indicating copy to clipboard operation
canvasapi copied to clipboard

Course Audit Log (course.query_audit_log()) breaks, possibly due to API change

Open hanleybrand opened this issue 1 year ago • 0 comments

Describe the bug

The following code fails:

course_id = 666666 
course = canvas.get_course(course_id)
logged_events = course.query_audit_by_course()
event_list = [e for e in logged_events]

fails with AttributeError: 'str' object has no attribute 'update' because (I believe) the API has been changed to return a JSON object with three-keys, the basic structure is:

{
    "links":  {},
    "events": [],
    "linked": {}
}

with the second key containing what used to be returned by the API (a list of CourseEvent objects)

To Reproduce

Run the above four lines with canvasapi==3.3.0 after substituting a valid course_id

Expected behavior

[e for e in course.query_audit_by_course()] would produce a list of CourseEvent objects

Fix

I was able to fix the issue by adding _root='events', to the PaginatedList args in Course.query_audit_by_course()

    def query_audit_by_course(self, **kwargs):

        return PaginatedList(
            CourseEvent,
            self._requester,
            "GET",
            "audit/course/courses/{}".format(self.id),
            _root='events',
            _kwargs=combine_kwargs(**kwargs),
        )

Environment information

  • Python version 3.9
  • CanvasAPI version 3.30

hanleybrand avatar Sep 04 '24 20:09 hanleybrand