feat: [VAN-986] Add amplitude API (#30675)
* feat: [VAN-986] Add amplitude API * feat: add suggestion * feat: add spinner Co-authored-by: Zainab Amir <zainab.amir@arbisoft.com>
This commit is contained in:
31
lms/djangoapps/learner_dashboard/api/utils.py
Normal file
31
lms/djangoapps/learner_dashboard/api/utils.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""API utils"""
|
||||
|
||||
import logging
|
||||
import requests
|
||||
from django.conf import settings
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_personalized_course_recommendations(user_id):
|
||||
""" get personalize recommendations from Amplitude. """
|
||||
headers = {
|
||||
'Authorization': f'Api-Key {settings.AMPLITUDE_API_KEY}',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
params = {
|
||||
'user_id': user_id,
|
||||
'get_recs': True,
|
||||
'rec_id': settings.REC_ID,
|
||||
}
|
||||
try:
|
||||
response = requests.get(settings.AMPLITUDE_URL, params=params, headers=headers)
|
||||
if response.status_code == 200:
|
||||
response = response.json()
|
||||
is_control = response['userData']['recommendations'][0]['is_control']
|
||||
course_keys = response['userData']['recommendations'][0]['items']
|
||||
return is_control, course_keys
|
||||
except Exception as ex: # pylint: disable=broad-except
|
||||
log.exception(f'Cannot get recommendations from Amplitude: {ex}')
|
||||
|
||||
return True, []
|
||||
Reference in New Issue
Block a user