Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Return objects as JSON strings #588

Open
lsloan opened this issue Feb 17, 2023 · 1 comment
Open

Feature request: Return objects as JSON strings #588

lsloan opened this issue Feb 17, 2023 · 1 comment

Comments

@lsloan
Copy link

lsloan commented Feb 17, 2023

When debugging programs that use data from Canvas, it can be extremely valuable to see that data in a handy human-readable format. For example, JSON. This module converts the JSON returned by the Canvas API into objects, but it would be helpful for those objects to return their original JSON strings upon request. Something like a to_json() method in the classes would help.

@bennettscience
Copy link
Contributor

Each request in the API returns an object which includes the Requester class so it can make it's own network requests directly, which is why serializing to JSON can be tricky. That said, each class has the CanvasObject parent class which is just a Python object so you can use the dunder methods to get pretty representations of the data. For example:

# set up your Canvas object, etc.
course = canvas.get_course(12345)

print(course)
# Course(_requester=<canvasapi.requester.Requester object at 0x106a94d30>, id=12345, name=Some Course, account_id=1, uuid=7W3gvPEhBHI2KbJ02u39kb7PLasfdasd234213096, start_at=None...)

# prints a nice representation of the course in the terminal
print(course.__dict__)
# {
# '_requester': <canvasapi.requester.Requester at 0x106a94d30>,
# 'id': 58866,
 # 'name': 'Certified Required Training Modules 22-23',
#  'account_id': 1,
 # 'uuid': '7W3gvPEhBHI2KbJ02u39kb7PLsBcl6vySYvxZaD6',
 # 'start_at': None,
# ...
# }

For any keys in the class that are also objects, you can print those using the same method:

# get the requester params specifically on an object
print(course._requester.__dict__)
# { 
#   'original_url': 'https://myurl.instructure.com', 
#   'base_url': 'https://myurl.instructure.com/api/v1/',
#   ...
# }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants