Skip to content

Commit

Permalink
now switching between http and https based on if we are in dev mode o…
Browse files Browse the repository at this point in the history
…r not, because my dev server is unencrypted
  • Loading branch information
drew-royster committed Dec 23, 2018
1 parent 90c0202 commit 6861221
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
44 changes: 32 additions & 12 deletions src/utils/apis.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -80,15 +88,15 @@ 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,
};
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`,
Expand All @@ -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,
Expand All @@ -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,
};
11 changes: 2 additions & 9 deletions src/utils/canvasIntegration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 6861221

Please sign in to comment.