UNPKG

1.95 kBJavaScriptView Raw
1"use strict"
2
3const request = require("request")
4
5class MessageBusClient {
6 constructor(logger, options={}) {
7 let {uri, token} = options
8
9 this.logger = logger
10 this.baseUri = `${uri}/api/v2/management`
11 this.requestOptions = {
12 json: true,
13 headers: {
14 "Content-Type": "application/json;charset=utf-8",
15 "Authorization": `Token ${token}`
16 }
17 }
18 }
19
20 //"/controllers/:uuid/execAsync/:command*"
21 async executeAsync(controllerUuid, commandName, body={}) {
22 return this.performRequest({
23 uri: `${this.baseUri}/controllers/${controllerUuid}/execAsync/${commandName}`,
24 method: "POST",
25 body
26 })
27 }
28
29 //"/controllers/:uuid/execSync/:command*"
30 async executeSync(controllerUuid, commandName, body={}) {
31 return this.performRequest({
32 uri: `${this.baseUri}/controllers/${controllerUuid}/execSync/${commandName}`,
33 method: "POST",
34 body
35 })
36 }
37
38 //"/controllers/:uuid/status"
39 async getControllerStatus(controllerUuid) {
40 return this.performRequest({
41 uri: `${this.baseUri}/controllers/${controllerUuid}/status`,
42 method: "GET"
43 })
44 }
45
46 //"/controllers/:uuid/status"
47 async updateApplication(controllerUuid, applicationUuid) {
48 return this.performRequest({
49 uri: `${this.baseUri}/controllers/${controllerUuid}`,
50 method: "POST",
51 body: {ApplicationUuid: applicationUuid}
52 })
53 }
54
55
56 async performRequest(requestOptions) {
57 let opts = Object.assign({}, this.requestOptions, requestOptions)
58 return new Promise((resolve, reject) => {
59 request(opts, (error, response, body) => {
60 if(error) return reject(error)
61 if(response.statusCode >= 400) return reject(new MessageBusError(response.statusMessage))
62 return resolve(body)
63 })
64 })
65 }
66}
67
68class MessageBusError extends Error {}
69
70MessageBusClient.MessageBusError = MessageBusError
71module.exports = MessageBusClient