def get_enterprise_customers(site): resource = 'enterprise-customer' client = get_enterprise_api_client(site) endpoint = getattr(client, resource) response = endpoint.get() return [{ 'name': each['name'], 'id': each['uuid'], } for each in traverse_pagination(response, endpoint)]
def get_user_ownership_data(self, basket, retrieve_entitlements=False): """ Retrieves existing enrollments and entitlements for a user from LMS """ enrollments = [] entitlements = [] site_configuration = basket.site.siteconfiguration if site_configuration.enable_partial_program: enrollments = self.get_lms_resource( basket, 'enrollments', site_configuration.enrollment_api_client.enrollment) if retrieve_entitlements: response = self.get_lms_resource( basket, 'entitlements', site_configuration.entitlement_api_client.entitlements) if isinstance(response, dict): entitlements = traverse_pagination( response, site_configuration.entitlement_api_client.entitlements) else: entitlements = response return enrollments, entitlements
def get_course_catalogs(site, resource_id=None): """ Get details related to course catalogs from Catalog Service. Arguments: site (Site): Site object containing Site Configuration data resource_id (int or str): Identifies a specific resource to be retrieved Returns: dict: Course catalogs received from Course Catalog API Raises: ConnectionError: requests exception "ConnectionError" SlumberBaseException: slumber exception "SlumberBaseException" Timeout: requests exception "Timeout" """ resource = 'catalogs' base_cache_key = '{}.catalog.api.data'.format(site.domain) cache_key = '{}.{}'.format(base_cache_key, resource_id) if resource_id else base_cache_key cache_key = hashlib.md5(cache_key).hexdigest() cached = cache.get(cache_key) if cached: return cached api = site.siteconfiguration.course_catalog_api_client endpoint = getattr(api, resource) response = endpoint(resource_id).get() if resource_id: results = response else: results = traverse_pagination(response, endpoint) cache.set(cache_key, results, settings.COURSES_API_CACHE_TIMEOUT) return results
def get_catalog_course_runs(site, query, limit=None, offset=None): """ Get course runs for a site on the basis of provided query from the Course Catalog API. This method will get all course runs by recursively retrieving API next urls in the API response if no limit is provided. Arguments: limit (int): Number of results per page offset (int): Page offset query (str): ElasticSearch Query site (Site): Site object containing Site Configuration data Example: >>> get_catalog_course_runs(site, query, limit=1) { "count": 1, "next": "None", "previous": "None", "results": [{ "key": "course-v1:edX+DemoX+Demo_Course", "title": edX Demonstration Course, "start": "2016-05-01T00:00:00Z", "image": { "src": "path/to/the/course/image" }, "enrollment_end": None }], } Returns: dict: Query search results for course runs received from Course Catalog API Raises: ConnectionError: requests exception "ConnectionError" SlumberBaseException: slumber exception "SlumberBaseException" Timeout: requests exception "Timeout" """ api_resource_name = 'course_runs' partner_code = site.siteconfiguration.partner.short_code cache_key = '{site_domain}_{partner_code}_{resource}_{query}_{limit}_{offset}'.format( site_domain=site.domain, partner_code=partner_code, resource=api_resource_name, query=query, limit=limit, offset=offset) cache_key = hashlib.md5(cache_key).hexdigest() response = cache.get(cache_key) if not response: api = site.siteconfiguration.discovery_api_client endpoint = getattr(api, api_resource_name) if limit: response = endpoint().get(partner=partner_code, q=query, limit=limit, offset=offset) else: response = endpoint().get(partner=partner_code, q=query) all_response_results = traverse_pagination(response, endpoint) response = { 'count': len(all_response_results), 'next': 'None', 'previous': 'None', 'results': all_response_results, } cache.set(cache_key, response, settings.COURSES_API_CACHE_TIMEOUT) return response