Created
April 12, 2015 11:20
-
-
Save fjenett/66d85dd9a4270649a792 to your computer and use it in GitHub Desktop.
Using Piecemeta API node.js client
This file contains 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
var PMApi = require('piecemeta-apiclient'); | |
var ls_apiKey, ls_access_token; | |
var authService = (function(){ | |
var auth = { | |
api_key : null, | |
access_token : null, | |
getCredentials : function () { | |
auth.api_key = ls_apiKey ? JSON.parse(ls_apiKey) : null; | |
auth.access_token = ls_access_token ? JSON.parse(ls_access_token) : null; | |
}, | |
setCredentials : function (api_key, access_token) { | |
ls_apiKey = JSON.stringify(api_key); | |
ls_access_token = JSON.stringify(access_token); | |
auth.getCredentials(); | |
}, | |
clearCredentials : function () { | |
ls_apiKey = null; | |
ls_access_token = null; | |
auth.getCredentials(); | |
} | |
}; | |
auth.getCredentials(); | |
return auth; | |
})(); | |
var apiClient; | |
// = PMApi({ | |
// host: host ? host : PIECEMETA_API_HOST, | |
// contentType: 'application/json', | |
// api_key: authService.api_key, | |
// access_token: authService.access_token | |
// }); | |
module.exports = { | |
init : function ( config ) { | |
config.contentType = 'application/json'; | |
config.api_key = authService.api_key; | |
config.access_token = authService.access_token; | |
apiClient = PMApi(config); | |
}, | |
client: apiClient, | |
actions: { | |
all: function (resourceName, callback, progress) { | |
apiClient.resource(resourceName).action('get', null, callback, progress); | |
}, | |
find: function (resourceName, uuid, callback, progress) { | |
apiClient.resource(resourceName).action('get', uuid, callback, progress); | |
}, | |
create: function (resourceName, data, callback, progress) { | |
apiClient.resource(resourceName).action('post', data, callback, progress); | |
}, | |
update: function (resourceName, uuid, data, callback, progress) { | |
data.uuid = uuid; | |
apiClient.resource(resourceName).action('put', data, callback, progress); | |
}, | |
remove: function (resourceName, uuid, callback, progress) { | |
apiClient.resource(resourceName).action('delete', uuid, callback, progress); | |
} | |
}, | |
getCredentials: function (access_token, callback) { | |
apiClient.setToken(access_token); | |
apiClient.getCredentials(function (err, credentials) { | |
if (err) { | |
return callback(err); | |
} | |
if (typeof credentials === 'object') { | |
authService.setCredentials(credentials, access_token); | |
callback(null); | |
} else { | |
callback(new Error('Failed to get credentials')); | |
} | |
}); | |
}, | |
authenticate: function (login, password, callback) { | |
apiClient.getToken({ email: login, password: password }, function (err, token) { | |
if (err) { | |
return callback(err); | |
} | |
if (typeof token === 'object') { | |
apiClient.getCredentials(function (err, credentials) { | |
if (err) { | |
return callback(err); | |
} | |
if (typeof credentials === 'object') { | |
authService.setCredentials(credentials, token); | |
callback(null); | |
} else { | |
callback(new Error('Failed to get credentials')); | |
} | |
}); | |
} else { | |
callback(new Error('Failed to get token')); | |
} | |
}); | |
}, | |
api_key : function(){ return ls_api_key; }, | |
token : function(){ return ls_access_token; } | |
} |
This file contains 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
var Piecemeta = require(__dirname+'/apiservice'); | |
var pmaHost = "http://192.168.56.101"; | |
var packageUUID = '8b646397-c53a-4c26-a061-fbc6fbf4743a' | |
Piecemeta.init({ | |
host: pmaHost | |
}); | |
Piecemeta.authenticate( '<---EMAIL-HERE--->', '<---PASSWORD--->', function (err) { | |
if (err) { console.log('ERROR!(1)',err); return; } | |
Piecemeta.actions.all('packages/'+packageUUID+'/channels', function(err,data){ | |
if (err) { console.log('ERROR!(2)',err); return; } | |
//console.log(data); | |
data.forEach(function(channel){ | |
console.log( channel ); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment