-
Notifications
You must be signed in to change notification settings - Fork 30
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
feat: fetch LMS_USER_ID from LMS #584
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import logging | ||
|
||
import requests | ||
from django.conf import settings | ||
|
||
from license_manager.apps.api_client.base_oauth import BaseOAuthClient | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LMSApiClient(BaseOAuthClient): | ||
""" | ||
API client for calls to the LMS. | ||
""" | ||
api_base_url = settings.LMS_URL | ||
user_details_endpoint = api_base_url + '/api/user/v1/accounts' | ||
|
||
def fetch_lms_user_id(self, email): | ||
""" | ||
Fetch user details for the specified user email. | ||
|
||
Arguments: | ||
email (str): Email of the user for which we want to fetch details for. | ||
|
||
Returns: | ||
str: lms_user_id of the user. | ||
""" | ||
# {base_api_url}/api/user/v1/[email protected] | ||
try: | ||
query_params = {'email': email} | ||
response = self.client.get(self.user_details_endpoint, params=query_params) | ||
response.raise_for_status() | ||
response_json = response.json() | ||
return response_json[0].get('id') | ||
except requests.exceptions.HTTPError as exc: | ||
logger.error( | ||
'Failed to fetch user details for user {email} because {reason}'.format( | ||
email=email, | ||
reason=str(exc), | ||
) | ||
) | ||
raise exc | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] It may be worth following the pattern of integrating the
lms_user_id
as a model field on the coreUser
model, similar to howenterprise-access
is set up.The benefit of this is that the user id is persisted in the database for each
User
record, which is created whenever an authenticated API request is made to the service.For example, the
EDX_DRF_EXTENSIONS
setting can be modified to includelms_user_id
(example source) and thelms_user_id
field can be added to the coreUser
model (example source).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that said, I suppose this would still assume a JWT cookie with a
user_id
claim...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use the pattern Adam described above and do something like this:
lms_user_id
by email from the model in this service.