-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #584 from openedx/sameen/ENT-8351
feat: fetch LMS_USER_ID from LMS
- Loading branch information
Showing
3 changed files
with
66 additions
and
6 deletions.
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 |