Created
August 17, 2018 09:45
-
-
Save chukwuemekachm/dd7f41458c52e5a3c3e4aaff6584f811 to your computer and use it in GitHub Desktop.
Http client module to interact with COPYLEAKS_API
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
Show hidden characters
| { | |
| "presets": [ | |
| "env" | |
| ] | |
| } |
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
| COPYLEAKS_KEY=<string> | |
| COPYLEAKS_EMAIL=<string> |
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
| import httpClient from 'request-promise'; | |
| import dotenv from 'dotenv'; | |
| dotenv.config(); | |
| class Auth { | |
| constructor(email, apiKey) { | |
| this._options = { | |
| method: 'POST', | |
| uri: 'https://api.copyleaks.com/v1/account/login-api', | |
| body: { | |
| Email: email, | |
| ApiKey: apiKey, | |
| }, | |
| json: true, | |
| }; | |
| this._accessToken = {}; | |
| this.login(); | |
| } | |
| get access_token() { | |
| return this._accessToken.access_token; | |
| } | |
| login() { | |
| httpClient(this._options) | |
| .then((response) => { | |
| console.log(response); | |
| this._accessToken = response; | |
| }) | |
| .catch((error) => { | |
| console.log(error); | |
| }); | |
| } | |
| valideToken() { | |
| return new Date(this._accessToken.expires).getTime() > new Date().getTime(); | |
| } | |
| } | |
| export default new Auth(process.env.COPYLEAKS_EMAIL, process.env.COPYLEAKS_KEY); |
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
| import Scanner from './Scan'; | |
| const token = 'vYQ5QQCDfqdL3z2OqyZSEAplOnyk4Nmn8CTaq0WCYa9sr31xc_KpHkpnbuM6hC7ERE_IIPcvxe3EYh8M0n4-6u9eLxny6eqgOKwoEBBehjJgPxZzPydxhQj4nrYgE3Ek4MHIAlA4HfxV5rEjB1JXHYlchqKVJiUKc0bJWn3A2zRnycFXbFn3gmaBEDlOiEBmRlqlLM9dch3rep4xXABZMRK6xn26OUTceTy0YoNp3za1E2irHcHJO9JlBL2NVNF1VCi2MJbruckRNKvgiQ0rJTB8S5SNl-4spTlDWo7eqxibXL8fPwyMSDDqxkqZMBiOY-KcD7NeC36NCDpI8x3Y7LKV2myj5c8gBd9NCHBI_fKkBIPQIiDsmp02fzK71IdiivZSuSy0-Of_cVoGH9YAEPsl0OkOFyFQ9H5mPM2H0CEj8XcjTyaqWcq7oP2hwVGD3vCCAqV-hVvDOIYmeyKLXBlmTLMwqVCYMatWeOM6fOmiqtdMSy1H1Nlyhn97_e4Fstf3KUAZ5UtXrqXakBK4ZuZgySg'; | |
| const text = `There are many ways in which we can make ourselves feel like we have “learned” a concept. For example, looking at a solution and thinking that you know how to arrive at that solution is one of the most common illusions of competence in learning. Highlighting or underlining are also techniques that often lead to this illusion of learning. On the other hand, brief notes that summarize keys concepts are much more effective.`; | |
| const scanner = new Scanner(token); | |
| scanner.uploadScan(text); |
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
| { | |
| "name": "Http_Client", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1", | |
| "start": "babel-node client.js" | |
| }, | |
| "keywords": [], | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "dotenv": "^6.0.0", | |
| "request": "^2.87.0", | |
| "request-promise": "^4.2.2" | |
| }, | |
| "devDependencies": { | |
| "babel-cli": "^6.26.0", | |
| "babel-core": "^6.26.3", | |
| "babel-preset-env": "^1.7.0" | |
| } | |
| } |
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
| import httpClient from 'request-promise'; | |
| import dotenv from 'dotenv'; | |
| dotenv.config(); | |
| class Scan { | |
| constructor(accessToken) { | |
| this._accessToken = accessToken; | |
| } | |
| /** | |
| * @description - This method uploads a text of about 5,000 characters to copyleaks api for plagiarism checks | |
| * | |
| * @param {String} scanText - The text to be scanned | |
| */ | |
| uploadScan(scanText) { | |
| httpClient({ | |
| uri: 'https://api.copyleaks.com/v1/businesses/create-by-text', | |
| method: 'POST', | |
| headers: { | |
| 'content-type': 'application/x-www-form-urlencoded', | |
| 'authorization': `Bearer ${this._accessToken}`, | |
| }, | |
| body: scanText, | |
| json: true, | |
| }) | |
| .then((response) => { | |
| // waits for 10secs before calling checkResult | |
| setTimeout(() => { | |
| this.checkResult(response.ProcessId) | |
| }, 10000); | |
| }) | |
| .catch((error) => { | |
| console.log(error); | |
| }); | |
| } | |
| /** | |
| * @description - Checks the result of a scanning process | |
| * | |
| * @param {Number} proccessId - The process id of a scan whoose result is to be retrieved | |
| */ | |
| checkResult(proccessId) { | |
| httpClient({ | |
| uri: `https://api.copyleaks.com/v2/businesses/${proccessId}/result`, | |
| method: 'GET', | |
| headers: { | |
| 'content-type': 'application/x-www-form-urlencoded', | |
| 'authorization': `Bearer ${this._accessToken}`, | |
| }, | |
| json: true, | |
| }) | |
| .then((response) => { | |
| response.results.forEach((result) => { | |
| console.log(result); | |
| }); | |
| }) | |
| .catch((error) => { | |
| console.log(error); | |
| }); | |
| } | |
| } | |
| export default Scan; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment