diff --git a/openedx/core/lib/edx_api_utils.py b/openedx/core/lib/edx_api_utils.py index 1df3668f26..f72bd749c5 100644 --- a/openedx/core/lib/edx_api_utils.py +++ b/openedx/core/lib/edx_api_utils.py @@ -58,7 +58,11 @@ def get_edx_api_data(api_config, resource, api, resource_id=None, querystring=No log.info("Cached course run was returned for the course: {resource_id} using the key:{cache_key}" " and response is {cached} ".format(resource_id=resource_id, cache_key=cache_key, cached=zunpickle(cached))) - return zunpickle(cached) + cached_response = zunpickle(cached) + if fields: + cached_response = get_fields(fields, cached_response) + + return cached_response try: endpoint = getattr(api, resource) @@ -68,17 +72,11 @@ def get_edx_api_data(api_config, resource, api, resource_id=None, querystring=No log.info("Response for the course: {resource_id} from discovery: {response} ". format(resource_id=resource_id, response=response)) - if resource_id is not None: - if fields: - log.info("Getting following fields:{fields} for the course:{resource_id}".format( - fields=fields, resource_id=resource_id)) - results = get_fields(fields, response) - else: - results = response - elif traverse_pagination: + if resource_id is None and traverse_pagination: results = _traverse_pagination(response, endpoint, querystring, no_data) else: results = response + except: # pylint: disable=bare-except log.exception('Failed to retrieve data from the %s API.', api_config.API_NAME) return no_data @@ -92,6 +90,9 @@ def get_edx_api_data(api_config, resource, api, resource_id=None, querystring=No resource_id=resource_id, cache_key=cache_key, results=results)) cache.set(cache_key, zdata, cache_ttl) + if fields: + results = get_fields(fields, results) + return results diff --git a/openedx/core/lib/tests/test_edx_api_utils.py b/openedx/core/lib/tests/test_edx_api_utils.py index d3280e983e..b2c2e842f0 100644 --- a/openedx/core/lib/tests/test_edx_api_utils.py +++ b/openedx/core/lib/tests/test_edx_api_utils.py @@ -175,6 +175,46 @@ class TestGetEdxApiData(CatalogIntegrationMixin, CredentialsApiConfigMixin, Cach self._assert_num_requests(1) + def test_get_specific_fields_from_cache_response(self): + """Verify that resource response is cached and get required fields from cached response""" + catalog_integration = self.create_catalog_integration(cache_ttl=5) + api = create_catalog_api_client(self.user) + + response = {'lang': 'en', 'weeks_to_complete': '5'} + + resource_id = 'course-v1:testX+testABC+1T2019' + url = '{api_root}/course_runs/{resource_id}/'.format( + api_root=CatalogIntegration.current().get_internal_api_url().strip('/'), + resource_id=resource_id, + ) + + expected_resource_for_lang = {'lang': 'en'} + expected_resource_for_weeks_to_complete = {'weeks_to_complete': '5'} + + self._mock_catalog_api( + [httpretty.Response(body=json.dumps(response), content_type='application/json')], + url=url + ) + + cache_key = CatalogIntegration.current().CACHE_KEY + + # get response and set the cache. + actual_resource_for_lang = get_edx_api_data( + catalog_integration, 'course_runs', resource_id=resource_id, api=api, cache_key=cache_key, fields=['lang'] + ) + self.assertEqual(actual_resource_for_lang, expected_resource_for_lang) + + # Hit the cache + actual_resource = get_edx_api_data( + catalog_integration, 'course_runs', api=api, resource_id=resource_id, cache_key=cache_key, + fields=['weeks_to_complete'] + ) + + self.assertEqual(actual_resource, expected_resource_for_weeks_to_complete) + + # Verify that only one requests were made, not three. + self._assert_num_requests(1) + def test_cache_utilization(self): """Verify that when enabled, the cache is used.""" catalog_integration = self.create_catalog_integration(cache_ttl=5)