Skip to content

Commit afc06ff

Browse files
authored
Update Rovo Dev API calls to include an Authorization Bearer token (#1347)
1 parent 0a7a732 commit afc06ff

File tree

6 files changed

+99
-33
lines changed

6 files changed

+99
-33
lines changed

jest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const baseConfigFor = (project: string, testExtension: string): Config =>
6666
testExtension === 'ts'
6767
? {
6868
statements: 65,
69-
branches: 55,
69+
branches: 54,
7070
functions: 59,
7171
lines: 65,
7272
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
],
4747
"main": "./build/extension/extension",
4848
"rovoDev": {
49-
"version": "0.13.4"
49+
"version": "0.13.6"
5050
},
5151
"scripts": {
5252
"vscode:uninstall": "node ./build/extension/uninstall.js",

src/rovo-dev/client/rovoDevApiClient.test.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('RovoDevApiClient', () => {
1717
const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
1818

1919
beforeEach(() => {
20-
client = new RovoDevApiClient('localhost', 8080);
20+
client = new RovoDevApiClient('localhost', 8080, 'sessionToken');
2121
mockFetch.mockClear();
2222
});
2323

@@ -27,12 +27,12 @@ describe('RovoDevApiClient', () => {
2727

2828
describe('constructor', () => {
2929
it('should create instance with correct base URL', () => {
30-
const testClient = new RovoDevApiClient('example.com', 3000);
30+
const testClient = new RovoDevApiClient('example.com', 3000, 'sessionToken');
3131
expect(testClient.baseApiUrl).toBe('http://example.com:3000');
3232
});
3333

3434
it('should handle IP address and port', () => {
35-
const testClient = new RovoDevApiClient('192.168.1.1', 9000);
35+
const testClient = new RovoDevApiClient('192.168.1.1', 9000, 'sessionToken');
3636
expect(testClient.baseApiUrl).toBe('http://192.168.1.1:9000');
3737
});
3838
});
@@ -60,6 +60,7 @@ describe('RovoDevApiClient', () => {
6060
headers: {
6161
accept: 'text/event-stream',
6262
'Content-Type': 'application/json',
63+
Authorization: 'Bearer sessionToken',
6364
},
6465
body: undefined,
6566
});
@@ -83,6 +84,7 @@ describe('RovoDevApiClient', () => {
8384
headers: {
8485
accept: 'text/event-stream',
8586
'Content-Type': 'application/json',
87+
Authorization: 'Bearer sessionToken',
8688
},
8789
body,
8890
});
@@ -136,6 +138,7 @@ describe('RovoDevApiClient', () => {
136138
headers: {
137139
accept: 'text/event-stream',
138140
'Content-Type': 'application/json',
141+
Authorization: 'Bearer sessionToken',
139142
},
140143
body: undefined,
141144
});
@@ -187,6 +190,7 @@ describe('RovoDevApiClient', () => {
187190
method: 'POST',
188191
headers: {
189192
accept: 'text/event-stream',
193+
Authorization: 'Bearer sessionToken',
190194
'Content-Type': 'application/json',
191195
},
192196
body: JSON.stringify({ message, context: [] }),
@@ -201,6 +205,7 @@ describe('RovoDevApiClient', () => {
201205
headers: {
202206
accept: 'text/event-stream',
203207
'Content-Type': 'application/json',
208+
Authorization: 'Bearer sessionToken',
204209
},
205210
},
206211
);
@@ -222,6 +227,7 @@ describe('RovoDevApiClient', () => {
222227
headers: {
223228
accept: 'text/event-stream',
224229
'Content-Type': 'application/json',
230+
Authorization: 'Bearer sessionToken',
225231
},
226232
body: JSON.stringify({ message, context: [] }),
227233
});
@@ -235,6 +241,7 @@ describe('RovoDevApiClient', () => {
235241
headers: {
236242
accept: 'text/event-stream',
237243
'Content-Type': 'application/json',
244+
Authorization: 'Bearer sessionToken',
238245
},
239246
},
240247
);
@@ -260,6 +267,7 @@ describe('RovoDevApiClient', () => {
260267
headers: {
261268
accept: 'text/event-stream',
262269
'Content-Type': 'application/json',
270+
Authorization: 'Bearer sessionToken',
263271
},
264272
body: JSON.stringify({ message, enable_deep_plan: true, context: [] }),
265273
});
@@ -273,6 +281,7 @@ describe('RovoDevApiClient', () => {
273281
headers: {
274282
accept: 'text/event-stream',
275283
'Content-Type': 'application/json',
284+
Authorization: 'Bearer sessionToken',
276285
},
277286
},
278287
);
@@ -293,6 +302,7 @@ describe('RovoDevApiClient', () => {
293302
headers: {
294303
accept: 'text/event-stream',
295304
'Content-Type': 'application/json',
305+
Authorization: 'Bearer sessionToken',
296306
},
297307
body: JSON.stringify({ message: '', context: [] }),
298308
});
@@ -306,6 +316,7 @@ describe('RovoDevApiClient', () => {
306316
headers: {
307317
accept: 'text/event-stream',
308318
'Content-Type': 'application/json',
319+
Authorization: 'Bearer sessionToken',
309320
},
310321
},
311322
);
@@ -327,6 +338,7 @@ describe('RovoDevApiClient', () => {
327338
headers: {
328339
accept: 'text/event-stream',
329340
'Content-Type': 'application/json',
341+
Authorization: 'Bearer sessionToken',
330342
},
331343
body: JSON.stringify({ message, context: [] }),
332344
});
@@ -340,6 +352,7 @@ describe('RovoDevApiClient', () => {
340352
headers: {
341353
accept: 'text/event-stream',
342354
'Content-Type': 'application/json',
355+
Authorization: 'Bearer sessionToken',
343356
},
344357
},
345358
);
@@ -362,6 +375,7 @@ describe('RovoDevApiClient', () => {
362375
headers: {
363376
accept: 'text/event-stream',
364377
'Content-Type': 'application/json',
378+
Authorization: 'Bearer sessionToken',
365379
},
366380
body: undefined,
367381
});
@@ -401,6 +415,7 @@ describe('RovoDevApiClient', () => {
401415
headers: {
402416
accept: 'text/event-stream',
403417
'Content-Type': 'application/json',
418+
Authorization: 'Bearer sessionToken',
404419
},
405420
body: undefined,
406421
},
@@ -427,6 +442,7 @@ describe('RovoDevApiClient', () => {
427442
headers: {
428443
accept: 'text/event-stream',
429444
'Content-Type': 'application/json',
445+
Authorization: 'Bearer sessionToken',
430446
},
431447
body: undefined,
432448
},
@@ -466,6 +482,7 @@ describe('RovoDevApiClient', () => {
466482
headers: {
467483
accept: 'text/event-stream',
468484
'Content-Type': 'application/json',
485+
Authorization: 'Bearer sessionToken',
469486
},
470487
body: undefined,
471488
});
@@ -520,6 +537,7 @@ describe('RovoDevApiClient', () => {
520537
headers: {
521538
accept: 'text/event-stream',
522539
'Content-Type': 'application/json',
540+
Authorization: 'Bearer sessionToken',
523541
},
524542
body: undefined,
525543
});
@@ -610,6 +628,7 @@ describe('RovoDevApiClient', () => {
610628
headers: {
611629
accept: 'text/event-stream',
612630
'Content-Type': 'application/json',
631+
Authorization: 'Bearer sessionToken',
613632
},
614633
body: JSON.stringify({ servers: [], accept_all: 'true' }),
615634
});
@@ -630,6 +649,7 @@ describe('RovoDevApiClient', () => {
630649
headers: {
631650
accept: 'text/event-stream',
632651
'Content-Type': 'application/json',
652+
Authorization: 'Bearer sessionToken',
633653
},
634654
body: JSON.stringify({
635655
servers: [{ server_name: 'server1', decision: 'accept' }],
@@ -653,6 +673,7 @@ describe('RovoDevApiClient', () => {
653673
headers: {
654674
accept: 'text/event-stream',
655675
'Content-Type': 'application/json',
676+
Authorization: 'Bearer sessionToken',
656677
},
657678
body: JSON.stringify({ servers: [{ server_name: 'server2', decision: 'deny' }], accept_all: 'false' }),
658679
});
@@ -689,6 +710,7 @@ describe('RovoDevApiClient', () => {
689710
headers: {
690711
accept: 'text/event-stream',
691712
'Content-Type': 'application/json',
713+
Authorization: 'Bearer sessionToken',
692714
},
693715
body: undefined,
694716
});
@@ -734,6 +756,7 @@ describe('RovoDevApiClient', () => {
734756
headers: {
735757
accept: 'text/event-stream',
736758
'Content-Type': 'application/json',
759+
Authorization: 'Bearer sessionToken',
737760
},
738761
body: JSON.stringify({
739762
decisions: [
@@ -765,6 +788,7 @@ describe('RovoDevApiClient', () => {
765788
headers: {
766789
accept: 'text/event-stream',
767790
'Content-Type': 'application/json',
791+
Authorization: 'Bearer sessionToken',
768792
},
769793
body: JSON.stringify({
770794
decisions: [
@@ -796,6 +820,7 @@ describe('RovoDevApiClient', () => {
796820
headers: {
797821
accept: 'text/event-stream',
798822
'Content-Type': 'application/json',
823+
Authorization: 'Bearer sessionToken',
799824
},
800825
body: JSON.stringify({
801826
decisions: [
@@ -829,6 +854,7 @@ describe('RovoDevApiClient', () => {
829854
headers: {
830855
accept: 'text/event-stream',
831856
'Content-Type': 'application/json',
857+
Authorization: 'Bearer sessionToken',
832858
},
833859
body: JSON.stringify({
834860
decisions: [
@@ -866,6 +892,7 @@ describe('RovoDevApiClient', () => {
866892
headers: {
867893
accept: 'text/event-stream',
868894
'Content-Type': 'application/json',
895+
Authorization: 'Bearer sessionToken',
869896
},
870897
body: JSON.stringify({
871898
decisions: [],
@@ -893,6 +920,7 @@ describe('RovoDevApiClient', () => {
893920
headers: {
894921
accept: 'text/event-stream',
895922
'Content-Type': 'application/json',
923+
Authorization: 'Bearer sessionToken',
896924
},
897925
body: JSON.stringify({
898926
decisions: [

src/rovo-dev/client/rovoDevApiClient.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function statusIsSuccessful(status: number | undefined) {
99
return !!status && Math.floor(status / 100) === 2;
1010
}
1111

12-
class RovoDevApiError extends Error {
12+
export class RovoDevApiError extends Error {
1313
constructor(
1414
message: string,
1515
public httpStatus: number,
@@ -21,6 +21,8 @@ class RovoDevApiError extends Error {
2121

2222
/** Implements the http client for the RovoDev CLI server */
2323
export class RovoDevApiClient {
24+
private readonly _authBearerHeader: string | undefined;
25+
2426
private readonly _baseApiUrl: string;
2527
/** Base API's URL for the RovoDev service */
2628
public get baseApiUrl() {
@@ -31,21 +33,28 @@ export class RovoDevApiClient {
3133
* @param {string} hostnameOrIp The hostname or IP address for the Rovo Dev service.
3234
* @param {number} port The http port for the Rovo Dev service.
3335
*/
34-
constructor(hostnameOrIp: string, port: number) {
36+
constructor(hostnameOrIp: string, port: number, sessionToken: string) {
3537
this._baseApiUrl = `http://${hostnameOrIp}:${port}`;
38+
this._authBearerHeader = sessionToken ? 'Bearer ' + sessionToken : undefined;
3639
}
3740

3841
private async fetchApi(restApi: string, method: 'GET'): Promise<Response>;
3942
private async fetchApi(restApi: string, method: 'POST', body?: BodyInit | null): Promise<Response>;
4043
private async fetchApi(restApi: string, method: 'GET' | 'POST', body?: BodyInit | null): Promise<Response> {
44+
const headers: Record<string, string> = {
45+
accept: 'text/event-stream',
46+
'Content-Type': 'application/json',
47+
};
48+
49+
if (this._authBearerHeader) {
50+
headers['Authorization'] = this._authBearerHeader;
51+
}
52+
4153
let response: Response;
4254
try {
4355
response = await fetch(this._baseApiUrl + restApi, {
4456
method,
45-
headers: {
46-
accept: 'text/event-stream',
47-
'Content-Type': 'application/json',
48-
},
57+
headers,
4958
body,
5059
});
5160
} catch (error) {

0 commit comments

Comments
 (0)