QLTools.js 10 KB

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