-
Notifications
You must be signed in to change notification settings - Fork 7
Added schoology api's. #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shubhampatwa
wants to merge
5
commits into
quizizz:dev/schoology
Choose a base branch
from
shubhampatwa:shubham/api/schoology
base: dev/schoology
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -336,5 +336,6 @@ | |
| "inside" | ||
| ], | ||
| "yoda": 2 | ||
| } | ||
| }, | ||
| "parser": "babel-eslint" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,66 +1,199 @@ | ||
| /// <reference path='./oauth2.d.ts' /> | ||
| // / <reference path='./oauth.d.ts' /> | ||
|
|
||
| const axios = require('axios'); | ||
| const { URL, URLSearchParams } = require('url'); | ||
|
|
||
| const LMSError = require('./error.js'); | ||
|
|
||
| // function handleError(err) { | ||
| // if (err.response) { | ||
| // if (err.response.status === 401 && err.response.data.error_message === 'The access token expired') { | ||
| // throw new LMSError('Token expired', 'edmodo.EXPIRED', err.response.data); | ||
| // } | ||
|
|
||
| // if (err.response.status === 403 && err.response.data.error === 'Forbidden') { | ||
| // throw new LMSError('Unauthorized', 'edmodo.FORBIDDEN', err.response.data); | ||
| // } | ||
| // throw new LMSError('Some other api error', 'edmodo.ERROR', err.response.data); | ||
| // } else if (err.request) { | ||
| // throw new LMSError('Something wrong with request', 'edmodo.ERROR', { | ||
| // message: err.message, | ||
| // config: err.config, | ||
| // }); | ||
| // } else { | ||
| // // Something happened in setting up the request that triggered an Error | ||
| // throw new LMSError('Something completely different', 'lms.ERROR', { | ||
| // message: err.message, | ||
| // config: err.config, | ||
| // }); | ||
| // } | ||
| // } | ||
|
|
||
| exports.makeURL = function makeURL(apiURL, path, query) { | ||
| const url = new URL(apiURL); | ||
|
|
||
| if (path) { | ||
| url.pathname = path; | ||
| } | ||
|
|
||
| url.search = new URLSearchParams(query); | ||
| return url.toString(); | ||
| }; | ||
|
|
||
| exports.post = function post(host, path, query, data, headers = {}) { | ||
| return axios.post(exports.makeURL(host, path, query), data, { | ||
| headers, | ||
| responseType: 'json', | ||
| }).then(response => { | ||
| return { | ||
| status: response.status, | ||
| data: response.data, | ||
| }; | ||
| }); | ||
| }; | ||
|
|
||
| exports.get = function get(host, path, query, headers) { | ||
| return axios.get(exports.makeURL(host, path, query), { | ||
| headers, | ||
| }).then(response => { | ||
| return { | ||
| status: response.status, | ||
| data: response.data, | ||
| }; | ||
| }).catch(err => { | ||
| return handleError(err); | ||
| }); | ||
| }; | ||
| const _ = require('lodash'); | ||
| const axios = require('axios').default; | ||
| const { v4: uuidV4 } = require('uuid'); | ||
|
|
||
| class OAuth { | ||
| constructor( { | ||
| consumerKey = '', | ||
| consumerSecret = '', | ||
| apiBase = '', | ||
| authRealm = '', | ||
| signatureMethod = 'PLAINTEXT', | ||
| nonceLength = 16, | ||
| requestToken, | ||
| accessToken, | ||
| errorHandler = (() => {}) | ||
| } ) { | ||
| this.consumerKey = consumerKey; | ||
| this.consumerSecret = consumerSecret; | ||
| this.apiBase = apiBase; | ||
| this.authRealm = authRealm; | ||
| this.signatureMethod = signatureMethod; | ||
| this.nonceLength = nonceLength; | ||
| this.requestToken = { | ||
| token: requestToken.token || '', | ||
| secret: requestToken.secret || '', | ||
| expiresAt: requestToken.expiresAt || new Date() | ||
| }; | ||
| this.accessToken = { | ||
| token: accessToken.token || '', | ||
| secret: accessToken.secret || '', | ||
| expiresAt: accessToken.expiresAt || new Date() | ||
| }; | ||
| this.errorHandler = errorHandler; | ||
| } | ||
|
|
||
| static getTimeStamp () { | ||
| return parseInt(new Date().getTime()/1000, 10); | ||
| } | ||
|
|
||
| static getNonce( nonceLength ) { | ||
| return uuidV4().replace(/-/g, '').slice(-16); | ||
| } | ||
|
|
||
| static makeURL( apiURL, path, query ) { | ||
| const url = new URL(apiURL); | ||
|
|
||
| if (path) { | ||
| url.pathname = path; | ||
| } | ||
|
|
||
| url.search = new URLSearchParams(query); | ||
| return url.toString(); | ||
| } | ||
|
|
||
| static post(host, path, query, data, headers = {}) { | ||
| const url = OAuth.makeURL(host, path, query); | ||
|
|
||
| return axios.post(url, data, { | ||
| headers, | ||
| responseType: 'json', | ||
| }); | ||
| } | ||
|
|
||
| static get(host, path, query, headers) { | ||
| const url = OAuth.makeURL(host, path, query); | ||
|
|
||
| return axios.get(url, { | ||
| headers, | ||
| responseType: 'json', | ||
| }); | ||
| } | ||
|
|
||
| static jsonifyResponseString(responseString) { | ||
| const strSplits = responseString.split( '&' ); | ||
|
|
||
| return _.reduce( strSplits, ( result, keyValPair ) => { | ||
| const splits = keyValPair.split( '=' ); | ||
|
|
||
| result[ splits[0] ] = splits[1]; | ||
| return result; | ||
| }, {} ); | ||
| } | ||
|
|
||
|
|
||
| async getRequestTokens( apiPath ) { | ||
| const oAuthDetails = this.getOAuthDetails(false); | ||
| const requestedAt = Date.now(); | ||
|
|
||
| try { | ||
| const response = await OAuth.get(this.apiBase, apiPath, oAuthDetails); | ||
| const jsonified = OAuth.jsonifyResponseString(response.data); | ||
| const ttl = parseInt( jsonified[ 'xoauth_token_ttl' ] ) || 3600; | ||
|
|
||
| return { | ||
| success: true, | ||
| response: { | ||
| token: jsonified[ 'oauth_token' ], | ||
| secret: jsonified[ 'oauth_token_secret' ], | ||
| expiresAt: new Date( requestedAt + ( ttl * 1000 ) ) | ||
| } | ||
| } | ||
| } catch ( error ) { | ||
| this.errorHandler(error); | ||
| } | ||
| } | ||
|
|
||
| async getAccessTokens( apiPath ) { | ||
| const oAuthDetails = this.getOAuthDetails(); | ||
|
|
||
| try { | ||
| const response = await OAuth.get(this.apiBase, apiPath, oAuthDetails); | ||
| const jsonified = OAuth.jsonifyResponseString(response.data); | ||
|
|
||
| this.accessToken = { | ||
| token: jsonified[ 'oauth_token' ], | ||
| secret: jsonified[ 'oauth_token_secret' ], | ||
| }; | ||
|
|
||
| return { | ||
| success: true, | ||
| response: this.accessToken | ||
| } | ||
| } catch (error) { | ||
| this.errorHandler(error); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Makes a request, defined by the requestConfig, to the server | ||
| * Attempts to refresh the accessToken if server throws a "token expired" error and | ||
| * then re-attempts the request | ||
| */ | ||
| async makeRequest(requestConfig, retries = 0) { | ||
| try { | ||
| if (_.isEmpty(this.accessToken)) { | ||
| // this.errorHandler( {} ) | ||
| return; | ||
| } | ||
|
|
||
| const url = OAuth.makeURL(this.apiBase, requestConfig.url, requestConfig.query || {}); | ||
| const oAuthHeader = this.getOAuthHeader(); | ||
|
|
||
| const response = await axios({ | ||
| ...requestConfig, | ||
| url, | ||
| headers: { | ||
| ...oAuthHeader, | ||
| ...requestConfig.headers | ||
| }, | ||
| }); | ||
| const { data, status } = response; | ||
|
|
||
| return { data, status }; | ||
| } catch (error) { | ||
| this.errorHandler(error); | ||
| } | ||
| } | ||
|
|
||
| getOAuthDetails( attachAccessToken = true ) { | ||
| const timestamp = OAuth.getTimeStamp(); | ||
| const nonce = OAuth.getNonce( this.nonceLength ); | ||
| const token = this.accessToken.token || this.requestToken.token || ''; | ||
| const secret = this.accessToken.secret || this.requestToken.secret || ''; | ||
| const oAuthConfig = { | ||
| 'oauth_version': '1.0', | ||
| 'oauth_nonce': nonce, | ||
| 'oauth_timestamp': timestamp, | ||
| 'oauth_signature_method': 'PLAINTEXT' | ||
| }; | ||
|
|
||
| if ( !_.isEmpty( this.consumerKey ) ) { | ||
| oAuthConfig[ 'oauth_consumer_key' ] = this.consumerKey; | ||
| } | ||
|
|
||
| if ( attachAccessToken && !_.isEmpty( token ) ) { | ||
| oAuthConfig[ 'oauth_token' ] = token; | ||
| } | ||
|
|
||
| if ( !_.isEmpty( this.consumerSecret ) || !_.isEmpty(secret) ) { | ||
| const secretToUse = attachAccessToken ? secret : ''; | ||
| oAuthConfig[ 'oauth_signature' ] = `${this.consumerSecret}&${secretToUse}`; | ||
| } | ||
|
|
||
| return oAuthConfig; | ||
| } | ||
|
|
||
| getOAuthHeader() { | ||
| const oAuthDetails = this.getOAuthDetails(); | ||
| const headerParts = _.map(oAuthDetails, (value, key) => `${key}=${value}`); | ||
|
|
||
| return { Authorization: `OAuth realm="${this.authRealm}", ${headerParts.join(', ')}` }; | ||
| } | ||
| } | ||
|
|
||
| module.exports = OAuth; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use the
nonceLengthparam to decide the length