"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var cm_tools_1 = require("../refs/cm-tools");
var mslib_1 = require("mslib");
/**
 * Kong管理API
 * @date 2021-06-16 09:29:17
 * @author 吴锋霖
 */
var AdminApi = /** @class */ (function () {
    function AdminApi(apiUrl) {
        this.apiUrl = apiUrl;
        if (this.apiUrl.endsWith('/')) {
            this.apiUrl = this.apiUrl.substring(0, this.apiUrl.length - 1);
        }
        this.httpClient = new cm_tools_1.HttpClient();
    }
    AdminApi.prototype.addService = function (kongService) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.addKongObj(this.apiUrl + "/services", kongService)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.addRoute = function (kongRoute) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.addKongObj(this.apiUrl + "/routes", kongRoute)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.updateRoute = function (kongRoute) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.updateKongObj(this.apiUrl + "/routes/" + kongRoute.id, kongRoute)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.addUpstream = function (kongUpstream) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.addKongObj(this.apiUrl + "/upstreams", kongUpstream)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.addTarget = function (kongTarget) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.addKongObj(this.apiUrl + "/upstreams/" + kongTarget.upstream.id + "/targets", kongTarget)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.addPlugin = function (kongPlugin) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.addKongObj(this.apiUrl + "/plugins/", kongPlugin)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.getService = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.getKongObj(this.apiUrl + "/services/" + id)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.getRoute = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.getKongObj(this.apiUrl + "/routes/" + id)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.getUpstream = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.getKongObj(this.apiUrl + "/upstreams/" + id)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.listServices = function (tags) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.listKongObjs(this.apiUrl + "/services?tags=" + tags)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.listRoutes = function (tags) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.listKongObjs(this.apiUrl + "/routes?tags=" + tags)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.listUpstreams = function (tags) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.listKongObjs(this.apiUrl + "/upstreams?tags=" + tags)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.listTargets = function (upstream) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.listKongObjs(this.apiUrl + "/upstreams/" + upstream + "/targets")];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.listServicePlugins = function (serviceId) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.listKongObjs(this.apiUrl + "/services/" + serviceId + "/plugins")];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.listRoutePlugins = function (routeId) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.listKongObjs(this.apiUrl + "/routes/" + routeId + "/plugins")];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.deleteService = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.deleteKongObj(this.apiUrl + "/services/" + id)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.deleteRoute = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.deleteKongObj(this.apiUrl + "/routes/" + id)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.deleteUpstream = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.deleteKongObj(this.apiUrl + "/upstreams/" + id)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.deleteTarget = function (upstream, targetId) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.deleteKongObj(this.apiUrl + "/upstreams/" + upstream + "/targets/" + targetId)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.removeTarget = function (upstream, host, post) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.deleteTarget(upstream, host + ":" + post)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    AdminApi.prototype.listKongObjs = function (url) {
        return __awaiter(this, void 0, void 0, function () {
            var response, e_1;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 2, , 3]);
                        return [4 /*yield*/, this.httpClient.get(url)];
                    case 1:
                        response = _a.sent();
                        if (!response) {
                            return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5931\u8D25 " + url)];
                        }
                        if (response.status !== 200) {
                            return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5931\u8D25 " + url + "/" + response.status + "/" + response.statusText)];
                        }
                        return [2 /*return*/, mslib_1.OpResult.success('OK', response.data.data)];
                    case 2:
                        e_1 = _a.sent();
                        return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5F02\u5E38 " + url + " ,error:" + e_1.message)];
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    AdminApi.prototype.getKongObj = function (url) {
        return __awaiter(this, void 0, void 0, function () {
            var response, e_2;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 2, , 3]);
                        return [4 /*yield*/, this.httpClient.get(url)];
                    case 1:
                        response = _a.sent();
                        if (!response) {
                            return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5931\u8D25 " + url)];
                        }
                        if (response.status !== 200) {
                            return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5931\u8D25 " + url + "/" + response.status + "/" + response.statusText)];
                        }
                        return [2 /*return*/, mslib_1.OpResult.success('OK', response.data)];
                    case 2:
                        e_2 = _a.sent();
                        return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5F02\u5E38 " + url + " ,error:" + e_2.message)];
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    AdminApi.prototype.deleteKongObj = function (url) {
        return __awaiter(this, void 0, void 0, function () {
            var response, e_3;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 2, , 3]);
                        return [4 /*yield*/, this.httpClient.request({ url: url, method: 'DELETE' })];
                    case 1:
                        response = _a.sent();
                        if (!response) {
                            return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5931\u8D25 " + url)];
                        }
                        if (response.status !== 204) {
                            return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5931\u8D25 " + url + "/" + response.status + "/" + response.statusText)];
                        }
                        return [2 /*return*/, mslib_1.OpResult.success('OK')];
                    case 2:
                        e_3 = _a.sent();
                        return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5F02\u5E38 " + url + " ,error:" + e_3.message)];
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    AdminApi.prototype.addKongObj = function (url, obj) {
        return __awaiter(this, void 0, void 0, function () {
            var response, e_4;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 2, , 3]);
                        return [4 /*yield*/, this.httpClient.postJson(url, {}, obj)];
                    case 1:
                        response = _a.sent();
                        if (!response) {
                            return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5931\u8D25 " + url)];
                        }
                        if (response.status !== 201) {
                            return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5931\u8D25 " + url + "/" + response.status + "/" + response.statusText)];
                        }
                        return [2 /*return*/, mslib_1.OpResult.success('OK', response.data)];
                    case 2:
                        e_4 = _a.sent();
                        mslib_1.logger.error("\u8C03\u7528KongApi\u5F02\u5E38 " + url, e_4);
                        return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5F02\u5E38 " + url + " ,error:" + e_4.message)];
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    AdminApi.prototype.updateKongObj = function (url, obj) {
        return __awaiter(this, void 0, void 0, function () {
            var response, e_5;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 2, , 3]);
                        return [4 /*yield*/, this.httpClient.patchJson(url, {}, obj)];
                    case 1:
                        response = _a.sent();
                        if (!response) {
                            return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5931\u8D25 " + url)];
                        }
                        if (response.status !== 201) {
                            return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5931\u8D25 " + url + "/" + response.status + "/" + response.statusText)];
                        }
                        return [2 /*return*/, mslib_1.OpResult.success('OK', response.data)];
                    case 2:
                        e_5 = _a.sent();
                        mslib_1.logger.error("\u8C03\u7528KongApi\u5F02\u5E38 " + url, e_5);
                        return [2 /*return*/, mslib_1.OpResult.error("\u8C03\u7528KongApi\u5F02\u5E38 " + url + " ,error:" + e_5.message)];
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    return AdminApi;
}());
exports.AdminApi = AdminApi;
//# sourceMappingURL=AdminApi.js.map