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

Refactor the canvas API factory to take parameters #6927

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

marcospri
Copy link
Member

@marcospri marcospri commented Dec 30, 2024

Take application_instance and user_id on the Canvas API factory
to allow creating a service for arbitrary users instead of just the one
of the current request.

The same modification is also made to the file service
as it is a dependency of the Canvas service.

Motivation

This will allow us to use the Canvas API to fetch sections rosters outside the request cycle, using any application instance we need.

Testing

  • Sanity check the existing factory with a canvas files assignment

https://hypothesis.instructure.com/courses/125/assignments/875

@marcospri marcospri changed the title Canvas refactor WIP Refactor the canvas API factory to take parameters Dec 30, 2024
@@ -106,7 +106,9 @@ def includeme(config): # noqa: PLR0915
config.register_service_factory(
"lms.services.grouping.service_factory", name="grouping"
)
config.register_service_factory("lms.services.file.factory", name="file")
config.register_service_factory(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new name on register_service_factory



def canvas_api_client_factory(_context, request):
def canvas_api_client_factory(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take both AI and user_id as new optional parameters in the factory.

"""
Get a CanvasAPIClient from a pyramid request.

:param request: Pyramid request object
:return: An instance of CanvasAPIClient
"""
application_instance = request.lti_user.application_instance
if application_instance and user_id:
oauth2_token_service = oauth2_token_service_factory(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that oauth2_token_service_factory already uses this pattern, takes AI and user_id.

file_service = file_service_factory(_context, request, application_instance)

else:
oauth2_token_service = request.find_service(name="oauth2_token")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, use the services from the factories without parameters.

return FileService(
application_instance=request.lti_user.application_instance, db=request.db
)
def file_service_factory(_context, request, application_instance=None):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give this a better name to be imported in other modules.

Take application_instance and user_id on the Canvas API factory
to allow creating a service for arbitrary users instead of just the one
of the current request.

The same modification is also made to the file service
as it is a dependency of the Canvas service.
@marcospri marcospri marked this pull request as ready for review January 8, 2025 10:08
@marcospri marcospri requested a review from acelaya January 8, 2025 10:08
Copy link
Contributor

@acelaya acelaya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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

Successfully merging this pull request may close these issues.

2 participants