1- import http from " k6/http" ;
1+ import http from ' k6/http' ;
22
33export const DatasourcesEndpoint = class DatasourcesEndpoint {
44 constructor ( httpClient ) {
@@ -68,31 +68,34 @@ export const UIEndpoint = class UIEndpoint {
6868
6969 login ( username , pwd ) {
7070 const payload = { user : username , password : pwd } ;
71- return this . httpClient . formPost ( '/login' , payload ) ;
71+ return this . httpClient . post ( '/login' , JSON . stringify ( payload ) ) ;
7272 }
7373
7474 renderPanel ( orgId , dashboardUid , panelId ) {
75- return this . httpClient . get (
76- `/render/d-solo/${ dashboardUid } /graph-panel` ,
77- {
78- orgId,
79- panelId,
80- width : 1000 ,
81- height : 500 ,
82- tz : 'Europe/Stockholm' ,
83- }
84- ) ;
75+ return this . httpClient . get ( `/render/d-solo/${ dashboardUid } /graph-panel` , {
76+ orgId,
77+ panelId : `panel-${ panelId } ` ,
78+ width : 1000 ,
79+ height : 500 ,
80+ tz : 'Europe/Stockholm' ,
81+ timeout : '10' ,
82+ } ) ;
8583 }
86- }
84+ } ;
8785
8886export const GrafanaClient = class GrafanaClient {
8987 constructor ( httpClient ) {
9088 httpClient . onBeforeRequest = ( params ) => {
89+ params . headers = params . headers || { } ;
90+
9191 if ( this . orgId && this . orgId > 0 ) {
92- params . headers = params . headers || { } ;
93- params . headers [ "X-Grafana-Org-Id" ] = this . orgId ;
92+ params . headers [ 'X-Grafana-Org-Id' ] = this . orgId ;
9493 }
95- }
94+
95+ if ( this . authToken ) {
96+ params . headers [ 'Authorization' ] = `Bearer ${ this . authToken } ` ;
97+ }
98+ } ;
9699
97100 this . raw = httpClient ;
98101 this . dashboards = new DashboardsEndpoint ( httpClient . withUrl ( '/api' ) ) ;
@@ -118,7 +121,11 @@ export const GrafanaClient = class GrafanaClient {
118121 withOrgId ( orgId ) {
119122 this . orgId = orgId ;
120123 }
121- }
124+
125+ withAuthToken ( authToken ) {
126+ this . authToken = authToken ;
127+ }
128+ } ;
122129
123130export const BaseClient = class BaseClient {
124131 constructor ( url , subUrl ) {
@@ -135,35 +142,28 @@ export const BaseClient = class BaseClient {
135142 }
136143
137144 withUrl ( subUrl ) {
138- let c = new BaseClient ( this . url , subUrl ) ;
145+ let c = new BaseClient ( this . url , subUrl ) ;
139146 c . onBeforeRequest = this . onBeforeRequest ;
140147 return c ;
141148 }
142149
143- beforeRequest ( params ) {
144-
145- }
150+ beforeRequest ( params ) { }
146151
147152 get ( url , queryParams , params ) {
148153 params = params || { } ;
149154 this . onBeforeRequest ( params ) ;
150155
151156 if ( queryParams ) {
152- url += '?' + Array . from ( Object . entries ( queryParams ) ) . map ( ( [ key , value ] ) =>
153- `${ key } =${ encodeURIComponent ( value ) } `
154- ) . join ( '&' ) ;
157+ url +=
158+ '?' +
159+ Array . from ( Object . entries ( queryParams ) )
160+ . map ( ( [ key , value ] ) => `${ key } =${ encodeURIComponent ( value ) } ` )
161+ . join ( '&' ) ;
155162 }
156163
157164 return http . get ( this . url + url , params ) ;
158165 }
159166
160- formPost ( url , body , params ) {
161- params = params || { } ;
162- this . beforeRequest ( params ) ;
163- this . onBeforeRequest ( params ) ;
164- return http . post ( this . url + url , body , params ) ;
165- }
166-
167167 post ( url , body , params ) {
168168 params = params || { } ;
169169 params . headers = params . headers || { } ;
@@ -206,8 +206,8 @@ export const BaseClient = class BaseClient {
206206
207207 return http . batch ( requests ) ;
208208 }
209- }
209+ } ;
210210
211211export const createClient = ( url ) => {
212212 return new GrafanaClient ( new BaseClient ( url , '' ) ) ;
213- }
213+ } ;
0 commit comments