@js4cytoscape/ndex-client 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@js4cytoscape/ndex-client",
3
+ "version": "0.3.2",
4
+ "description": "NDEx client library",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/cytoscape/js4cytoscape.git"
8
+ },
9
+ "bugs": {
10
+ "url": "https://github.com/cytoscape/js4cytoscape/issues"
11
+ },
12
+ "homepage": "https://github.com/cytoscape/js4cytoscape",
13
+ "main": "./dist/ndexClient.common.js",
14
+ "module": "src/index.js",
15
+ "files": [
16
+ "dist",
17
+ "src"
18
+ ],
19
+ "scripts": {
20
+ "watch:js": "webpack --watch",
21
+ "build": "webpack --mode=development",
22
+ "build-prod": "webpack --mode=production",
23
+ "test": "nyc mocha"
24
+ },
25
+ "author": "Jing Chen",
26
+ "license": "MIT",
27
+ "engines": {
28
+ "node": ">=12.16.1"
29
+ },
30
+ "dependencies": {
31
+ "axios": "^0.26.0"
32
+ }
33
+ }
package/src/CyNDEx.js ADDED
@@ -0,0 +1,174 @@
1
+ const CY_REST_BASE_URL = 'http://127.0.0.1';
2
+
3
+ const axios = require('axios');
4
+
5
+ class CyNDEx {
6
+
7
+ constructor(port = 1234) {
8
+ this._port = port;
9
+ }
10
+ get port() { return this._port; }
11
+
12
+ static get cyRestBaseURL() {
13
+ return CY_REST_BASE_URL;
14
+ }
15
+
16
+ setNDExServer(ndexServer) {
17
+ if (ndexServer !== undefined && ndexServer != null && ndexServer !== '') {
18
+ this._ndexServer = ndexServer;
19
+ }
20
+ }
21
+
22
+ getNDExServer() {
23
+ return this._ndexServer ? this._ndexServer : 'http://public.ndexbio.org';
24
+ }
25
+
26
+ setGoogleAuth(googleAuthObj) {
27
+ if (googleAuthObj !== undefined) {
28
+ this._googleAuth = googleAuthObj;
29
+ this._authType = 'g'; // valid values are 'g','b' or undefined
30
+ }
31
+ }
32
+
33
+ setAuthToken(authToken) {
34
+ if (authToken !== undefined ) {
35
+ this._authToken = authToken;
36
+ this._authType = 'g'; // valid values are 'g','b' or undefined
37
+ }
38
+ }
39
+
40
+ setBasicAuth(username, password) {
41
+ if (username !== undefined && username != null && username !== '') {
42
+ this._username = username;
43
+ this._password = password;
44
+ this._authType = 'b';
45
+ }
46
+ }
47
+
48
+ cyRestURL() {
49
+ return CY_REST_BASE_URL + ':' + this._port;
50
+ }
51
+
52
+ _getIdToken() {
53
+ return this._authToken ? this._authToken : this._googleAuth.getAuthInstance().currentUser.get().getAuthResponse().id_token;
54
+ }
55
+
56
+ _getAuthorizationFields() {
57
+ switch (this._authType) {
58
+ case 'b' : return {
59
+ username : this._username,
60
+ password : this._password
61
+ };
62
+ case 'g' : return {
63
+ idToken : this._getIdToken()
64
+ };
65
+ default : return {};
66
+ }
67
+ }
68
+
69
+ _httpGet(url) {
70
+
71
+ const baseURL = this.cyRestURL();
72
+
73
+ return new Promise(function (resolve, reject) {
74
+ axios({
75
+ method: 'get',
76
+ url: url,
77
+ baseURL: baseURL
78
+ }).then((response) => {
79
+ if (response.status === 200) {
80
+ return resolve(response.data);
81
+ }
82
+ return reject(response);
83
+ },
84
+ (response) => { return reject(response); }
85
+ );
86
+ });
87
+ }
88
+
89
+ _httpPut(url, parameters, data) {
90
+ return this._http('put', url, parameters, data);
91
+ }
92
+
93
+ _httpPost(url, parameters, data) {
94
+ return this._http('post', url, parameters, data);
95
+ }
96
+
97
+ _http(method, url, parameters, data) {
98
+
99
+ const baseURL = this.cyRestURL();
100
+
101
+ let config = {
102
+ method: method,
103
+ url: url,
104
+ baseURL: baseURL
105
+ };
106
+
107
+ if (parameters !== undefined) {
108
+ config['params'] = parameters;
109
+ }
110
+
111
+ config.data = data;
112
+ return new Promise(function (resolve, reject) {
113
+ axios(config).then((response) => {
114
+ if (response.status >= 200 && response.status <= 299) {
115
+ return resolve(response.data);
116
+ }
117
+ return reject(response);
118
+ },
119
+ (response) => {
120
+ return reject(response);
121
+ }
122
+ );
123
+ });
124
+
125
+ }
126
+
127
+ getCyNDExStatus() {
128
+ return this._httpGet('/cyndex2/v1');
129
+ }
130
+
131
+ getCytoscapeNetworkSummary(suid = 'current') {
132
+ return this._httpGet('/cyndex2/v1/networks/' + suid);
133
+ }
134
+
135
+ postNDExNetworkToCytoscape(uuid, accessKey, createView = undefined) {
136
+ const importParams = {
137
+ serverUrl: this.getNDExServer() + '/v2',
138
+ uuid: uuid,
139
+ accessKey: accessKey,
140
+ createView: createView
141
+ };
142
+
143
+ const authorizationFields = this._getAuthorizationFields();
144
+
145
+ return this._httpPost('/cyndex2/v1/networks', undefined, Object.assign(importParams, authorizationFields));
146
+ }
147
+
148
+ postCXNetworkToCytoscape(cx) {
149
+ return this._httpPost('/cyndex2/v1/networks/cx', undefined, cx);
150
+ }
151
+
152
+ postCytoscapeNetworkToNDEx(suid = 'current') {
153
+ const saveParams = {
154
+ serverUrl: this.getNDExServer() + '/v2',
155
+ };
156
+
157
+ const authorizationFields = this._getAuthorizationFields();
158
+
159
+ return this._httpPost('/cyndex2/v1/networks/' + suid, undefined, Object.assign(saveParams, authorizationFields));
160
+ }
161
+
162
+ putCytoscapeNetworkInNDEx(suid = 'current', uuid) {
163
+ const saveParams = {
164
+ serverUrl: this.getNDExServer() + '/v2',
165
+ uuid: uuid
166
+ };
167
+
168
+ const authorizationFields = this._getAuthorizationFields();
169
+
170
+ return this._httpPut('/cyndex2/v1/networks/' + suid, undefined, Object.assign(saveParams, authorizationFields));
171
+ }
172
+ }
173
+
174
+ module.exports = { CyNDEx };