This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from edx/thallada/programs-endpoint
AN-8556 Add programs endpoint to client
- Loading branch information
Showing
8 changed files
with
158 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import urllib | ||
|
||
import analyticsclient.constants.data_format as DF | ||
|
||
|
||
class Programs(object): | ||
"""Programs client.""" | ||
|
||
def __init__(self, client): | ||
""" | ||
Initialize the Programs client. | ||
Arguments: | ||
client (analyticsclient.client.Client): The client to use to access remote resources. | ||
""" | ||
self.client = client | ||
|
||
def programs(self, program_ids=None, fields=None, exclude=None, data_format=DF.JSON): | ||
""" | ||
Get list of programs metadata. | ||
Arguments: | ||
program_ids: Array of program IDs as strings to return. Default is to return all. | ||
fields: Array of fields to return. Default is to return all. | ||
exclude: Array of fields to exclude from response. Default is to not exclude any fields. | ||
""" | ||
query_params = {} | ||
for query_arg, data in zip(['program_ids', 'fields', 'exclude'], | ||
[program_ids, fields, exclude]): | ||
if data: | ||
query_params[query_arg] = ','.join(data) | ||
|
||
path = 'programs/' | ||
querystring = urllib.urlencode(query_params) | ||
if querystring: | ||
path += '?{0}'.format(querystring) | ||
|
||
return self.client.get(path, data_format=data_format) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,29 @@ | ||
# pylint: disable=arguments-differ | ||
import ddt | ||
import httpretty | ||
|
||
|
||
from analyticsclient.tests import ClientTestCase | ||
from analyticsclient.tests import ClientTestCase, APIListTestCase | ||
|
||
|
||
@ddt.ddt | ||
class CourseSummariesTests(ClientTestCase): | ||
|
||
def setUp(self): | ||
super(CourseSummariesTests, self).setUp() | ||
self.base_uri = self.get_api_url('course_summaries/') | ||
self.course_summaries_client = self.client.course_summaries() | ||
httpretty.enable() | ||
|
||
def verify_last_querystring_equal(self, expected_query): | ||
""" | ||
Convenience method for asserting the last request was made with the | ||
expected query parameters. | ||
""" | ||
self.assertDictEqual(httpretty.last_request().querystring, expected_query) | ||
|
||
def expected_query(self, course_ids=None, fields=None): | ||
"""Packs the query arguments into expected format for http pretty.""" | ||
query = {} | ||
for field, data in zip(['course_ids', 'fields'], [course_ids, fields]): | ||
if data is not None: | ||
query[field] = [','.join(data)] | ||
return query | ||
class CourseSummariesTests(APIListTestCase, ClientTestCase): | ||
|
||
@httpretty.activate | ||
def test_all_summaries_url(self): | ||
"""Course summaries can be called without parameters.""" | ||
httpretty.register_uri(httpretty.GET, self.base_uri, body='{}') | ||
self.course_summaries_client.course_summaries() | ||
|
||
@httpretty.activate | ||
@ddt.data( | ||
['edx/demo/course'], | ||
['edx/demo/course', 'another/demo/course'] | ||
) | ||
def test_courses_ids(self, course_ids): | ||
"""Course summaries can be called with course IDs""" | ||
uri_template = '{uri}?course_ids={ids}' | ||
uri = uri_template.format(uri=self.base_uri, ids=course_ids) | ||
httpretty.register_uri(httpretty.GET, uri, body='{}') | ||
self.course_summaries_client.course_summaries(course_ids=course_ids) | ||
self.verify_last_querystring_equal(self.expected_query(course_ids=course_ids)) | ||
endpoint = 'course_summaries' | ||
id_field = 'course_ids' | ||
|
||
@httpretty.activate | ||
@ddt.data( | ||
['course_id'], | ||
['course_id', 'enrollment_modes'] | ||
['123'], | ||
['123', '456'] | ||
) | ||
def test_fields(self, fields): | ||
"""Course summaries can be called with fields.""" | ||
uri_template = '{uri}?fields={fields}' | ||
uri = uri_template.format(uri=self.base_uri, fields=fields[0]) | ||
httpretty.register_uri(httpretty.GET, uri, body='{}') | ||
self.course_summaries_client.course_summaries(fields=fields) | ||
self.verify_last_querystring_equal(self.expected_query(fields=fields)) | ||
def test_programs(self, programs): | ||
"""Course summaries can be called with programs.""" | ||
self.kwarg_test(programs=programs) | ||
|
||
@httpretty.activate | ||
@ddt.data( | ||
(['edx/demo/course'], ['course_id']), | ||
(['edx/demo/course', 'another/demo/course'], ['course_id', 'enrollment_modes']) | ||
(['edx/demo/course'], ['course_id'], ['enrollment_modes'], ['123']), | ||
(['edx/demo/course', 'another/demo/course'], ['course_id', 'enrollment_modes'], | ||
['created', 'pacing_type'], ['123', '456']) | ||
) | ||
@ddt.unpack | ||
def test_all_parameters(self, course_ids, fields): | ||
"""Course summaries can be called with both fields and course IDs.""" | ||
httpretty.reset() | ||
uri_template = '{uri}?course_ids={ids}fields={fields}' | ||
uri = uri_template.format(uri=self.base_uri, ids=course_ids, fields=fields) | ||
httpretty.register_uri(httpretty.GET, uri, body='{}') | ||
self.course_summaries_client.course_summaries(course_ids=course_ids, fields=fields) | ||
self.verify_last_querystring_equal(self.expected_query(course_ids=course_ids, fields=fields)) | ||
def test_all_parameters(self, course_ids, fields, exclude, programs): | ||
"""Course summaries can be called with all parameters including programs.""" | ||
self.kwarg_test(course_ids=course_ids, fields=fields, exclude=exclude, programs=programs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import ddt | ||
|
||
from analyticsclient.tests import APIListTestCase, ClientTestCase | ||
|
||
|
||
@ddt.ddt | ||
class ProgramsTests(APIListTestCase, ClientTestCase): | ||
|
||
endpoint = 'programs' | ||
id_field = 'program_ids' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters