Skip to content

Commit

Permalink
Merge branch 'issue-9'
Browse files Browse the repository at this point in the history
  • Loading branch information
kkamkou committed Jan 25, 2017
1 parent a42a2b0 commit de21e9e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 64 deletions.
12 changes: 12 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
engines:
duplication:
enabled: false
eslint:
enabled: true
fixme:
enabled: true
ratings:
paths:
- "lib/**.js"
exclude_paths:
- test/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
/node_modules/
/coverage/
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: node_js
node_js:
- "5.12"
notifications:
slack:
on_success: never
on_failure: always
sudo: false
deploy:
provider: npm
api_key: $NPM_TOKEN
email: $NPM_EMAIL
on:
tags: true
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Akamai NetStorage HTTP API for Node.js (Unofficial).
Official library [might be found here](https://github.com/akamai-open/NetStorageKit-Node) (though, the quality of this library is suspicious).

[![Dependency Status](https://www.versioneye.com/user/projects/56eca2d34fb9b000127dc24e/badge.svg?style=flat)](https://www.versioneye.com/user/projects/56eca2d34fb9b000127dc24e)
[![Coverage Status](https://coveralls.io/repos/github/kkamkou/node-akamai-http-api/badge.svg?branch=master)](https://coveralls.io/github/kkamkou/node-akamai-http-api?branch=master)
[![Code Climate](https://codeclimate.com/github/kkamkou/node-akamai-http-api/badges/gpa.svg)](https://codeclimate.com/github/kkamkou/node-akamai-http-api)
[![Build Status](https://travis-ci.org/kkamkou/node-akamai-http-api.svg?branch=master)](https://travis-ci.org/kkamkou/node-akamai-http-api)

## Installation
```
Expand Down
92 changes: 38 additions & 54 deletions lib/akamai.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ akamai.getRequestObject = function (path, params, cb) {

if (typeof(cb) === 'function') {
callback = function (err, resp, body) {
/* istanbul ignore if */
if (err) {
return cb(err);
}
Expand All @@ -173,28 +174,50 @@ akamai.getRequestObject = function (path, params, cb) {
return request(options, callback);
};

/**
* Sugar function to simplify calls
* @param {string} path
* @param {string} method
* @param {object} headers
* @param {function} cb
* @returns {request}
* @private
*/
akamai._simpleRequestObject = function (path, method, headers, cb) {
return this.getRequestObject(path, {request: {method: method}, headers: headers}, cb);
};

/** Custom helpers **/

akamai.fileExists = function (path, cb) {
return this.stat(path, function (err, data) {
this.stat(path, function (err, data) {
if (err && err.message.indexOf('404 code') !== -1) {
return cb(null, false);
}
if (data && data.stat && data.stat.file) {
return cb(null, true);
}
/* istanbul ignore if */
return cb(err);
});
return this;
};

/** Api functions **/

_.each(
{stat: 'get', du: 'get', dir: 'get', 'delete': 'put', mkdir: 'put', rmdir: 'put'},
function (method, fnc) {
akamai[fnc] = function (path, cb) {
this._simpleRequestObject(path, method, {action: fnc}, cb);
return this;
};
}
);

akamai.upload = function (stream, path, cb) {
var options = {
request: {method: 'put'},
headers: {action: 'upload', 'upload-type': 'binary'}
};
stream.pipe(this.getRequestObject(path, options, cb));
stream
.pipe(this._simpleRequestObject(path, 'put', {action: 'upload', 'upload-type': 'binary'}, cb));
return this;
};

Expand All @@ -203,64 +226,25 @@ akamai.download = function (path, stream, cb) {
return this;
};

akamai.stat = function (path, cb) {
this.getRequestObject(path, {headers: {action: 'stat'}}, cb);
return this;
};

akamai.du = function (path, cb) {
this.getRequestObject(path, {headers: {action: 'du'}}, cb);
return this;
};

akamai.dir = function (path, cb) {
this.getRequestObject(path, {headers: {action: 'dir'}}, cb);
return this;
};

akamai.delete = function (path, cb) {
this.getRequestObject(path, {request: {method: 'put'}, headers: {action: 'delete'}}, cb);
return this;
};

akamai.mkdir = function (path, cb) {
this.getRequestObject(path, {request: {method: 'put'}, headers: {action: 'mkdir'}}, cb);
return this;
};

akamai.rmdir = function (path, cb) {
this.getRequestObject(path, {request: {method: 'put'}, headers: {action: 'rmdir'}}, cb);
return this;
};

akamai.rename = function (pathFrom, pathTo, cb) {
var options = {
request: {method: 'put'},
headers: {action: 'rename', destination: pathTo}
};
this.getRequestObject(pathFrom, options, cb);
this._simpleRequestObject(pathFrom, 'put', {action: 'rename', destination: pathTo}, cb);
return this;
};

akamai.symlink = function (pathFileTo, pathFileFrom, cb) {
var options = {
request: {method: 'put'},
headers: {action: 'symlink', target: pathFileTo}
};
this.getRequestObject(pathFileFrom, options, cb);
akamai.symlink = function (pathFileTo, pathSymlink, cb) {
this._simpleRequestObject(pathSymlink, 'put', {action: 'symlink', target: pathFileTo}, cb);
return this;
};

akamai.mtime = function (path, date, cb) {
if (!(date instanceof Date)) {
return cb(new TypeError('The date has to be an instance of Date'));
cb(new TypeError('The date has to be an instance of Date'));
return this;
}

var options = {
request: {method: 'put'},
headers: {action: 'mtime', mtime: parseInt(date.getTime() / 1000, 10)}
};
this.getRequestObject(path, options, cb);
this._simpleRequestObject(
path, 'put', {action: 'mtime', mtime: parseInt(date.getTime() / 1000, 10)},
cb
);
return this;
};

Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "akamai-http-api",
"version": "0.5.1",
"version": "0.6.0",
"main": "./lib/akamai.js",
"author": "Kanstantsin Kamkou <kkamkou@gmail.com>",
"description": "Akamai NetStorage HTTP API for the Node.js",
"description": "Akamai NetStorage HTTP API for Node.js",
"keywords": ["akamai", "api", "http", "netstorage"],
"repository" : {
"type" : "git",
Expand All @@ -14,15 +14,17 @@
},
"license" : "MIT",
"scripts": {
"test": "node_modules/.bin/mocha"
"test": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"dependencies": {
"lodash": "~4.16",
"request": "~2.78",
"xml2js": ">=0.4.16"
},
"devDependencies": {
"should": "~11.1",
"mocha": "~3.1"
"coveralls": "~2.11",
"istanbul": "~0.4.3",
"mocha": "~3.1",
"should": "~11.1"
}
}
18 changes: 13 additions & 5 deletions test/akamai.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ var path = require('path'),
akamai = require(path.join('..', 'lib', 'akamai'));

// config and the remote path for a file
var pathRemoteFile = '/CODE/FILE.jpg',
var pathRemoteFile = (process.env.AKAMAI_DIR || '/CODE') + '/FILE.jpg',
pathRemoteDir = path.join(path.dirname(pathRemoteFile), 'AkamaiHttpApi'),
pathRemoteFileMtime = new Date(),
config = {
keyName: 'keyName',
key: 'aLongString',
host: 'changeme.akamaihd.net',
keyName: process.env.AKAMAI_KEY_NAME || 'keyName',
key: process.env.AKAMAI_KEY || 'aLongString',
host: process.env.AKAMAI_HOST || 'changeme.akamaihd.net',
ssl: true,
verbose: false,
request: {timeout: 20000}
Expand Down Expand Up @@ -76,11 +76,12 @@ module.exports = {
'API functions': {
'du()': {
'Folder exists': function (done) {
akamai.du(path.dirname(pathRemoteFile), function (err, data) {
var fnc = akamai.du(path.dirname(pathRemoteFile), function (err, data) {
should.not.exist(err);
data.should.have.property('du');
data.du.should.have.properties(['du-info', 'attribs']);
data.du['du-info'][0].should.have.properties(['files', 'bytes']);
fnc.dir.should.be.a.Function;
done();
});
},
Expand Down Expand Up @@ -165,6 +166,13 @@ module.exports = {
},

'mtime()': {
'Incorrect argument': function (done) {
akamai.mtime(pathRemoteFile, (new Date()).toString(), function (err, data) {
err.should.be.instanceOf(TypeError);
should.not.exist(data);
done();
});
},
'File exists': function (done) {
akamai.mtime(pathRemoteFile, pathRemoteFileMtime, function (err, data) {
should.not.exist(err);
Expand Down

0 comments on commit de21e9e

Please sign in to comment.