Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
381d136a
Commit
381d136a
authored
Mar 02, 2017
by
Saleem Latif
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a switch for selecting pagination traversal in edx_api_utils
parent
4cc76286
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
3 deletions
+36
-3
openedx/core/lib/edx_api_utils.py
+6
-3
openedx/core/lib/tests/test_edx_api_utils.py
+30
-0
No files found.
openedx/core/lib/edx_api_utils.py
View file @
381d136a
...
@@ -14,8 +14,8 @@ from openedx.core.lib.token_utils import JwtBuilder
...
@@ -14,8 +14,8 @@ from openedx.core.lib.token_utils import JwtBuilder
log
=
logging
.
getLogger
(
__name__
)
log
=
logging
.
getLogger
(
__name__
)
def
get_edx_api_data
(
api_config
,
user
,
resource
,
def
get_edx_api_data
(
api_config
,
user
,
resource
,
api
=
None
,
resource_id
=
None
,
api
=
None
,
resource_id
=
None
,
querystring
=
None
,
cache_key
=
None
,
many
=
True
):
querystring
=
None
,
cache_key
=
None
,
many
=
True
,
traverse_pagination
=
True
):
"""GET data from an edX REST API.
"""GET data from an edX REST API.
DRY utility for handling caching and pagination.
DRY utility for handling caching and pagination.
...
@@ -33,6 +33,7 @@ def get_edx_api_data(api_config, user, resource,
...
@@ -33,6 +33,7 @@ def get_edx_api_data(api_config, user, resource,
(neither inspected nor updated).
(neither inspected nor updated).
many (bool): Whether the resource requested is a collection of objects, or a single object.
many (bool): Whether the resource requested is a collection of objects, or a single object.
If false, an empty dict will be returned in cases of failure rather than the default empty list.
If false, an empty dict will be returned in cases of failure rather than the default empty list.
traverse_pagination (bool): Whether to traverse pagination or return paginated response..
Returns:
Returns:
Data returned by the API. When hitting a list endpoint, extracts "results" (list of dict)
Data returned by the API. When hitting a list endpoint, extracts "results" (list of dict)
...
@@ -79,8 +80,10 @@ def get_edx_api_data(api_config, user, resource,
...
@@ -79,8 +80,10 @@ def get_edx_api_data(api_config, user, resource,
if
resource_id
is
not
None
:
if
resource_id
is
not
None
:
results
=
response
results
=
response
el
se
:
el
if
traverse_pagination
:
results
=
_traverse_pagination
(
response
,
endpoint
,
querystring
,
no_data
)
results
=
_traverse_pagination
(
response
,
endpoint
,
querystring
,
no_data
)
else
:
results
=
response
except
:
# pylint: disable=bare-except
except
:
# pylint: disable=bare-except
log
.
exception
(
'Failed to retrieve data from the
%
s API.'
,
api_config
.
API_NAME
)
log
.
exception
(
'Failed to retrieve data from the
%
s API.'
,
api_config
.
API_NAME
)
return
no_data
return
no_data
...
...
openedx/core/lib/tests/test_edx_api_utils.py
View file @
381d136a
...
@@ -101,6 +101,36 @@ class TestGetEdxApiData(CatalogIntegrationMixin, CredentialsApiConfigMixin, Cach
...
@@ -101,6 +101,36 @@ class TestGetEdxApiData(CatalogIntegrationMixin, CredentialsApiConfigMixin, Cach
self
.
_assert_num_requests
(
len
(
expected_collection
))
self
.
_assert_num_requests
(
len
(
expected_collection
))
def
test_get_paginated_data_do_not_traverse_pagination
(
self
):
"""
Verify that pagination is not traversed if traverse_pagination=False is passed as argument.
"""
catalog_integration
=
self
.
create_catalog_integration
()
api
=
create_catalog_api_client
(
self
.
user
,
catalog_integration
)
url
=
CatalogIntegration
.
current
()
.
internal_api_url
.
strip
(
'/'
)
+
'/programs/?page={}'
responses
=
[
{
'next'
:
url
.
format
(
2
),
'results'
:
[
'some'
],
},
{
'next'
:
url
.
format
(
None
),
'results'
:
[
'test'
],
},
]
expected_response
=
responses
[
0
]
self
.
_mock_catalog_api
(
[
httpretty
.
Response
(
body
=
json
.
dumps
(
body
),
content_type
=
'application/json'
)
for
body
in
responses
]
)
actual_collection
=
get_edx_api_data
(
catalog_integration
,
self
.
user
,
'programs'
,
api
=
api
,
traverse_pagination
=
False
,
)
self
.
assertEqual
(
actual_collection
,
expected_response
)
self
.
_assert_num_requests
(
1
)
def
test_get_specific_resource
(
self
):
def
test_get_specific_resource
(
self
):
"""Verify that a specific resource can be retrieved."""
"""Verify that a specific resource can be retrieved."""
catalog_integration
=
self
.
create_catalog_integration
()
catalog_integration
=
self
.
create_catalog_integration
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment