Commit b7498ef8 authored by Mihkel Putrinš's avatar Mihkel Putrinš

cleanup

parent 65ff3ec2
# Logs
logs
*.log
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# C extensions
*.so
# Runtime data
pids
*.pid
*.seed
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Coverage directory used by tools like istanbul
coverage
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Translations
*.mo
*.pot
# Django stuff:
*.log
# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules
# Sphinx documentation
docs/_build/
# Users Environment Variables
.lock-wscript
\ No newline at end of file
# PyBuilder
target/
\ No newline at end of file
var https = require('https')
var crypto = require('crypto')
var querystring = require('querystring')
var fs = require('fs')
var EntuLib = function EntuLib(entu_user_id, entu_user_key, entu_url) {
var POLICY_EXPIRATION_MINUTES = 15
var API_VERSION = '/api2/'
var returned_data = false
//
// Possible values for entu_query =
// Fetch entity by id | {}
// Create entity | { 'definition': entitydefinition, ('entitydefinition-propertydefinition':value) }
// Search and fetch entities | { 'definition': entitydefinition, 'query': query, 'limit': limit }
// PUT properties to entity | { ('entitydefinition-propertydefinition':value) }
//
var __create_policy = function __create_policy(entu_query) {
var conditions = []
var entu_query = entu_query === undefined ? {} : entu_query
var conditions = Object.keys(entu_query).map(function(v) { return entu_query[v] })
var expiration_time = new Date()
expiration_time.setMinutes(expiration_time.getMinutes() + POLICY_EXPIRATION_MINUTES)
var policy = { 'expiration': expiration_time.toISOString(), 'conditions': conditions }
policy = JSON.stringify(policy)
encoded_policy = new Buffer(new Buffer(policy).toString('utf8')).toString('base64')
var signature = crypto.createHmac('sha1', entu_user_key).update(encoded_policy).digest().toString('base64')
entu_query.policy = encoded_policy
entu_query.user = entu_user_id
entu_query.signature = signature
return querystring.stringify(entu_query)
}
var __submit_it = function __submit_it(callback, path, method, data) {
var options = {
hostname: entu_url,
path: path,
port: 443,
method: method
}
if (data !== undefined) {
options.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
}
var buffer = ''
var request = https.request(options)
request.on('response', function response_handler( response ) {
response.on('data', function chunk_sticher(chunk) {
buffer += chunk
})
response.on('end', function response_emitter() {
var str = buffer.toString()
var returned_data = JSON.parse(str)
callback(returned_data)
})
})
if (data !== undefined) {
request.write(data)
}
request.end()
}
return {
getEntity: function (callback, entity_id) {
var data = __create_policy()
var path = API_VERSION + 'entity-' + entity_id + '?' + data
__submit_it(callback, path, 'GET')
},
// definition = property's dataproperty name
findEntity: function (callback, definition, query, limit) {
var entu_query = {
'definition': definition,
'query': query,
'limit': limit
}
var data = __create_policy(entu_query)
var path = API_VERSION + 'entity?' + data
__submit_it(callback, path, 'GET')
},
// definition = property's dataproperty name
createEntity: function (callback, parent_id, definition, properties) {
var entu_query = {}
entu_query.definition = definition
for (var key in properties) {
entu_query[definition + '-' + key] = properties[key]
}
var data = __create_policy(entu_query)
var path = API_VERSION + 'entity-' + parent_id
__submit_it(callback, path, 'POST', data)
},
// definition = property's dataproperty name
addProperties: function (callback, entity_id, definition, properties) {
var entu_query = {}
for (var key in properties) {
entu_query[definition + '-' + key] = properties[key]
}
var data = __create_policy(entu_query)
var path = API_VERSION + 'entity-' + entity_id
__submit_it(callback, path, 'PUT', data)
},
// property_definition in form of entity_keyname + "-" + property_keyname
// as for entity with definition "person" and property with definition "photo"
// property_definition = "person-photo"
addFile: function (callback, entity_id, property_definition, abspath) {
if (!fs.existsSync(abspath))
callback({'Error':'No such file','Path':abspath})
var entu_query = {
'filename': abspath,
'entity': entity_id,
'property': property_definition
}
var data = __create_policy(entu_query)
var path = API_VERSION + 'file?' + data
file_contents = fs.readFileSync(abspath)
__submit_it(callback, path, 'POST', file_contents)
}
}
}
module.exports = EntuLib
// Sample usage
var print_result = function print_result(data) {
console.log(stringifier(data))
}
var stringifier = function stringifier(o) {
var cache = [];
return JSON.stringify(o, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, replace key
return 'Circular reference to: ' + key
}
// Store value in our collection
cache.push(value)
}
return value
}, '\t')
}
var entu_user_id = 1001
var entu_user_key = 'Write your Entu key here'
var entu_url = 'yourdomain.entu.ee'
var EntuLib = new EntuLib(entu_user_id, entu_user_key, entu_url)
// EntuLib.getEntity(print_result, 684)
// EntuLib.findEntity(print_result, 'person', 'test', 10)
// EntuLib.createEntity(print_result, 610, 'person', {'forename':'test3','surname':'test3'})
// EntuLib.addProperties(print_result, 684, 'person', {'email':'foo@bar','user':'zaza@google'})
// EntuLib.addFile(print_result, 684, 'person-photo', '/Users/michelek/Dropbox/Screenshots/penrose_bw.png')
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