parent
23ca544247
commit
f1de8a69f9
@ -1,4 +0,0 @@ |
|||||||
|
|
||||||
export class Client { |
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,63 @@ |
|||||||
|
export enum Method { |
||||||
|
GET = 'GET', |
||||||
|
POST = 'POST', |
||||||
|
PUT = 'PUT', |
||||||
|
PATCH = 'PATCH', |
||||||
|
} |
||||||
|
|
||||||
|
export let hasQuery = (method: Method) => { |
||||||
|
return Method.GET === method |
||||||
|
} |
||||||
|
|
||||||
|
export interface ClientOptions { |
||||||
|
baseUrl: string |
||||||
|
} |
||||||
|
|
||||||
|
export interface SchemaInterface { |
||||||
|
url: string, |
||||||
|
method: Method |
||||||
|
contentType: string | null |
||||||
|
} |
||||||
|
|
||||||
|
export class SchemaClient { |
||||||
|
baseUrl: string |
||||||
|
|
||||||
|
constructor({baseUrl}: ClientOptions) { |
||||||
|
this.baseUrl = baseUrl |
||||||
|
} |
||||||
|
|
||||||
|
async send(schema: SchemaInterface, data: any) { |
||||||
|
let url = `${this.baseUrl}${schema.url}` |
||||||
|
let {url: preparedUrl, data: preparedData} = this.processAttributes(url, data) |
||||||
|
|
||||||
|
if (hasQuery(schema.method)) { |
||||||
|
preparedUrl += '?' + new URLSearchParams(preparedData) |
||||||
|
} |
||||||
|
|
||||||
|
let response = await fetch(preparedUrl, { |
||||||
|
method: schema.method.toString(), |
||||||
|
headers: { |
||||||
|
...(schema.contentType ? {'Content-Type': schema.contentType} : {}), |
||||||
|
'X-Plugin-Token': 'passw0rd' |
||||||
|
}, |
||||||
|
body: hasQuery(schema.method) ? null : JSON.stringify(preparedData) |
||||||
|
}); |
||||||
|
|
||||||
|
let responseData = response.headers.get('Content-Type')?.toString().includes('application/json') |
||||||
|
? await response.json() |
||||||
|
: await response.text() |
||||||
|
return {responseData: responseData, headers: response.headers} |
||||||
|
} |
||||||
|
|
||||||
|
private processAttributes(url: string, data: any) |
||||||
|
{ |
||||||
|
const preparedData = data |
||||||
|
for (const key in data) { |
||||||
|
if (url.indexOf('{' + key + '}') > -1) { |
||||||
|
url = url.replace('{' + key + '}', preparedData[key]) |
||||||
|
delete preparedData[key] |
||||||
|
} |
||||||
|
} |
||||||
|
return {url, data: preparedData } |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
export enum ProcessesType |
||||||
|
{ |
||||||
|
RUNNING = 'running', |
||||||
|
HISTORY = 'history', |
||||||
|
} |
||||||
|
|
||||||
|
export class GetProcessesRequest { |
||||||
|
type?: ProcessesType |
||||||
|
} |
@ -0,0 +1,4 @@ |
|||||||
|
export class PaginationRequest { |
||||||
|
page?: number |
||||||
|
limit?: number |
||||||
|
} |
@ -0,0 +1,28 @@ |
|||||||
|
export interface OptionInterface { |
||||||
|
name: string, |
||||||
|
description: string | null, |
||||||
|
default: any, |
||||||
|
value: any, |
||||||
|
shortcut: string | null, |
||||||
|
isArray: boolean, |
||||||
|
isNegatable: boolean, |
||||||
|
isValueOptional: boolean, |
||||||
|
isValueRequired: boolean |
||||||
|
acceptValue: boolean |
||||||
|
} |
||||||
|
|
||||||
|
export interface ArgumentInterface { |
||||||
|
name: string, |
||||||
|
description: string|null, |
||||||
|
default: any, |
||||||
|
isArray: boolean, |
||||||
|
isRequired: boolean, |
||||||
|
} |
||||||
|
|
||||||
|
export interface CommandResponseInterface { |
||||||
|
class: string, |
||||||
|
name: string, |
||||||
|
description: string, |
||||||
|
options: OptionInterface[], |
||||||
|
arguments: ArgumentInterface[], |
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
export interface ProcessExceptionInterface { |
||||||
|
message: string, |
||||||
|
file: string, |
||||||
|
line: number, |
||||||
|
code: number, |
||||||
|
trace: any[], |
||||||
|
} |
||||||
|
|
||||||
|
export interface ProcessProgressInterface { |
||||||
|
total: number, |
||||||
|
progress: number, |
||||||
|
memory: any[], |
||||||
|
} |
||||||
|
|
||||||
|
export interface ProcessesResponseInterface { |
||||||
|
id: string |
||||||
|
lock: string | null |
||||||
|
containerUuid: string | null |
||||||
|
pid: bigint | null |
||||||
|
name: string |
||||||
|
options: Record<string, any> |
||||||
|
arguments: Record<string, any> |
||||||
|
exception: ProcessExceptionInterface | null |
||||||
|
progress: ProcessProgressInterface | null |
||||||
|
outputId: string | null |
||||||
|
createdAt: string |
||||||
|
updatedAt: string | null |
||||||
|
startedAt: string | null |
||||||
|
pausedAt: string | null |
||||||
|
cancelledAt: string | null |
||||||
|
completedAt: string | null |
||||||
|
} |
@ -0,0 +1,4 @@ |
|||||||
|
export interface ResponseInterface { |
||||||
|
data: any |
||||||
|
headers: Headers |
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
import {Method, SchemaInterface} from "../../schema-client"; |
||||||
|
|
||||||
|
class CommandsSchema implements SchemaInterface { |
||||||
|
method = Method.GET |
||||||
|
url = '/system-monitoring/commands' |
||||||
|
contentType = null; |
||||||
|
} |
||||||
|
|
||||||
|
export let commandsSchema = new CommandsSchema() |
||||||
|
export default commandsSchema |
@ -0,0 +1,10 @@ |
|||||||
|
import {Method, SchemaInterface} from "../../schema-client"; |
||||||
|
|
||||||
|
class ProcessesSchema implements SchemaInterface { |
||||||
|
method = Method.GET |
||||||
|
url = '/system-monitoring/processes' |
||||||
|
contentType = null; |
||||||
|
} |
||||||
|
|
||||||
|
export let processesSchema = new ProcessesSchema() |
||||||
|
export default processesSchema |
@ -0,0 +1,10 @@ |
|||||||
|
import {Method, SchemaInterface} from "../../schema-client"; |
||||||
|
|
||||||
|
class RunCommandsSchema implements SchemaInterface { |
||||||
|
method = Method.POST |
||||||
|
url = '/system-monitoring/commands/{commandName}/run' |
||||||
|
contentType = 'application/json;charset=utf-8'; |
||||||
|
} |
||||||
|
|
||||||
|
export let runCommandsSchema = new RunCommandsSchema() |
||||||
|
export default runCommandsSchema |
@ -0,0 +1,46 @@ |
|||||||
|
import {processesSchema} from "./schemas/processes"; |
||||||
|
import {ProcessesResponseInterface} from "./responses/processes"; |
||||||
|
import {SchemaClient} from "../schema-client"; |
||||||
|
import {GetProcessesRequest} from "./requests/get-processes"; |
||||||
|
import {PaginationRequest} from "./requests/pagination"; |
||||||
|
import {ResponseInterface} from "./responses/response"; |
||||||
|
import {CommandResponseInterface} from "./responses/comamnds"; |
||||||
|
import commandsSchema from "./schemas/commands"; |
||||||
|
import runCommandsSchema from "./schemas/run-commands"; |
||||||
|
|
||||||
|
export class SMClient { |
||||||
|
schemaClient: SchemaClient |
||||||
|
|
||||||
|
constructor() { |
||||||
|
this.schemaClient = new SchemaClient({ |
||||||
|
baseUrl: 'http://fmw.sipachev.sv' |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
async getCommands(): Promise<ResponseInterface> { |
||||||
|
let { responseData, headers } = await this.schemaClient.send(commandsSchema, {}) |
||||||
|
return { |
||||||
|
data: responseData as CommandResponseInterface[], |
||||||
|
headers: headers |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
async runCommand(data: any): Promise<ResponseInterface> { |
||||||
|
let { headers } = await this.schemaClient.send(runCommandsSchema, data) |
||||||
|
return { |
||||||
|
data: null, |
||||||
|
headers: headers |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
async getProcesses(data: GetProcessesRequest | PaginationRequest): Promise<ResponseInterface> { |
||||||
|
let { responseData, headers } = await this.schemaClient.send(processesSchema, data) |
||||||
|
return { |
||||||
|
data: responseData as ProcessesResponseInterface[], |
||||||
|
headers: headers |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export let smClient = new SMClient |
||||||
|
export default smClient |
Loading…
Reference in new issue