adonis-kraken
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -10,12 +10,5 @@ 'use strict' | ||
*/ | ||
credentials: { | ||
api_key: Env.get('KRAKEN_API_KEY') || null, | ||
secret: Env.get('KRAKEN_SECRET') || null | ||
}, | ||
api_key: Env.get('KRAKEN_API_KEY') || null, | ||
api_secret: Env.get('KRAKEN_API_SECRET') || null | ||
/** | ||
* The current Kraken API base URL | ||
*/ | ||
base_api_url: 'https://api.kraken.io/v1' | ||
} |
{ | ||
"name": "adonis-kraken", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "Simplifies working with Kraken.io (image optimisation service) through Adonis", | ||
@@ -18,3 +18,3 @@ "main": "index.js", | ||
"author": "Liam Potter", | ||
"license": "MIT", | ||
"license": "WTFPL", | ||
"bugs": { | ||
@@ -26,4 +26,3 @@ "url": "https://github.com/liam-potter/adonis-kraken/issues" | ||
"@adonisjs/ace": "^5.0.0", | ||
"@adonisjs/fold": "^4.0.5", | ||
"@adonisjs/sink": "^1.0.13" | ||
"@adonisjs/fold": "^4.0.5" | ||
}, | ||
@@ -30,0 +29,0 @@ "dependencies": { |
@@ -14,28 +14,24 @@ 'use strict' | ||
this.options = inputConfig.merge('kraken', Config.get('kraken')) | ||
this.auth = { | ||
api_key: this.options.api_key || '', | ||
api_secret: this.options.secret || '' | ||
api_secret: this.options.api_secret || '' | ||
} | ||
this.api_url = this.options.base_api_url || 'https://api.kraken.io/v1' | ||
} | ||
/** | ||
* Creates an HTTP response handler | ||
* Rounds bytes to nearest KB, MB, GB etc | ||
* | ||
* @param {Function} cb | ||
* @param {Integer} bytes | ||
* @param {Integer} decimals | ||
*/ | ||
_createResponseHandler (cb) { | ||
return function (err, res, body) { | ||
if (err) return cb(err) | ||
return (body.success === false) | ||
? cb(new Error(body.message)) | ||
: cb(undefined, body) | ||
} | ||
_formatBytes ({ bytes, decimals = 2 }) { | ||
if (parseFloat(bytes === 0)) return '0 Bytes' | ||
const k = 1024, | ||
i = Math.floor(Math.log(bytes) / Math.log(k)), | ||
s = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] | ||
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + s[i] | ||
} | ||
/** | ||
* Pass the given image URL along with credentials to Kraken API via HTTPS POST | ||
* Optimise image from URL | ||
* | ||
@@ -45,15 +41,15 @@ * @param {Object} body | ||
*/ | ||
url (body = {}, cb) { | ||
body.auth = this.auth | ||
request.post({ | ||
url: `${this.api_url}/url`, | ||
json: true, | ||
strictSSL: false, | ||
body | ||
}, this._createResponseHandler(cb)) | ||
async url (body = {}) { | ||
try { | ||
body.auth = this.auth | ||
const { data } = await axios.post('https://api.kraken.io/v1/url', body) | ||
return data | ||
} catch (error) { | ||
console.log(error) | ||
return error | ||
} | ||
} | ||
/** | ||
* Upload the given file along with credentials to Kraken API via HTTPS POST | ||
* Optimise image from direct upload | ||
* | ||
@@ -64,5 +60,3 @@ * @param {Object} opts | ||
opts.auth = this.auth | ||
let form = new FormData() | ||
form.append('file', (opts.file && opts.file instanceof stream.Stream) | ||
@@ -72,8 +66,5 @@ ? opts.file | ||
) | ||
delete opts.file | ||
form.append('data', JSON.stringify(opts)) | ||
axios.post(`${this.api_url}/upload`, form, { headers: form.getHeaders() }) | ||
axios.post('https://api.kraken.io/v1/upload', form, { headers: form.getHeaders() }) | ||
.then((response) => response) | ||
@@ -86,4 +77,28 @@ .catch((error) => { | ||
/** | ||
* Get status for authenticated user (quota used/remaining etc) | ||
*/ | ||
async userStatus (body = {}, formatSizes = true) { | ||
try { | ||
body.auth = this.auth | ||
let { data } = await axios.post('https://api.kraken.io/user_status', { | ||
auth: { | ||
api_key: this.auth.api_key, | ||
api_secret: this.auth.api_secret | ||
} | ||
}) | ||
if (formatSizes) { | ||
data.quota_total = this._formatBytes({ bytes: data.quota_total }) | ||
data.quota_used = this._formatBytes({ bytes: data.quota_used }) | ||
data.quota_remaining = this._formatBytes({ bytes: data.quota_remaining }) | ||
} | ||
return data | ||
} catch (error) { | ||
console.log(error) | ||
return error | ||
} | ||
} | ||
} | ||
module.exports = Kraken |
Sorry, the diff of this file is not supported yet
45294
2
10
173