[FC-0031] Mobile API Migration Solution Notes
High-level notes about changes RG introduced in the edx-plugin to correct behavior for mobile apps.
DeactivateLogoutView
As mobile apps are using bearer token auth, the BearerAuthentication
class authentication_classes
for this API View should be added. This view is used in the mobile account settings, to allow user deactivate (delete) their account from the system.
UserCourseEnrollmentsList
https://github.com/openedx/edx-platform/blob/master/lms/djangoapps/mobile_api/users/views.py#L253
The pagination is necessary to optimize course loading in the mobile app.
Solution:
Increment API version to version 3
Introduce
DefaultPagination
class to the API’s, paginator property
urlpatterns += [
re_path(r'^api/mobile/(?P<api_version>v(3|2|1|0.5))/', include('lms.djangoapps.mobile_api.urls')),
]
@property
def paginator(self):
super().paginator
api_version = self.kwargs.get('api_version')
if self._paginator is None and api_version == API_V3:
self._paginator = DefaultPagination
return self._paginator
CommentViewSet
https://github.com/openedx/edx-platform/blob/master/lms/djangoapps/discussion/rest_api/views.py#L746
It is required to extend POST response with the data about profile image. This way it’s possible to load avatar image for a post or response without an additional request to a separate account settings endpoint.
"profile_image": {
"has_image": true,
"image_url_full": "http://localhost:18000/media/profile-images/765490233e5640ad12a58d58370a3512_500.jpg?v=1677749000",
"image_url_large": "http://localhost:18000/media/profile-images/765490233e5640ad12a58d58370a3512_120.jpg?v=1677749000",
"image_url_medium": "http://localhost:18000/media/profile-images/765490233e5640ad12a58d58370a3512_50.jpg?v=1677749000",
"image_url_small": "http://localhost:18000/media/profile-images/765490233e5640ad12a58d58370a3512_30.jpg?v=1677749000"
}
Solution:
Add field profile_image
to CommentSerializer
class CommentSerializer(_ContentSerializer):
...
profile_image = serializers.SerializerMethodField(read_only=True)
def get_profile_image(self, obj):
request = self.context['request']
return AccountLegacyProfileSerializer.get_profile_image(request.user.profile, request.user, request)
_filter_by_search
Bug
While searching for courses using API api/courses/v1/courses/
and providing search_term
query parameter, pagination
object returns an incorrect count for all pages except for the last page.
Solution:
def _filter_by_search(course_queryset, search_term):
"""
Filters a course queryset by the specified search term.
"""
if not settings.FEATURES['ENABLE_COURSEWARE_SEARCH'] or not search_term:
return course_queryset
# Return all the results, 10K is the maximum allowed value for ElasticSearch.
# We should use 0 after upgrading to 1.1+:
# - https://github.com/elastic/elasticsearch/commit/8b0a863d427b4ebcbcfb1dcd69c996c52e7ae05e
results_size_infinity = 10000
search_courses = search.api.course_discovery_search(
search_term,
size=results_size_infinity,
)
search_courses_ids = {course['data']['id'] for course in search_courses['results']}
courses = [course for course in course_queryset if str(course.id) in search_courses_ids]
return LazySequence(
iter(courses),
est_len=len(courses)
)
A new endpoint to get the course home data
The endpoint should extend BlocksInCourseView, and add more data about a course as media
, course dates
, certificate
etc.
Solution:
This endpoint can be added into mobile_api LMS application. In the future, this endpoint can be extended to be a course home endpoint for a mobile.
Example response:
BlocksInCourseView response +
"id": "course-v1:test+test1+test1_247",
"name": "test1",
"number": "test1",
"org": "test",
"start": "2022-05-30T00:00:00Z",
"start_display": "May 30, 2022",
"start_type": "timestamp",
"end": "2022-08-28T00:00:00Z",
"media": {
"image": {
"raw": "/asset-v1:test+test1+test1_247+type@asset+block@images_course_image.jpg",
"small": "/asset-v1:test+test1+test1_247+type@asset+block@images_course_image.jpg",
"large": "/asset-v1:test+test1+test1_247+type@asset+block@images_course_image.jpg"
}
},
"certificate": {
"url": "http://localhost:18000/certificates/61630ecf103c4afaa50ff2dd275ab5cf"
},
"is_self_paced": false
A new endpoint to return about course information
This view can be based on the CourseDetailView and CourseHomeMetadataView, but additionally should provide if user is enrolled to the course or not (maybe if user has some specific visibility conditions.
Solution:
Add a new endpoint into mobile_api LMS application. URL for the new endpoint /mobile/{api_version}/course_info/{course_id}