Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
58812f2c
Unverified
Commit
58812f2c
authored
6 years ago
by
adeelehsan
Committed by
GitHub
6 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #19926 from edx/aehsan/LEARNER-6943/caching_full_response_instead_of_fields
Caching full response
parents
e76536b6
82a34c00
Loading
Loading
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
openedx/core/lib/edx_api_utils.py
+10
-9
10 additions, 9 deletions
openedx/core/lib/edx_api_utils.py
openedx/core/lib/tests/test_edx_api_utils.py
+40
-0
40 additions, 0 deletions
openedx/core/lib/tests/test_edx_api_utils.py
with
50 additions
and
9 deletions
openedx/core/lib/edx_api_utils.py
+
10
−
9
View file @
58812f2c
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
openedx/core/lib/tests/test_edx_api_utils.py
+
40
−
0
View file @
58812f2c
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment