client.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import http from "k6/http";
  2. import encoding from 'k6/encoding';
  3. export const UIEndpoint = class UIEndpoint {
  4. constructor(httpClient) {
  5. this.httpClient = httpClient;
  6. }
  7. login(username, pwd) {
  8. const payload = { user: username, password: pwd };
  9. return this.httpClient.formPost('/login', payload);
  10. }
  11. }
  12. export const DatasourcesEndpoint = class DatasourcesEndpoint {
  13. constructor(httpClient) {
  14. this.httpClient = httpClient;
  15. }
  16. getById(id) {
  17. return this.httpClient.get(`/datasources/${id}`);
  18. }
  19. getByName(name) {
  20. return this.httpClient.get(`/datasources/name/${name}`);
  21. }
  22. create(payload) {
  23. return this.httpClient.post(`/datasources`, JSON.stringify(payload));
  24. }
  25. delete(id) {
  26. return this.httpClient.delete(`/datasources/${id}`);
  27. }
  28. }
  29. export const OrganizationsEndpoint = class OrganizationsEndpoint {
  30. constructor(httpClient) {
  31. this.httpClient = httpClient;
  32. }
  33. getById(id) {
  34. return this.httpClient.get(`/orgs/${id}`);
  35. }
  36. getByName(name) {
  37. return this.httpClient.get(`/orgs/name/${name}`);
  38. }
  39. create(name) {
  40. let payload = {
  41. name: name,
  42. };
  43. return this.httpClient.post(`/orgs`, JSON.stringify(payload));
  44. }
  45. delete(id) {
  46. return this.httpClient.delete(`/orgs/${id}`);
  47. }
  48. }
  49. export const GrafanaClient = class GrafanaClient {
  50. constructor(httpClient) {
  51. httpClient.onBeforeRequest = this.onBeforeRequest;
  52. this.raw = httpClient;
  53. this.ui = new UIEndpoint(httpClient);
  54. this.orgs = new OrganizationsEndpoint(httpClient.withUrl('/api'));
  55. this.datasources = new DatasourcesEndpoint(httpClient.withUrl('/api'));
  56. }
  57. batch(requests) {
  58. return this.raw.batch(requests);
  59. }
  60. withOrgId(orgId) {
  61. this.orgId = orgId;
  62. }
  63. onBeforeRequest(params) {
  64. if (this.orgId && this.orgId > 0) {
  65. params = params.headers || {};
  66. params.headers["X-Grafana-Org-Id"] = this.orgId;
  67. }
  68. }
  69. }
  70. export const BaseClient = class BaseClient {
  71. constructor(url, subUrl) {
  72. if (url.endsWith('/')) {
  73. url = url.substring(0, url.length - 1);
  74. }
  75. if (subUrl.endsWith('/')) {
  76. subUrl = subUrl.substring(0, subUrl.length - 1);
  77. }
  78. this.url = url + subUrl;
  79. this.onBeforeRequest = () => {};
  80. }
  81. withUrl(subUrl) {
  82. let c = new BaseClient(this.url, subUrl);
  83. c.onBeforeRequest = this.onBeforeRequest;
  84. return c;
  85. }
  86. beforeRequest(params) {
  87. }
  88. get(url, params) {
  89. params = params || {};
  90. this.beforeRequest(params);
  91. this.onBeforeRequest(params);
  92. return http.get(this.url + url, params);
  93. }
  94. formPost(url, body, params) {
  95. params = params || {};
  96. this.beforeRequest(params);
  97. this.onBeforeRequest(params);
  98. return http.post(this.url + url, body, params);
  99. }
  100. post(url, body, params) {
  101. params = params || {};
  102. params.headers = params.headers || {};
  103. params.headers['Content-Type'] = 'application/json';
  104. this.beforeRequest(params);
  105. this.onBeforeRequest(params);
  106. return http.post(this.url + url, body, params);
  107. }
  108. delete(url, params) {
  109. params = params || {};
  110. this.beforeRequest(params);
  111. this.onBeforeRequest(params);
  112. return http.del(this.url + url, null, params);
  113. }
  114. batch(requests) {
  115. for (let n = 0; n < requests.length; n++) {
  116. let params = requests[n].params || {};
  117. params.headers = params.headers || {};
  118. params.headers['Content-Type'] = 'application/json';
  119. this.beforeRequest(params);
  120. this.onBeforeRequest(params);
  121. requests[n].params = params;
  122. requests[n].url = this.url + requests[n].url;
  123. if (requests[n].body) {
  124. requests[n].body = JSON.stringify(requests[n].body);
  125. }
  126. }
  127. return http.batch(requests);
  128. }
  129. }
  130. export class BasicAuthClient extends BaseClient {
  131. constructor(url, subUrl, username, password) {
  132. super(url, subUrl);
  133. this.username = username;
  134. this.password = password;
  135. }
  136. withUrl(subUrl) {
  137. let c = new BasicAuthClient(this.url, subUrl, this.username, this.password);
  138. c.onBeforeRequest = this.onBeforeRequest;
  139. return c;
  140. }
  141. beforeRequest(params) {
  142. params = params || {};
  143. params.headers = params.headers || {};
  144. let token = `${this.username}:${this.password}`;
  145. params.headers['Authorization'] = `Basic ${encoding.b64encode(token)}`;
  146. }
  147. }
  148. export const createClient = (url) => {
  149. return new GrafanaClient(new BaseClient(url, ''));
  150. }
  151. export const createBasicAuthClient = (url, username, password) => {
  152. return new GrafanaClient(new BasicAuthClient(url, '', username, password));
  153. }