feat(auth): Use the frontend-auth client

- Also add some config
This commit is contained in:
Alex Dusenbery
2018-11-09 13:14:16 -05:00
committed by Alex Dusenbery
parent f94a0bd7f7
commit 45272dd8b7
8 changed files with 5671 additions and 5081 deletions

View File

@@ -1,36 +1,28 @@
import 'whatwg-fetch';
import {
STARTED_FETCHING_GRADES,
FINISHED_FETCHING_GRADES,
ERROR_FETCHING_GRADES,
GOT_GRADES,
} from '../constants/actionTypes/grades';
import LmsApiService from '../services/LmsApiService';
const startedFetchingGrades = () => ({ type: STARTED_FETCHING_GRADES });
const finishedFetchingGrades = () => ({ type: FINISHED_FETCHING_GRADES });
const errorFetchingGrades = () => ({ type: ERROR_FETCHING_GRADES });
const gotGrades = grades => ({ type: GOT_GRADES, grades });
const fetchGrades = courseId => (
(dispatch) => {
dispatch(startedFetchingGrades());
return fetch(`http://localhost:18000/api/grades/v1/gradebook/${courseId}/`, {
headers: {
'Content-Type': 'application/json',
Authorization: 'JWT eyJhbGciOiJIUzI1NiJ9.eyJzY29wZXMiOiBbInJlYWQiLCAid3JpdGUiLCAicHJvZmlsZSIsICJlbWFpbCJdLCAiYWRtaW5pc3RyYXRvciI6IHRydWUsICJhdWQiOiAibG1zLWtleSIsICJmYW1pbHlfbmFtZSI6ICIiLCAiaXNzIjogImh0dHA6Ly9lZHguZGV2c3RhY2subG1zOjE4MDAwL29hdXRoMiIsICJmaWx0ZXJzIjogW10sICJwcmVmZXJyZWRfdXNlcm5hbWUiOiAic3RhZmYiLCAibmFtZSI6ICIiLCAidmVyc2lvbiI6ICIxLjEuMCIsICJnaXZlbl9uYW1lIjogIiIsICJleHAiOiAxNTQxNzExNTU3LCAiaWF0IjogMTU0MTcwNzk1NywgImlzX3Jlc3RyaWN0ZWQiOiBmYWxzZSwgImVtYWlsIjogInN0YWZmQGV4YW1wbGUuY29tIiwgInN1YiI6ICI5MDIzNGM1NmE5NGNiODJkNWRjZDgxYTIzYmRhNjAwNyJ9.hvaJvCDVUwB533m5YDegjifDTvV5-WQLDGSrN47xjTE'
},
})
.then((response) => {
if (response.ok) {
return response.json();
}
throw new Error();
})
return LmsApiService.fetchGradebookData(courseId)
.then(response => response.data)
.then((data) => {
dispatch(gotGrades(data.results));
dispatch(finishedFetchingGrades());
})
.catch(() => dispatch(errorFetchingGrades()));
.catch((error) => {
dispatch(errorFetchingGrades())
});
}
);

13
src/data/apiClient.js Normal file
View File

@@ -0,0 +1,13 @@
import { configuration } from '../config';
import { getAuthenticatedAPIClient } from '@edx/frontend-auth';
const apiClient = getAuthenticatedAPIClient({
appBaseUrl: configuration.BASE_URL,
loginUrl: configuration.LOGIN_URL,
logoutUrl: configuration.LOGOUT_URL,
refreshAccessTokenEndpoint: configuration.REFRESH_ACCESS_TOKEN_ENDPOINT,
accessTokenCookieName: configuration.ACCESS_TOKEN_COOKIE_NAME,
csrfCookieName: configuration.CSRF_COOKIE_NAME,
});
export default apiClient;

View File

@@ -0,0 +1,14 @@
import apiClient from '../apiClient';
import { configuration } from '../../config';
class LmsApiService {
static baseUrl = configuration.LMS_BASE_URL;
static fetchGradebookData(courseId) {
const fixedCourseId = 'course-v1:edX+DemoX+Demo_Course'; // TODO: get rid of this in favor of courseId
const gradebookUrl = `${LmsApiService.baseUrl}/api/grades/v1/gradebook/${fixedCourseId}/`;
return apiClient.get(gradebookUrl);
}
}
export default LmsApiService;