-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzato_client.js
102 lines (91 loc) · 3.43 KB
/
zato_client.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
"use strict";
const request = require('request-promise-native');
const Base64 = require('js-base64').Base64;
/**
* A zato.service.invoke client that handles only JSON payloads and JSON responses.
*
* @param {string} url
* @param {string} username
* @param {string} password
*/
function ZatoClient(url, username, password) {
this._url = url;
this._username = username;
this._password = password;
this._request = request.defaults({
baseUrl: url,
url: this.IDE_DEPLOY_SERVICE_SUFFIX,
auth: {
user: username,
pass: password,
sendImmediately: true // Don't waste a roundtrip on HTTP 401.
},
json: true,
resolveWithFullResponse: true
});
}
ZatoClient.prototype = {
IDE_DEPLOY_SERVICE_SUFFIX: "/ide-deploy",
/**
* Arrange for a dummy request to be made through the admin.invoke mechanism.
*
* @param {function} onSuccess
* Callback invoked as onSuccess(msg) on success.
* @param {function} onFailure
* Callback invoked as onFailure(e) on failure. The passed
* argument may be used as an internal diagnostic only.
*/
ping: function(onSuccess, onFailure) {
(this._request({})
.then(this._onPingResponse.bind(this, onSuccess, onFailure))
.catch(onFailure));
},
_onPingResponse: function(onSuccess, onFailure, response) {
if(response.statusCode != 200) {
console.log("_onPingResponse: status!=200: %o", response);
onFailure('Cluster returned HTTP status ' + response.statusCode);
} else if(! response.body.zato_ide_deploy_create_response.success) {
console.log("_onPingResponse: success=false: %o", response.body);
onFailure('Cluster indicated failure: ' + response.body.zato_ide_deploy_create_response.msg);
} else {
onSuccess(response.body.zato_ide_deploy_create_response.msg);
}
},
/**
* Arrange for a source file to be hot-deployed to the cluster.
*
* @param {string} filename
* File name to deploy.
* @param {string} data
* File contents.
* @param {function} onSuccess
* Callback invoked as onSuccess(msg) on success.
* @param {function} onFailure
* Callback invoked as onFailure(e) on failure. The passed
* argument may be used as an internal diagnostic only.
*/
deploy: function(filename, data, onSuccess, onFailure)
{
var json = {
payload_name: filename,
payload: Base64.encode(data)
};
(this._request({json: json})
.then(this._onDeployResponse.bind(this, onSuccess, onFailure))
.catch(onFailure));
},
_onDeployResponse: function(onSuccess, onFailure, response)
{
console.log("_onDeployResponse %o", response);
if(response.statusCode != 200) {
console.log("_onDeployResponse: status!=200: %o", response);
onFailure('Cluster returned HTTP status ' + response.statusCode);
} else if(! response.body.zato_ide_deploy_create_response.success) {
console.log("_onPingResponse: success=false: %o", response.body);
onFailure('Cluster indicated failure: ' + response.body.zato_ide_deploy_create_response.msg);
} else {
onSuccess(response.body.zato_ide_deploy_create_response.msg);
}
}
};
module.exports = ZatoClient;