From e93f035876b419e22b213a2870eeac8f30dded39 Mon Sep 17 00:00:00 2001 From: Ben Holt Date: Mon, 15 Mar 2021 14:43:00 -0400 Subject: [PATCH] feat: exposed 'Date' header so clients can compute accurate relative time (#26979) REV-2126 Exposed the Date header so clients can accurately compute times relative to the dates returned by the API; browser time is notoriously unreliable for this, especially for a Learner-facing countdown call-to-action based on the access expiration date. (REV-2126) Using the Date header for this allows the client to make use of information that is already sent, does not require additional calls nor modifying the API, and could be generalized to more or all our APIs without modifying them. --- openedx/core/djangoapps/courseware_api/views.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/openedx/core/djangoapps/courseware_api/views.py b/openedx/core/djangoapps/courseware_api/views.py index bb73f0fc82..40ea30ac22 100644 --- a/openedx/core/djangoapps/courseware_api/views.py +++ b/openedx/core/djangoapps/courseware_api/views.py @@ -441,6 +441,17 @@ class CoursewareInformation(RetrieveAPIView): context['requested_fields'] = self.request.GET.get('requested_fields', None) return context + def finalize_response(self, request, response, *args, **kwargs): + """ + Return the final response, exposing the 'Date' header for computing relative time to the dates in the data. + """ + response = super().finalize_response(request, response, *args, **kwargs) + # Adding this header should be moved somewhere global, not just this endpoint + exposedHeaders = response.get('Access-Control-Expose-Headers', '') + exposedHeaders += ', Date' if exposedHeaders else 'Date' + response['Access-Control-Expose-Headers'] = exposedHeaders + return response + class SequenceMetadata(DeveloperErrorViewMixin, APIView): """