Skip to content

Commit b32a02c

Browse files
committed
Add apiUrl vscode settings json
1 parent 1c694cb commit b32a02c

3 files changed

Lines changed: 72 additions & 70 deletions

File tree

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@
119119
"type": "string",
120120
"description": "Defaults to value from ~/.wakatime.cfg, unless running in browser.",
121121
"scope": "application"
122+
},
123+
"wakatime.apiUrl": {
124+
"type": "string",
125+
"description": "Defaults to https://api.wakatime.com/api/v1.",
126+
"scope": "application"
122127
}
123128
}
124129
}
@@ -146,4 +151,4 @@
146151
"webpack-cli": "^4.9.2",
147152
"which": "^2.0.2"
148153
}
149-
}
154+
}

src/options.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export class Options {
202202
return this.logFile;
203203
}
204204

205-
public async getApiKeyAsync(): Promise<string> {
205+
public async getApiKey(): Promise<string> {
206206
if (!Utils.apiKeyInvalid(this.cache.api_key)) {
207207
return this.cache.api_key;
208208
}
@@ -233,6 +233,11 @@ export class Options {
233233
return apiKey;
234234
} catch (err) {
235235
this.logger.debug(`Exception while reading API Key from config file: ${err}`);
236+
if (`${err}`.includes('spawn EPERM')) {
237+
vscode.window.showErrorMessage(
238+
'Microsoft Defender is blocking WakaTime. Please allow WakaTime to run so it can upload code stats to your dashboard.',
239+
);
240+
}
236241
return '';
237242
}
238243
}
@@ -275,30 +280,14 @@ export class Options {
275280
}
276281
}
277282

278-
public getApiKey(callback: (apiKey: string | null) => void): void {
279-
this.getApiKeyAsync()
280-
.then((apiKey) => {
281-
if (!Utils.apiKeyInvalid(apiKey)) {
282-
callback(apiKey);
283-
} else {
284-
callback(null);
285-
}
286-
})
287-
.catch((err) => {
288-
this.logger.warn(`Unable to get api key: ${err}`);
289-
if (`${err}`.includes('spawn EPERM')) {
290-
vscode.window.showErrorMessage(
291-
'Microsoft Defender is blocking WakaTime. Please allow WakaTime to run so it can upload code stats to your dashboard.',
292-
);
293-
}
294-
callback(null);
295-
});
296-
}
297-
298283
private getApiKeyFromEditor(): string {
299284
return vscode.workspace.getConfiguration().get('wakatime.apiKey') || '';
300285
}
301286

287+
private getApiUrlFromEditor(): string {
288+
return vscode.workspace.getConfiguration().get('wakatime.apiUrl') || '';
289+
}
290+
302291
// Support for gitpod.io https://github.com/wakatime/vscode-wakatime/pull/220
303292
public getApiKeyFromEnv(): string {
304293
if (this.cache.api_key_from_env !== undefined) return this.cache.api_key_from_env;
@@ -308,15 +297,25 @@ export class Options {
308297
return this.cache.api_key_from_env;
309298
}
310299

311-
public async getApiUrl(): Promise<string> {
312-
let apiUrl = this.getApiUrlFromEnv();
300+
public async getApiUrl(checkSettingsFile = false): Promise<string> {
301+
let apiUrl = this.getApiUrlFromEditor();
302+
303+
if (!apiUrl) {
304+
apiUrl = this.getApiUrlFromEnv();
305+
}
306+
307+
if (!apiUrl && !checkSettingsFile) {
308+
return '';
309+
}
310+
313311
if (!apiUrl) {
314312
try {
315313
apiUrl = await this.getSettingAsync<string>('settings', 'api_url');
316314
} catch (err) {
317315
this.logger.debug(`Exception while reading API Url from config file: ${err}`);
318316
}
319317
}
318+
320319
if (!apiUrl) apiUrl = 'https://api.wakatime.com/api/v1';
321320

322321
const suffixes = ['/', '.bulk', '/users/current/heartbeats', '/heartbeats', '/heartbeat'];
@@ -329,7 +328,7 @@ export class Options {
329328
return apiUrl;
330329
}
331330

332-
public getApiUrlFromEnv(): string {
331+
private getApiUrlFromEnv(): string {
333332
if (this.cache.api_url_from_env !== undefined) return this.cache.api_url_from_env;
334333

335334
this.cache.api_url_from_env = process.env.WAKATIME_API_URL || '';
@@ -338,7 +337,7 @@ export class Options {
338337
}
339338

340339
public hasApiKey(callback: (valid: boolean) => void): void {
341-
this.getApiKeyAsync()
340+
this.getApiKey()
342341
.then((apiKey) => callback(!Utils.apiKeyInvalid(apiKey)))
343342
.catch((err) => {
344343
this.logger.warn(`Unable to check for api key: ${err}`);

src/wakatime.ts

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -226,30 +226,29 @@ export class WakaTime {
226226
this.statusBarTeamOther.tooltip = tooltipText;
227227
}
228228

229-
public promptForApiKey(hidden: boolean = true): void {
230-
this.options.getApiKey((defaultVal: string | null) => {
231-
if (Utils.apiKeyInvalid(defaultVal ?? undefined)) defaultVal = '';
232-
let promptOptions = {
233-
prompt: 'WakaTime Api Key',
234-
placeHolder: 'Enter your api key from https://wakatime.com/api-key',
235-
value: defaultVal!,
236-
ignoreFocusOut: true,
237-
password: hidden,
238-
validateInput: Utils.apiKeyInvalid.bind(this),
239-
};
240-
vscode.window.showInputBox(promptOptions).then((val) => {
241-
if (val != undefined) {
242-
let invalid = Utils.apiKeyInvalid(val);
243-
if (!invalid) {
244-
this.options.setSetting('settings', 'api_key', val, false);
245-
} else vscode.window.setStatusBarMessage(invalid);
246-
} else vscode.window.setStatusBarMessage('WakaTime api key not provided');
247-
});
229+
public async promptForApiKey(hidden: boolean = true): Promise<void> {
230+
let defaultVal = await this.options.getApiKey();
231+
if (Utils.apiKeyInvalid(defaultVal ?? undefined)) defaultVal = '';
232+
let promptOptions = {
233+
prompt: 'WakaTime Api Key',
234+
placeHolder: 'Enter your api key from https://wakatime.com/api-key',
235+
value: defaultVal!,
236+
ignoreFocusOut: true,
237+
password: hidden,
238+
validateInput: Utils.apiKeyInvalid.bind(this),
239+
};
240+
vscode.window.showInputBox(promptOptions).then((val) => {
241+
if (val != undefined) {
242+
let invalid = Utils.apiKeyInvalid(val);
243+
if (!invalid) {
244+
this.options.setSetting('settings', 'api_key', val, false);
245+
} else vscode.window.setStatusBarMessage(invalid);
246+
} else vscode.window.setStatusBarMessage('WakaTime api key not provided');
248247
});
249248
}
250249

251250
public async promptForApiUrl(): Promise<void> {
252-
const apiUrl = await this.options.getApiUrl();
251+
const apiUrl = await this.options.getApiUrl(true);
253252
let promptOptions = {
254253
prompt: 'WakaTime Api Url (Defaults to https://api.wakatime.com/api/v1)',
255254
placeHolder: 'https://api.wakatime.com/api/v1',
@@ -379,7 +378,7 @@ export class WakaTime {
379378
}
380379

381380
public async openDashboardWebsite(): Promise<void> {
382-
const url = (await this.options.getApiUrl()).replace('/api/v1', '').replace('api.', '');
381+
const url = (await this.options.getApiUrl(true)).replace('/api/v1', '').replace('api.', '');
383382
vscode.env.openExternal(vscode.Uri.parse(url));
384383
}
385384

@@ -519,31 +518,30 @@ export class WakaTime {
519518
}, this.debounceMs);
520519
}
521520

522-
private sendHeartbeat(
521+
private async sendHeartbeat(
523522
doc: vscode.TextDocument,
524523
time: number,
525524
selection: vscode.Position,
526525
isWrite: boolean,
527526
isCompiling: boolean,
528527
isDebugging: boolean,
529-
): void {
530-
this.options.getApiKey((apiKey) => {
531-
if (apiKey) {
532-
this._sendHeartbeat(doc, time, selection, isWrite, isCompiling, isDebugging);
533-
} else {
534-
this.promptForApiKey();
535-
}
536-
});
528+
): Promise<void> {
529+
const apiKey = await this.options.getApiKey();
530+
if (apiKey) {
531+
await this._sendHeartbeat(doc, time, selection, isWrite, isCompiling, isDebugging);
532+
} else {
533+
await this.promptForApiKey();
534+
}
537535
}
538536

539-
private _sendHeartbeat(
537+
private async _sendHeartbeat(
540538
doc: vscode.TextDocument,
541539
time: number,
542540
selection: vscode.Position,
543541
isWrite: boolean,
544542
isCompiling: boolean,
545543
isDebugging: boolean,
546-
): void {
544+
): Promise<void> {
547545
if (!this.dependencies.isCliInstalled()) return;
548546

549547
let file = doc.fileName;
@@ -580,7 +578,7 @@ export class WakaTime {
580578
const apiKey = this.options.getApiKeyFromEnv();
581579
if (!Utils.apiKeyInvalid(apiKey)) args.push('--key', Utils.quote(apiKey));
582580

583-
const apiUrl = this.options.getApiUrlFromEnv();
581+
const apiUrl = await this.options.getApiUrl();
584582
if (apiUrl) args.push('--api-url', Utils.quote(apiUrl));
585583

586584
const project = this.getProjectName(doc.uri);
@@ -612,7 +610,7 @@ export class WakaTime {
612610
this.logger.error(error.toString());
613611
}
614612
});
615-
proc.on('close', (code, _signal) => {
613+
proc.on('close', async (code, _signal) => {
616614
if (code == 0) {
617615
if (this.showStatusBar) this.getCodingActivity();
618616
} else if (code == 102 || code == 112) {
@@ -642,7 +640,7 @@ export class WakaTime {
642640
let now: number = Date.now();
643641
if (this.lastApiKeyPrompted < now - 86400000) {
644642
// only prompt once per day
645-
this.promptForApiKey(false);
643+
await this.promptForApiKey(false);
646644
this.lastApiKeyPrompted = now;
647645
}
648646
} else {
@@ -656,21 +654,21 @@ export class WakaTime {
656654
});
657655
}
658656

659-
private getCodingActivity() {
657+
private async getCodingActivity() {
660658
if (!this.showStatusBar) return;
661659

662660
const cutoff = Date.now() - this.fetchTodayInterval;
663661
if (this.lastFetchToday > cutoff) return;
664662

665663
this.lastFetchToday = Date.now();
666664

667-
this.options.getApiKey((apiKey) => {
668-
if (!apiKey) return;
669-
this._getCodingActivity();
670-
});
665+
const apiKey = await this.options.getApiKey();
666+
if (!apiKey) return;
667+
668+
await this._getCodingActivity();
671669
}
672670

673-
private _getCodingActivity() {
671+
private async _getCodingActivity() {
674672
if (!this.dependencies.isCliInstalled()) return;
675673

676674
let user_agent =
@@ -682,7 +680,7 @@ export class WakaTime {
682680
const apiKey = this.options.getApiKeyFromEnv();
683681
if (!Utils.apiKeyInvalid(apiKey)) args.push('--key', Utils.quote(apiKey));
684682

685-
const apiUrl = this.options.getApiUrlFromEnv();
683+
const apiUrl = await this.options.getApiUrl();
686684
if (apiUrl) args.push('--api-url', Utils.quote(apiUrl));
687685

688686
if (Desktop.isWindows()) {
@@ -764,7 +762,7 @@ export class WakaTime {
764762
}
765763
}
766764

767-
private updateTeamStatusBar(doc?: vscode.TextDocument) {
765+
private async updateTeamStatusBar(doc?: vscode.TextDocument) {
768766
if (!this.showStatusBarTeam) return;
769767
if (!this.hasTeamFeatures) return;
770768
if (!this.dependencies.isCliInstalled()) return;
@@ -802,7 +800,7 @@ export class WakaTime {
802800
const apiKey = this.options.getApiKeyFromEnv();
803801
if (!Utils.apiKeyInvalid(apiKey)) args.push('--key', Utils.quote(apiKey));
804802

805-
const apiUrl = this.options.getApiUrlFromEnv();
803+
const apiUrl = await this.options.getApiUrl();
806804
if (apiUrl) args.push('--api-url', Utils.quote(apiUrl));
807805

808806
const project = this.getProjectName(doc.uri);

0 commit comments

Comments
 (0)