-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathacquisition-sdk.js
More file actions
169 lines (168 loc) · 6.56 KB
/
acquisition-sdk.js
File metadata and controls
169 lines (168 loc) · 6.56 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var AcquisitionStatus = (function () {
function AcquisitionStatus() {
}
AcquisitionStatus.DeploymentSucceeded = "DeploymentSucceeded";
AcquisitionStatus.DeploymentFailed = "DeploymentFailed";
return AcquisitionStatus;
})();
exports.AcquisitionStatus = AcquisitionStatus;
var AcquisitionManager = (function () {
function AcquisitionManager(httpRequester, configuration) {
this._httpRequester = httpRequester;
this._serverUrl = configuration.serverUrl;
if (this._serverUrl.slice(-1) !== "/") {
this._serverUrl += "/";
}
this._appVersion = configuration.appVersion;
this._clientUniqueId = configuration.clientUniqueId;
this._deploymentKey = configuration.deploymentKey;
this._ignoreAppVersion = configuration.ignoreAppVersion;
}
AcquisitionManager.prototype.queryUpdateWithCurrentPackage = function (currentPackage, callback) {
var _this = this;
if (!currentPackage || !currentPackage.appVersion) {
throw new Error("Calling common acquisition SDK with incorrect package"); // Unexpected; indicates error in our implementation
}
var updateRequest = {
deploymentKey: this._deploymentKey,
appVersion: currentPackage.appVersion,
packageHash: currentPackage.packageHash,
isCompanion: this._ignoreAppVersion,
label: currentPackage.label,
clientUniqueId: this._clientUniqueId
};
var requestUrl = this._serverUrl + "updateCheck?" + queryStringify(updateRequest);
this._httpRequester.request(0 /* GET */, requestUrl, function (error, response) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode !== 200) {
callback(new Error(response.statusCode + ": " + response.body), null);
return;
}
try {
var responseObject = JSON.parse(response.body);
var updateInfo = responseObject.updateInfo;
}
catch (error) {
callback(error, null);
return;
}
if (!updateInfo) {
callback(error, null);
return;
}
else if (updateInfo.updateAppVersion) {
callback(null, { updateAppVersion: true, appVersion: updateInfo.appVersion });
return;
}
else if (!updateInfo.isAvailable) {
callback(null, null);
return;
}
var remotePackage = {
deploymentKey: _this._deploymentKey,
description: updateInfo.description,
label: updateInfo.label,
appVersion: updateInfo.appVersion,
isMandatory: updateInfo.isMandatory,
packageHash: updateInfo.packageHash,
packageSize: updateInfo.packageSize,
downloadUrl: updateInfo.downloadURL
};
callback(null, remotePackage);
});
};
AcquisitionManager.prototype.reportStatusDeploy = function (deployedPackage, status, previousLabelOrAppVersion, previousDeploymentKey, callback) {
var url = this._serverUrl + "reportStatus/deploy";
var body = {
appVersion: this._appVersion,
deploymentKey: this._deploymentKey
};
if (this._clientUniqueId) {
body.clientUniqueId = this._clientUniqueId;
}
if (deployedPackage) {
body.label = deployedPackage.label;
body.appVersion = deployedPackage.appVersion;
switch (status) {
case AcquisitionStatus.DeploymentSucceeded:
case AcquisitionStatus.DeploymentFailed:
body.status = status;
break;
default:
if (callback) {
if (!status) {
callback(new Error("Missing status argument."), null);
}
else {
callback(new Error("Unrecognized status \"" + status + "\"."), null);
}
}
return;
}
}
if (previousLabelOrAppVersion) {
body.previousLabelOrAppVersion = previousLabelOrAppVersion;
}
if (previousDeploymentKey) {
body.previousDeploymentKey = previousDeploymentKey;
}
callback = typeof arguments[arguments.length - 1] === "function" && arguments[arguments.length - 1];
this._httpRequester.request(2 /* POST */, url, JSON.stringify(body), function (error, response) {
if (callback) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode !== 200) {
callback(new Error(response.statusCode + ": " + response.body), null);
return;
}
callback(null, null);
}
});
};
AcquisitionManager.prototype.reportStatusDownload = function (downloadedPackage, callback) {
var url = this._serverUrl + "reportStatus/download";
var body = {
clientUniqueId: this._clientUniqueId,
deploymentKey: this._deploymentKey,
label: downloadedPackage.label
};
this._httpRequester.request(2 /* POST */, url, JSON.stringify(body), function (error, response) {
if (callback) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode !== 200) {
callback(new Error(response.statusCode + ": " + response.body), null);
return;
}
callback(null, null);
}
});
};
return AcquisitionManager;
})();
exports.AcquisitionManager = AcquisitionManager;
function queryStringify(object) {
var queryString = "";
var isFirst = true;
for (var property in object) {
if (object.hasOwnProperty(property)) {
var value = object[property];
if (!isFirst) {
queryString += "&";
}
queryString += encodeURIComponent(property) + "=";
if (value !== null && typeof value !== "undefined") {
queryString += encodeURIComponent(value);
}
isFirst = false;
}
}
return queryString;
}