forked from igaln/graphcommons-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (86 loc) · 2.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var unirest = require('unirest');
var graph = require('./graph.js')
var api_url = 'https://graphcommons.com/api/v1/';
var api_key = '';
// Constructor
// Can take a callback to eval when api is ready
var GraphCommons = function(_apikey, _callback) {
if(!_apikey) throw new Error('no api key provided');
api_key = _apikey;
unirest.get(api_url + 'status')
.headers({'Authentication': api_key})
.end(function (response) {
if(_callback) _callback(response.body);
});
}
// Create a new graph from api
// Returns a graph object
GraphCommons.prototype.new_graph = function(_graph, _callback) {
if(!_graph) throw new Error('missing graph data');
unirest.post(api_url + 'graphs')
.headers({'Authentication': api_key})
.headers({'Content-Type' : 'application/json'})
.send(_graph)
.end(function (response) {
if(_callback) _callback(new graph(response.body.graph));
});
}
// Retrieve a graph from api
// Returns a graph object
GraphCommons.prototype.graphs = function(_id, _callback) {
if(!_id) throw new Error('missing id for the graph');
unirest.get(api_url + 'graphs/' + _id)
.headers({'Authentication': api_key})
.end(function (response) {
if(_callback) _callback(new graph(response.body.graph));
});
}
// Retrieve particular node and all of its properties
// Returns the node object
GraphCommons.prototype.nodes = function(_id, _callback) {
if(!_id) throw new Error('missing id for the node');
unirest.get(api_url + 'nodes/' + _id)
.headers({'Authentication': api_key})
.end(function (response) {
if(_callback) _callback(response.body.node);
});
}
// Search nodes in graphs
// Returns the search results in nodes
GraphCommons.prototype.nodes_search = function(_search_obj, _callback) {
if(!_search_obj) throw new Error('missing search query for the node search');
unirest.get(api_url + 'nodes/search')
.headers({'Authentication': api_key})
.headers({'Content-Type' : 'application/json'})
.send(_search_obj)
.end(function (response) {
if(_callback) _callback(response.body);
});
}
// Search nodes in graphs
// Returns the search results in nodes
GraphCommons.prototype.search = function(_search_obj, _callback) {
if(!_search_obj) throw new Error('missing search query for the node search');
unirest.get(api_url + 'search')
.headers({'Authentication': api_key})
.headers({'Content-Type' : 'application/json'})
.send(_search_obj)
.end(function (response) {
if(_callback) _callback(response.body);
});
}
// Update graph with signals
GraphCommons.prototype.update_graph = function(_id, _signals, _callback) {
sendSignal(_id, _signals, _callback);
}
//Utility
var sendSignal = function(_id,_signals,_callback) {
unirest.put(api_url + 'graphs/' + _id + '/add')
.headers({'Authentication': api_key})
.headers({'Content-Type' : 'application/json'})
.send(_signals)
.end(function (response) {
if(_callback) _callback(response.body);
});
}
module.exports = GraphCommons;