QLTools.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. function QLTools(url, clientId, clientSecret) {
  2. return new (class {
  3. constructor(url, clientId, clientSecret) {
  4. this.isEnableLog = true;
  5. this.logSeparator = '\n██'
  6. this.url = url;
  7. this.clientId = clientId;
  8. this.clientSecret = clientSecret;
  9. this.auth = null;
  10. }
  11. log(message) {
  12. if (this.isEnableLog) console.log(`${this.logSeparator}${message}`);
  13. }
  14. isSurge() {
  15. return typeof $httpClient != "undefined";
  16. }
  17. isQuanX() {
  18. return typeof $task != "undefined";
  19. }
  20. isLoon() {
  21. return typeof $loon != "undefined";
  22. }
  23. isJSBox() {
  24. return typeof $app != "undefined" && typeof $http != "undefined";
  25. }
  26. isStash() {
  27. return 'undefined' !== typeof $environment && $environment['stash-version'];
  28. }
  29. isNode() {
  30. return typeof require == "function" && !this.isJSBox();
  31. }
  32. adapterStatus(response) {
  33. if (response) {
  34. if (response.status) {
  35. response["statusCode"] = response.status
  36. } else if (response.statusCode) {
  37. response["status"] = response.statusCode
  38. }
  39. }
  40. return response
  41. }
  42. get(options, callback = () => { }) {
  43. if (this.isQuanX()) {
  44. if (typeof options == "string") options = {
  45. url: options
  46. }
  47. options["method"] = "GET"
  48. $task.fetch(options).then(response => {
  49. callback(null, this.adapterStatus(response), response.body)
  50. }, reason => callback(reason.error, null, null))
  51. }
  52. if (this.isSurge() || this.isLoon() || this.isStash()) $httpClient.get(options, (error, response, body) => {
  53. callback(error, this.adapterStatus(response), body)
  54. })
  55. if (this.isNode()) {
  56. this.node.request(options, (error, response, body) => {
  57. callback(error, this.adapterStatus(response), body)
  58. })
  59. }
  60. if (this.isJSBox()) {
  61. if (typeof options == "string") options = {
  62. url: options
  63. }
  64. options["header"] = options["headers"]
  65. options["handler"] = function (resp) {
  66. let error = resp.error
  67. if (error) error = JSON.stringify(resp.error)
  68. let body = resp.data
  69. if (typeof body == "object") body = JSON.stringify(resp.data)
  70. callback(error, this.adapterStatus(resp.response), body)
  71. }
  72. $http.get(options)
  73. }
  74. }
  75. post(options, callback = () => { }) {
  76. if (this.isQuanX()) {
  77. if (typeof options == "string") options = {
  78. url: options
  79. }
  80. options["method"] = "POST"
  81. $task.fetch(options).then(response => {
  82. callback(null, this.adapterStatus(response), response.body)
  83. }, reason => callback(reason.error, null, null))
  84. }
  85. if (this.isSurge() || this.isLoon() || this.isStash()) {
  86. $httpClient.post(options, (error, response, body) => {
  87. callback(error, this.adapterStatus(response), body)
  88. })
  89. }
  90. if (this.isNode()) {
  91. this.node.request.post(options, (error, response, body) => {
  92. callback(error, this.adapterStatus(response), body)
  93. })
  94. }
  95. if (this.isJSBox()) {
  96. if (typeof options == "string") options = {
  97. url: options
  98. }
  99. options["header"] = options["headers"]
  100. options["handler"] = function (resp) {
  101. let error = resp.error
  102. if (error) error = JSON.stringify(resp.error)
  103. let body = resp.data
  104. if (typeof body == "object") body = JSON.stringify(resp.data)
  105. callback(error, this.adapterStatus(resp.response), body)
  106. }
  107. $http.post(options)
  108. }
  109. }
  110. put(options, callback = () => { }) {
  111. if (this.isQuanX()) {
  112. // no test
  113. if (typeof options == "string") options = {
  114. url: options
  115. }
  116. options["method"] = "PUT"
  117. $task.fetch(options).then(response => {
  118. callback(null, this.adapterStatus(response), response.body)
  119. }, reason => callback(reason.error, null, null))
  120. }
  121. if (this.isSurge() || this.isLoon() || this.isStash()) {
  122. options.method = "PUT"
  123. $httpClient.put(options, (error, response, body) => {
  124. callback(error, this.adapterStatus(response), body)
  125. })
  126. }
  127. if (this.isNode()) {
  128. options.method = "PUT"
  129. this.node.request.put(options, (error, response, body) => {
  130. callback(error, this.adapterStatus(response), body)
  131. })
  132. }
  133. }
  134. delete(options, callback = () => { }) {
  135. if (this.isQuanX()) {
  136. // no test
  137. if (typeof options == "string") options = {
  138. url: options
  139. }
  140. options["method"] = "DELETE"
  141. $task.fetch(options).then(response => {
  142. callback(null, this.adapterStatus(response), response.body)
  143. }, reason => callback(reason.error, null, null))
  144. }
  145. if (this.isSurge() || this.isLoon() || this.isStash()) {
  146. options.method = "DELETE"
  147. $httpClient.put(options, (error, response, body) => {
  148. callback(error, this.adapterStatus(response), body)
  149. })
  150. }
  151. if (this.isNode()) {
  152. options.method = "DELETE"
  153. this.node.request.put(options, (error, response, body) => {
  154. callback(error, this.adapterStatus(response), body)
  155. })
  156. }
  157. }
  158. setAuth(auth){
  159. this.auth = auth;
  160. }
  161. async login(){
  162. let result = await this.getToken();
  163. if(!result){
  164. return false;
  165. }
  166. if(result.code == 200){
  167. let data = result.data;
  168. this.auth = `${data.token_type} ${data.token}`;
  169. return true;
  170. }
  171. return false;
  172. }
  173. async getToken(){
  174. let url = `${this.url}/open/auth/token?client_id=${this.clientId}&client_secret=${this.clientSecret}`;
  175. let options = {
  176. url: url,
  177. };
  178. return new Promise(resolve => {
  179. this.get(options, async (error, response, data) => {
  180. try {
  181. error && this.log(error);
  182. let result = JSON.parse(data);
  183. resolve(result);
  184. } catch (error) {
  185. this.log(error);
  186. } finally {
  187. resolve();
  188. }
  189. });
  190. });
  191. }
  192. async callApi(reqType, optName1, optName2=null , params=null){
  193. let url = `${this.url}/open/${optName1}`;
  194. if(optName2 && optName2.length > 0){
  195. url += `/${optName2}`;
  196. }
  197. let body = '';
  198. if(params){
  199. body = JSON.stringify(params);
  200. }
  201. let options = {
  202. url: url,
  203. headers: {
  204. 'Authorization': this.auth,
  205. 'Content-Type': `application/json`,
  206. 'Accept': `application/json`,
  207. },
  208. body: body,
  209. };
  210. return new Promise(resolve => {
  211. let func = this[reqType];
  212. if(!func){
  213. resolve();
  214. return;
  215. }
  216. func.call(this, options, async (error, response, data) => {
  217. try {
  218. let result = JSON.parse(data);
  219. resolve(result);
  220. } catch (error) {
  221. resolve(data);
  222. } finally {
  223. resolve();
  224. }
  225. });
  226. });
  227. }
  228. async getEnvs(){
  229. return this.callApi('get', 'envs');
  230. }
  231. async getEnvById(id){
  232. return this.callApi('get', 'envs', id);
  233. }
  234. async deleteEnvs(ids){
  235. return this.callApi('delete', 'envs', '', ids);
  236. }
  237. async addEnvs(envsList){
  238. return this.callApi('post', 'envs', '', envsList);
  239. }
  240. async updateEnv(envObj){
  241. return this.callApi('put', 'envs', '', envObj);
  242. }
  243. async getCrons(){
  244. return this.callApi('get', 'crons');
  245. }
  246. async getCronById(id){
  247. return this.callApi('get', 'crons', id);
  248. }
  249. async deleteCrons(ids){
  250. return this.callApi('delete', 'crons', '', ids);
  251. }
  252. async addCron(cronObj){
  253. return this.callApi('post', 'crons', '', cronObj);
  254. }
  255. async updateCron(cronObj){
  256. return this.callApi('put', 'crons', '', cronObj);
  257. }
  258. async runCrons(ids){
  259. return this.callApi('put', 'crons', 'run', ids);
  260. }
  261. async stopCrons(ids){
  262. return this.callApi('put', 'crons', 'stop', ids);
  263. }
  264. async enableCrons(ids){
  265. return this.callApi('put', 'crons', 'enable', ids);
  266. }
  267. async disableCrons(ids){
  268. return this.callApi('put', 'crons', 'disable', ids);
  269. }
  270. async getCronDetail(id){
  271. return this.callApi('get', 'crons', id);
  272. }
  273. async getCronLog(id){
  274. return this.callApi('get', 'crons', `${id}/log`);
  275. }
  276. })(url, clientId, clientSecret);
  277. }