From 68612211fdd2d15fab83c6279fb37fbf9a462126 Mon Sep 17 00:00:00 2001 From: Drew Royster Date: Sat, 22 Dec 2018 22:30:13 -0700 Subject: [PATCH] now switching between http and https based on if we are in dev mode or not, because my dev server is unencrypted --- src/utils/apis.js | 44 ++++++++++++++++++++++++---------- src/utils/canvasIntegration.js | 11 ++------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/utils/apis.js b/src/utils/apis.js index 986d4c6..9406a35 100644 --- a/src/utils/apis.js +++ b/src/utils/apis.js @@ -1,6 +1,14 @@ const log = require('electron-log'); const request = require('request-promise'); +let protocol; + +if (process.env.NODE_ENV !== 'development') { + protocol = 'https://'; +} else { + protocol = 'http://'; +} + const listActiveCanvasCourses = ( authToken, rootURL, @@ -9,7 +17,7 @@ const listActiveCanvasCourses = ( try { const options = { method: 'GET', - uri: `https://${rootURL}/api/v1/users/self/courses?enrollment_state=active`, + uri: `${protocol}${rootURL}/api/v1/users/self/courses?enrollment_state=active`, headers: { Authorization: `Bearer ${authToken}` }, json: true, encoding: null, @@ -25,7 +33,7 @@ const listActiveCanvasCourses = ( const listModules = async (authToken, rootURL, course) => { const options = { method: 'GET', - uri: `https://${rootURL}/api/v1/courses/${course.id}/modules?per_page=100`, + uri: `${protocol}${rootURL}/api/v1/courses/${course.id}/modules?per_page=100`, headers: { Authorization: `Bearer ${authToken}` }, json: true, encoding: null, @@ -69,7 +77,7 @@ const getModuleFileDetails = async (authToken, fileModuleURL) => { const getCourseRootFolder = async (authToken, rootURL, courseID) => { const options = { method: 'GET', - uri: `https://${rootURL}/api/v1/courses/${courseID}/folders/root`, + uri: `${protocol}${rootURL}/api/v1/courses/${courseID}/folders/root`, headers: { Authorization: `Bearer ${authToken}` }, json: true, encoding: null, @@ -80,7 +88,7 @@ const getCourseRootFolder = async (authToken, rootURL, courseID) => { const listFoldersByUpdatedAt = async (authToken, rootURL, courseID) => { const options = { method: 'GET', - uri: `https://${rootURL}/api/v1/courses/${courseID}/folders?sort=updated_at&order=desc&per_page=200`, + uri: `${protocol}${rootURL}/api/v1/courses/${courseID}/folders?sort=updated_at&order=desc&per_page=200`, headers: { Authorization: `Bearer ${authToken}` }, json: true, encoding: null, @@ -88,7 +96,7 @@ const listFoldersByUpdatedAt = async (authToken, rootURL, courseID) => { return request.get(options); }; -const listFilesByUpdatedAt = async (authToken, filesURL) => { +const list200FilesByUpdatedAt = async (authToken, filesURL) => { const options = { method: 'GET', uri: `${filesURL}/?per_page=200&sort=updated_at&order=desc`, @@ -99,10 +107,21 @@ const listFilesByUpdatedAt = async (authToken, filesURL) => { return request.get(options); }; +const getLatestFile = async (authToken, rootURL, courseID) => { + const options = { + method: 'GET', + uri: `${protocol}${rootURL}/api/v1/courses/${courseID}/files?per_page=1&sort=updated_at&order=desc`, + headers: { Authorization: `Bearer ${authToken}` }, + json: true, + encoding: null, + }; + return request.get(options); +}; + const listCourseTabs = async (authToken, rootURL, courseID) => { const options = { method: 'GET', - uri: `https://${rootURL}/api/v1/courses/${courseID}/tabs`, + uri: `${protocol}${rootURL}/api/v1/courses/${courseID}/tabs`, headers: { Authorization: `Bearer ${authToken}` }, json: true, encoding: null, @@ -111,13 +130,14 @@ const listCourseTabs = async (authToken, rootURL, courseID) => { }; export default { - listActiveCanvasCourses, - listModules, - listModuleItems, + getLatestFile, + getCourseRootFolder, getModuleFileDetails, + listModules, list200Items, - getCourseRootFolder, - listFoldersByUpdatedAt, - listFilesByUpdatedAt, listCourseTabs, + listModuleItems, + listFoldersByUpdatedAt, + listActiveCanvasCourses, + list200FilesByUpdatedAt, }; diff --git a/src/utils/canvasIntegration.js b/src/utils/canvasIntegration.js index 3616a4e..55d34ef 100644 --- a/src/utils/canvasIntegration.js +++ b/src/utils/canvasIntegration.js @@ -199,7 +199,7 @@ const getFiles = async (authToken, filesURL, currentPath) => { //Right now this will only get 200 files may want to add recursion into this as well const getNewOrUpdatedFiles = async (authToken, filesURL, currentPath, lastSynced) => { try { - const filesResponse = await apis.listFilesByUpdatedAt(authToken, filesURL); + const filesResponse = await apis.list200FilesByUpdatedAt(authToken, filesURL); // filter files updated more recently than lastSynced const newFiles = _.filter(filesResponse, (file) => { if (new Date(file.updated_at) > new Date(lastSynced)) { @@ -338,14 +338,7 @@ const getNewFolders = async (authToken, rootURL, course, lastSynced) => { const hasNewFile = async (authToken, rootURL, courseID, lastSynced) => { try { - const options = { - method: 'GET', - uri: `https://${rootURL}/api/v1/courses/${courseID}/files?sort=updated_at&order=desc`, - headers: { Authorization: `Bearer ${authToken}` }, - json: true, - encoding: null, - }; - const filesLastUpdated = await request(options); + const filesLastUpdated = await apis.getLatestFile(authToken, rootURL, courseID); // theoretically this works, but it is not yet tested all the way through if (new Date(filesLastUpdated[0].updated_at) > new Date(lastSynced)) {