QUANDER NODE SDK
Installation
npm install quander-node-sdk
Usage
To write an app using the SDK
- Register for a developer account and get your client_id and secret.
- Add dependency 'quander-node-sdk' in your package.json file.
- Require 'quander-node-sdk' in your file
var Quander = require('quander-node-sdk').Quander;
- Create config options, with parameters (mode, client_id, secret).
var quander = new Quander({
baseUrl: 'http://dev.quander.io/api',
tokenUrl: 'http://dev.quander.io',
clientId: '1a1t2u7b9540ggkk8s0gc4wcwcwwow40k4osw40cwo44swcoo0',
clientSecret: '1a1t2u7b9540ggkk8s0gc4wcwcwwow40k4osw40cwo44swcoo0'
});
- Login with username/password
quander.login(req.body.username, req.body.password).then((accessToken) => {
});
- If you already have your access token
quander.setAccessToken(user);
How to use the resources
var accountManager = quander.createResource('accounts');
var projectManager = quander.createResource('projects', {account: account});
accountManager.getList().then(function (accounts) {
res.send(accounts instanceof ResourceCollection);
res.send(accounts);
});
accountManager.getList({limit: 2, page: 1}).then(function (accounts) {
res.send(accounts instanceof ResourceCollection);
res.send(accounts);
});
- get operation ".get(uuid)"
accountManager.get('ccfb1d41-7c0b-4cb1-8836-606f0d8d5511').then(function (account) {
res.send(account);
});
- post operation ".post(payload, data)"
mediaManager.post({
posterurl: 'https://www.google.com/logos/doodles/2015/new-years-eve-2015-5985438795825152-hp2x.gif',
url: 'https://www.google.com/logos/doodles/2015/new-years-eve-2015-5985438795825152-hp2x.gif',
type: 'image',
referenceId: 'REFEREFEFEFEF',
metadata: ''
}).then((media) =>{
res.send(media);
});
mediaManager.post({
type: 'image',
referenceId: 'REFEREFEFEFEF',
metadata: ''
}, {
media: fs.createReadStream('/..../test.gif'),
poster: fs.createReadStream('/....p/test.gif')
}).then((media) =>{
res.send(media);
});
Available Resources and Operations
- Account => getList, get, post
- Project(account) => getList
- Experience(project) => getList
- Media(project) => getList
- Media(attendee) => getList
- Media(experience) => post
- Attendee(project) => post
Handling Error
There are 2 types of error, one coming from the sdk (QuanderSdkError), and the other one from the api responses (QuanderApiError).
accountManager.getList().then(function (accounts) {
res.send(accounts);
}).catch((error) => {
res.send(error instanceof Error);
res.send(error instanceof QuanderApiError);
});
- Example: Handling expired token
try {
quander.setAccessToken(token);
} catch (e) {
if (e instanceof QuanderSdkError && QuanderSdkError.TOKEN_EXPIRED === e.errorCode) {
return quander.getRefreshedToken(token).then((token) => {
quander.setAccessToken(token);
}).catch((e) => {
if (e instanceof QuanderApiError && QuanderApiError.REFRESH_TOKEN_INVALID === e.errorCode) {
res.redirect('/login');
}
});
}
}
Available Errors
Key | Description |
---|
BAD_RESOURCE | Resource not supported by the sdk |
BAD_OPERATION | Operation not supported by the sdk |
TOKEN_EXPIRED | Token is expired, you should request a new one through the api |
Key | Description |
---|
INVALID_GRANT | You can't authenticate with that method |
REFRESH_TOKEN_INVALID | Your refresh token is invalid, you have to authenticate again |
BAD_REQUEST | General error. The server cannot or will not process the request. |
FORBIDDEN | The request was a valid request, but the server is refusing to respond to it. |
NOT_FOUND | Resource could not be found but may be available in the future. |
CONFLICT | Indicates that the request could not be processed because of conflict in the request. |
Debugging
Quander.enableDebug();