Commit c8b1b8ed authored by Mihkel Putrinš's avatar Mihkel Putrinš
Browse files

get familiar with promise

parent c56c6131
# msXentu
Sync new resources between MetaShare and Entu
var async = require('async')
var path = require('path')
var debug = require('debug')('app:' + path.basename(__filename).replace('.js', ''))
var fs = require('fs')
var op = require('object-path')
var raven = require('raven')
var json2xml = require('json2xml')
APP_VERSION = require('./package').version
var ravenClient = new raven.Client(
'https://918a9d19b82f49db9c4925ff9a9f742a:ed9e8f217d0447d782dfe9305ef9f873@app.getsentry.com/61259',
{ 'release': APP_VERSION }
)
ravenClient.patchGlobal()
APP_ENTU_URL = "https://entu.keeleressursid.ee/api2"
APP_DEBUG = process.env.DEBUG
RESOURCE_ROOT = process.env.RESOURCE_ROOT
APP_ENTU_USER = process.env.API_USER
APP_ENTU_KEY = process.env.API_KEY
var entu = require('./code/entu')
debug(APP_VERSION)
entu.get_childs(RESOURCE_ROOT, 'resource')
.catch(function (reason) {
debug(JSON.stringify(reason, null, 4))
})
.then(function(entities) {
entities.forEach(function(entity) {
debug(JSON.stringify(entity, null, 4), json2xml(entity))
})
})
.catch(function (reason) {
debug('4')
debug(JSON.stringify(reason, null, 4))
})
var path = require('path')
var debug = require('debug')('app:' + path.basename(__filename).replace('.js', ''))
var request = require('request')
var rp = require('request-promise')
var async = require('async')
var op = require('object-path')
var md = require('marked')
var crypto = require('crypto')
LIMIT_PARALLEL = 3
function sign_data(data) {
data = data || {}
if (!APP_ENTU_USER || !APP_ENTU_KEY) { return data }
var conditions = []
for (k in data) {
if (data.hasOwnProperty(k)) {
conditions.push({k: data[k]})
}
}
var expiration = new Date()
expiration.setMinutes(expiration.getMinutes() + 10)
data.user = APP_ENTU_USER
var buff_str = JSON.stringify({expiration: expiration.toISOString(), conditions: conditions})
data.policy = new Buffer(buff_str).toString('base64')
data.signature = crypto.createHmac('sha1', APP_ENTU_KEY).update(data.policy).digest('base64')
return data
}
//Get entity from Entu
function get_entity(id) {
return new Promise(function (fulfill, reject) {
if (!id) { return reject(new Error('Missing "id"')) }
var headers = {}
var qs = {}
qs = sign_data()
var options = {url: APP_ENTU_URL + '/entity-' + id, headers: headers, qs: qs, strictSSL: true, json: true}
rp(options)
.catch(function (reason) { reject(reason) })
.then(function (body) {
var properties = op.get(body, 'result.properties', {})
var entity = {
id: op.get(body, 'result.id', null),
displayname: op.get(body, 'result.displayname', null),
displayinfo: op.get(body, 'result.displayinfo', null),
definition: op.get(body, 'result.definition.keyname', null),
picture: APP_ENTU_URL + '/entity-' + op.get(body, 'result.id', null) + '/picture',
right: op.get(body, 'result.right', null),
properties: {}
}
for (var p in properties) {
if (op.has(properties, [p, 'values'])) {
for (var v in op.get(properties, [p, 'values'])) {
if (op.get(properties, [p, 'datatype']) === 'file') {
op.push(entity, ['properties', p], {
id: op.get(properties, [p, 'values', v, 'id']),
value: op.get(properties, [p, 'values', v, 'value']),
file: APP_ENTU_URL + '/file-' + op.get(properties, [p, 'values', v, 'db_value'])
})
} else if (op.get(properties, [p, 'datatype']) === 'text') {
op.push(entity, ['properties', p], {
id: op.get(properties, [p, 'values', v, 'id']),
value: op.get(properties, [p, 'values', v, 'value']),
md: md(op.get(properties, [p, 'values', v, 'db_value']))
})
} else if (op.get(properties, [p, 'datatype']) === 'reference') {
op.push(entity, ['properties', p], {
id: op.get(properties, [p, 'values', v, 'id']),
value: op.get(properties, [p, 'values', v, 'value']),
reference: op.get(properties, [p, 'values', v, 'db_value'])
})
} else {
op.push(entity, ['properties', p], {
id: op.get(properties, [p, 'values', v, 'id']),
value: op.get(properties, [p, 'values', v, 'value']),
})
}
}
if (op.get(properties, [p, 'multiplicity']) === 1) { op.set(entity, ['properties', p], op.get(entity, ['properties', p, 0])) }
}
}
fulfill(entity)
})
})
}
// //Get entities by definition
// function get_entities(definition, limit, auth_id, auth_token, callback) {
// if (!definition) {
// callback(new Error('Missing "definition"'))
// return
// }
//
// var qs = {definition: definition}
// var headers = {}
// if (limit) {
// qs.limit = limit
// }
// if (auth_id && auth_token) {
// headers = {'X-Auth-UserId': auth_id, 'X-Auth-Token': auth_token}
// } else {
// qs = sign_data(qs)
// }
//
// var url = '/entity'
// var loop = 'result'
//
// // debug('get_entities: ' + APP_ENTU_URL + url)
// request.get({url: APP_ENTU_URL + url, headers: headers, qs: qs, strictSSL: true, json: true}, function (error, response, body) {
// if (error) {
// return callback(error)
// }
// if (response.statusCode !== 200 || !body.result) { return callback(new Error(op.get(body, 'error', body))) }
//
// var entities = []
// async.eachLimit(op.get(body, loop, []), LIMIT_PARALLEL, function (e, callback) {
// get_entity(e.id, auth_id, auth_token, function (error, op_entity) {
// if (error) {
// return callback(error)
// }
// entities.push(op_entity)
// callback()
// })
// }, function (error) {
// if (error) {
// return callback(error)
// }
// // debug(definition + ' returned ' + entities.length + ' entities.')
// callback(null, entities)
// })
// })
// }
//
//Get childs by parent entity id and optionally by definition
function get_childs(parent_entity_id, definition) {
return new Promise(function (fulfill, reject) {
if (!parent_entity_id) { return reject(new Error('Missing "parent_entity_id"')) }
var qs = {}
if (definition) { qs = {definition: definition} }
var headers = {}
qs = sign_data(qs)
var url = APP_ENTU_URL + '/entity-' + parent_entity_id + '/childs'
var options = {
url : url,
headers : headers,
qs : qs,
strictSSL : true,
json : true
}
rp(options)
.catch(function (reason) { reject(reason) })
.then(function (body) {
var childs = []
async.forEachOfSeries(op.get(body, ['result'], {}), function (definition_entities, definition, callback) {
async.eachSeries(op.get(definition_entities, ['entities'], []), function (entity, callback) {
get_entity(entity.id)
.catch(function (err) { return reject(err) })
.then(function (child_e) {
op.set(child_e, '_display', {name: child_e.name, info: child_e.info})
childs.push(child_e)
callback()
})
}, function (err) {
if (err) { return callback(err) }
callback()
})
}, function (err) {
if (err) { return reject(err) }
fulfill(childs)
})
})
})
}
//Edit entity
// params = {
// entity_id: entity_id,
// entity_definition: entity_definition,
// dataproperty: dataproperty,
// property_id: property_id,
// new_value: new_value
// }
// function edit(params, callback) {
// var body = {}
// var property = params.entity_definition + '-' + params.dataproperty
// if (op.get(params, ['property_id'], false)) {
// property = property + '.' + params.property_id
// }
// body[property] = op.get(params, 'new_value', '')
//
// var headers = {}
// var qb = sign_data(body)
//
// request.put({url: APP_ENTU_URL + '/entity-' + params.entity_id, headers: headers, body: qb, strictSSL: true, json: true, timeout: 60000}, function (error, response, body) {
// if(error) { return callback(error) }
// if(response.statusCode !== 201 || !body.result) { return callback(new Error(op.get(body, 'error', body))) }
//
// callback(null, op.get(body, 'result.properties.' + property + '.0', null))
// })
// }
//
// //Add entity
// function add(parent_entity_id, definition, properties, auth_id, auth_token, callback) {
// var data = {
// definition: definition
// }
//
// for (p in properties) {
// if (properties.hasOwnProperty(p)) {
// data[definition + '-' + p] = properties[p]
// }
// }
//
// var headers = {}
// var qb = data
// if (auth_id && auth_token) {
// headers = {'X-Auth-UserId': auth_id, 'X-Auth-Token': auth_token}
// } else {
// qb = sign_data(data)
// }
//
// var options = {
// url: APP_ENTU_URL + '/entity-' + parent_entity_id,
// headers: headers,
// body: qb,
// strictSSL: true,
// json: true
// }
// // debug(JSON.stringify(options, null, 4))
// request.post(options, function (error, response, body) {
// if (error) {
// debug('E:', error)
// callback(error)
// return
// }
// if (response.statusCode !== 201 || !body.result) {
// debug('E:' + response.statusCode, body)
// callback(new Error(op.get(body, 'error', body)))
// return
// }
// callback(null, op.get(body, 'result.id', null))
// })
// }
//
// //Share entity
// function rights(id, person_id, right, auth_id, auth_token, callback) {
// var body = {
// entity: person_id,
// right: right
// }
// var headers = {}
// var qb = body
// if(auth_id && auth_token) {
// headers = {'X-Auth-UserId': auth_id, 'X-Auth-Token': auth_token}
// } else {
// qb = sign_data(body)
// }
//
// request.post({url: APP_ENTU_URL + '/entity-' + id + '/rights', headers: headers, body: qb, strictSSL: true, json: true}, function (error, response, body) {
// if(error) { return callback(error) }
// if(response.statusCode !== 200) { return callback(new Error(op.get(body, 'error', body))) }
//
// callback(null, id)
// })
// }
exports.get_entity = get_entity
exports.get_childs = get_childs
// exports.get_entities = get_entities
// exports.edit = edit
// exports.add = add
// exports.rights = rights
{
"name": "msXentu",
"description": "Keep an eye and sync new resources between MetaShare and Entu.",
"version": "0.0.0",
"license": "MIT",
"private": true,
"single-instance": false,
"repository": {
"type": "git",
"url": "https://github.com/Keeleressursid/msXentu"
},
"dependencies": {
"async": "^1.5.0",
"debug": "^2.2.0",
"joi": "^7.0.1",
"json2xml": "^0.1.2",
"marked": "0.x",
"node-xml": "^1.0.2",
"nodeunit": "^0.9.1",
"object-path": "0.x",
"raven": "0.x",
"request-promise": "^1.0.2",
"xml2json": "^0.9.0"
},
"scripts": {
"start": "node app.js"
}
}
LIMIT_PARALLEL = 3
APP_ENTU_URL = "https://entu.keeleressursid.ee/api2"
RESOURCE_ROOT=4387
APP_ENTU_USER=3396
APP_ENTU_KEY="8D9FQNjnxdfwbNVEJrvCKCUN4n3kZm2Sp"
var entu = require('./code/entu')
var p = entu.get_childs(4387, 'resource')
var p = entu.get_entity(4387)
p.then(function(ope) {
console.log(ope.get())
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment