2016-09-01 02:24:47 +00:00
|
|
|
function isJSONArray(jsonString) {
|
2020-04-10 21:54:53 +00:00
|
|
|
return Object.prototype.toString.call(jsonString) === '[object Array]';
|
2016-09-01 02:24:47 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 23:26:02 +00:00
|
|
|
function isJSON(jsonString) {
|
|
|
|
try {
|
|
|
|
var o = JSON.parse(jsonString);
|
2017-05-23 18:56:10 +00:00
|
|
|
if (o && typeof o === 'object') {
|
2020-04-10 21:54:53 +00:00
|
|
|
return o;
|
2016-08-30 23:26:02 +00:00
|
|
|
}
|
2020-04-10 21:54:53 +00:00
|
|
|
} catch (e) {
|
2019-03-21 05:46:49 +00:00
|
|
|
//empty
|
|
|
|
}
|
2016-08-30 23:26:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-17 01:50:55 +00:00
|
|
|
// The Docker API often returns a list of JSON object.
|
2016-08-10 03:14:10 +00:00
|
|
|
// This handler wrap the JSON objects in an array.
|
2016-08-17 01:50:55 +00:00
|
|
|
// Used by the API in: Image push, Image create, Events query.
|
2019-03-21 05:46:49 +00:00
|
|
|
export function jsonObjectsToArrayHandler(data) {
|
2021-06-11 00:05:54 +00:00
|
|
|
// catching empty data helps the function not to fail and prevents unwanted error message to user.
|
|
|
|
if (!data) {
|
|
|
|
return [];
|
|
|
|
}
|
2017-05-23 18:56:10 +00:00
|
|
|
var str = '[' + data.replace(/\n/g, ' ').replace(/\}\s*\{/g, '}, {') + ']';
|
2016-08-17 00:27:54 +00:00
|
|
|
return angular.fromJson(str);
|
|
|
|
}
|
|
|
|
|
2016-09-02 03:25:20 +00:00
|
|
|
// The Docker API often returns an empty string or a valid JSON object on success (Docker 1.9 -> Docker 1.12).
|
2016-09-01 03:07:31 +00:00
|
|
|
// On error, it returns either an error message as a string (Docker < 1.12) or a JSON object with the field message
|
2016-09-02 03:25:20 +00:00
|
|
|
// container the error (Docker = 1.12)
|
|
|
|
// This handler ensure a valid JSON object is returned in any case.
|
|
|
|
// Used by the API in: container deletion, network deletion, network creation, volume creation,
|
|
|
|
// container exec, exec resize.
|
2019-03-21 05:46:49 +00:00
|
|
|
export function genericHandler(data) {
|
2016-09-01 03:07:31 +00:00
|
|
|
var response = {};
|
|
|
|
// No data is returned when deletion is successful (Docker 1.9 -> 1.12)
|
|
|
|
if (!data) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
// A string is returned on failure (Docker < 1.12)
|
|
|
|
else if (!isJSON(data)) {
|
|
|
|
response.message = data;
|
|
|
|
}
|
|
|
|
// Docker 1.12 returns a valid JSON object when an error occurs
|
|
|
|
else {
|
|
|
|
response = angular.fromJson(data);
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2018-02-28 06:19:28 +00:00
|
|
|
// The Docker API returns the logs as a single string.
|
2018-05-04 07:45:05 +00:00
|
|
|
// This handler wraps the data in a JSON object under the "logs" property.
|
2019-03-21 05:46:49 +00:00
|
|
|
export function logsHandler(data) {
|
2018-05-04 07:45:05 +00:00
|
|
|
return {
|
2020-04-10 21:54:53 +00:00
|
|
|
logs: data,
|
2018-05-04 07:45:05 +00:00
|
|
|
};
|
2018-02-28 06:19:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 02:24:47 +00:00
|
|
|
// Image delete API returns an array on success (Docker 1.9 -> Docker 1.12).
|
|
|
|
// On error, it returns either an error message as a string (Docker < 1.12) or a JSON object with the field message
|
|
|
|
// container the error (Docker = 1.12).
|
|
|
|
// This handler returns the original array on success or a newly created array containing
|
|
|
|
// only one JSON object with the field message filled with the error message on failure.
|
2019-03-21 05:46:49 +00:00
|
|
|
export function deleteImageHandler(data) {
|
2016-09-01 02:24:47 +00:00
|
|
|
// A string is returned on failure (Docker < 1.12)
|
2016-09-07 06:03:55 +00:00
|
|
|
var response = [];
|
2016-08-30 23:26:02 +00:00
|
|
|
if (!isJSON(data)) {
|
2020-04-10 21:54:53 +00:00
|
|
|
response.push({ message: data });
|
2016-09-01 02:24:47 +00:00
|
|
|
}
|
|
|
|
// A JSON object is returned on failure (Docker = 1.12)
|
2017-01-22 01:42:12 +00:00
|
|
|
else if (!isJSONArray(data)) {
|
2016-09-01 02:24:47 +00:00
|
|
|
var json = angular.fromJson(data);
|
|
|
|
response.push(json);
|
|
|
|
}
|
|
|
|
// An array is returned on success (Docker 1.9 -> 1.12)
|
|
|
|
else {
|
|
|
|
response = angular.fromJson(data);
|
2016-08-10 03:14:10 +00:00
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|