client.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. return new BaseClient(this.url, subUrl);
  83. }
  84. beforeRequest(params) {
  85. }
  86. get(url, params) {
  87. params = params || {};
  88. this.beforeRequest(params);
  89. this.onBeforeRequest(params);
  90. return http.get(this.url + url, params);
  91. }
  92. formPost(url, body, params) {
  93. params = params || {};
  94. this.beforeRequest(params);
  95. this.onBeforeRequest(params);
  96. return http.post(this.url + url, body, params);
  97. }
  98. post(url, body, params) {
  99. params = params || {};
  100. params.headers = params.headers || {};
  101. params.headers['Content-Type'] = 'application/json';
  102. this.beforeRequest(params);
  103. this.onBeforeRequest(params);
  104. return http.post(this.url + url, body, params);
  105. }
  106. delete(url, params) {
  107. params = params || {};
  108. this.beforeRequest(params);
  109. this.onBeforeRequest(params);
  110. return http.del(this.url + url, null, params);
  111. }
  112. batch(requests) {
  113. for (let n = 0; n < requests.length; n++) {
  114. let params = requests[n].params || {};
  115. params.headers = params.headers || {};
  116. params.headers['Content-Type'] = 'application/json';
  117. this.beforeRequest(params);
  118. this.onBeforeRequest(params);
  119. requests[n].params = params;
  120. requests[n].url = this.url + requests[n].url;
  121. if (requests[n].body) {
  122. requests[n].body = JSON.stringify(requests[n].body);
  123. }
  124. }
  125. return http.batch(requests);
  126. }
  127. }
  128. export class BasicAuthClient extends BaseClient {
  129. constructor(url, subUrl, username, password) {
  130. super(url, subUrl);
  131. this.username = username;
  132. this.password = password;
  133. }
  134. withUrl(subUrl) {
  135. return new BasicAuthClient(this.url, subUrl, this.username, this.password);
  136. }
  137. beforeRequest(params) {
  138. params = params || {};
  139. params.headers = params.headers || {};
  140. let token = `${this.username}:${this.password}`;
  141. params.headers['Authorization'] = `Basic ${encoding.b64encode(token)}`;
  142. }
  143. }
  144. export const createClient = (url) => {
  145. return new GrafanaClient(new BaseClient(url, ''));
  146. }
  147. export const createBasicAuthClient = (url, username, password) => {
  148. return new GrafanaClient(new BasicAuthClient(url, '', username, password));
  149. }