Skip to content

Commit

Permalink
Allow changing api key url in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Nov 7, 2024
1 parent acd96b3 commit 23fa774
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
"command": "wakatime.apikey",
"title": "WakaTime: Api Key"
},
{
"command": "wakatime.apiurl",
"title": "WakaTime: Api Url"
},
{
"command": "wakatime.proxy",
"title": "WakaTime: Proxy"
Expand Down
8 changes: 8 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode';

import {
COMMAND_API_KEY,
COMMAND_API_URL,
COMMAND_CONFIG_FILE,
COMMAND_DASHBOARD,
COMMAND_DEBUG,
Expand All @@ -12,6 +13,7 @@ import {
COMMAND_STATUS_BAR_ENABLED,
LogLevel,
} from './constants';

import { Logger } from './logger';
import { WakaTime } from './wakatime';

Expand All @@ -29,6 +31,12 @@ export function activate(ctx: vscode.ExtensionContext) {
}),
);

ctx.subscriptions.push(
vscode.commands.registerCommand(COMMAND_API_URL, function () {
wakatime.promptForApiUrl();
}),
);

ctx.subscriptions.push(
vscode.commands.registerCommand(COMMAND_PROXY, function () {
wakatime.promptForProxy();
Expand Down
21 changes: 21 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,27 @@ export class Options {
return this.cache.api_key_from_env;
}

public async getApiUrl(): Promise<string> {
let apiUrl = this.getApiUrlFromEnv();
if (!apiUrl) {
try {
apiUrl = await this.getSettingAsync<string>('settings', 'api_url');
} catch (err) {
this.logger.debug(`Exception while reading API Url from config file: ${err}`);
}
}
if (!apiUrl) apiUrl = 'https://api.wakatime.com/api/v1';

const suffixes = ['/', '.bulk', '/users/current/heartbeats', '/heartbeats', '/heartbeat'];
for (const suffix of suffixes) {
if (apiUrl.endsWith(suffix)) {
apiUrl = apiUrl.slice(0, -suffix.length);
}
}

return apiUrl;
}

public getApiUrlFromEnv(): string {
if (this.cache.api_url_from_env !== undefined) return this.cache.api_url_from_env;

Expand Down
33 changes: 18 additions & 15 deletions src/wakatime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ export class WakaTime {
});
}

public async promptForApiUrl(): Promise<void> {
const apiUrl = await this.options.getApiUrl();
let promptOptions = {
prompt: 'WakaTime Api Url (Defaults to https://api.wakatime.com/api/v1)',
placeHolder: 'https://api.wakatime.com/api/v1',
value: apiUrl,
ignoreFocusOut: true,
};
vscode.window.showInputBox(promptOptions).then((val) => {
if (val) {
this.options.setSetting('settings', 'api_url', val, false);
}
});
}

public promptForProxy(): void {
this.options.getSetting('settings', 'proxy', false, (proxy: Setting) => {
let defaultVal = proxy.value;
Expand Down Expand Up @@ -363,21 +378,9 @@ export class WakaTime {
});
}

public openDashboardWebsite(): void {
this.options.getSetting('settings', 'api_url', false, (apiUrl: Setting) => {
let url = 'https://wakatime.com/';
if (apiUrl.value?.trim()) {
try {
const parsedUrl = new URL(apiUrl.value);
url = `${parsedUrl.protocol}//${parsedUrl.hostname}${
parsedUrl.port ? `:${parsedUrl.port}` : ''
}`;
} catch (e) {
this.logger.warnException(e);
}
}
vscode.env.openExternal(vscode.Uri.parse(url));
});
public async openDashboardWebsite(): Promise<void> {
const url = (await this.options.getApiUrl()).replace('/api/v1', '');
vscode.env.openExternal(vscode.Uri.parse(url));
}

public openConfigFile(): void {
Expand Down

0 comments on commit 23fa774

Please sign in to comment.