Skip to content

Commit

Permalink
Allow changing api url in web extension
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Nov 7, 2024
1 parent 72e5e62 commit 44a84bf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const COMMAND_API_KEY = 'wakatime.apikey';
export const COMMAND_API_URL = 'wakatime.apiurl';
export const COMMAND_CONFIG_FILE = 'wakatime.config_file';
export const COMMAND_DASHBOARD = 'wakatime.dashboard';
export const COMMAND_DEBUG = 'wakatime.debug';
Expand Down
8 changes: 8 additions & 0 deletions src/web/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import * as vscode from 'vscode';

import {
COMMAND_API_KEY,
COMMAND_API_URL,
COMMAND_DASHBOARD,
COMMAND_DEBUG,
COMMAND_DISABLE,
COMMAND_STATUS_BAR_CODING_ACTIVITY,
COMMAND_STATUS_BAR_ENABLED,
LogLevel,
} from '../constants';

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

Expand All @@ -26,6 +28,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_DEBUG, function () {
wakatime.promptForDebug();
Expand Down
37 changes: 33 additions & 4 deletions src/web/wakatime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ export class WakaTime {
});
}

public promptForApiUrl(): void {
const defaultVal: string = this.config.get('wakatime.apiUrl') || '';
let promptOptions = {
prompt: 'WakaTime Api Url (Defaults to https://api.wakatime.com/api/v1)',
placeHolder: 'https://api.wakatime.com/api/v1',
value: defaultVal,
ignoreFocusOut: true,
};
vscode.window.showInputBox(promptOptions).then((val) => {
if (val) {
this.config.update('wakatime.apiUrl', val);
}
});
}

public promptForDebug(): void {
let defaultVal: string = this.config.get('wakatime.debug') || '';
if (!defaultVal || defaultVal !== 'true') defaultVal = 'false';
Expand Down Expand Up @@ -280,7 +295,7 @@ export class WakaTime {
}

public openDashboardWebsite(): void {
let url = 'https://wakatime.com/';
const url = this.getApiUrl().replace('/api/v1', '');
vscode.env.openExternal(vscode.Uri.parse(url));
}

Expand Down Expand Up @@ -478,7 +493,8 @@ export class WakaTime {
this.logger.debug(`Sending heartbeat: ${JSON.stringify(payload)}`);

const apiKey = this.config.get('wakatime.apiKey');
const url = `https://api.wakatime.com/api/v1/users/current/heartbeats?api_key=${apiKey}`;
const apiUrl = this.getApiUrl();
const url = `${apiUrl}/users/current/heartbeats?api_key=${apiKey}`;

try {
const response = await fetch(url, {
Expand Down Expand Up @@ -544,7 +560,8 @@ export class WakaTime {
private async _getCodingActivity() {
this.logger.debug('Fetching coding activity for Today from api.');
const apiKey = this.config.get('wakatime.apiKey');
const url = `https://api.wakatime.com/api/v1/users/current/statusbar/today?api_key=${apiKey}`;
const apiUrl = this.getApiUrl();
const url = `${apiUrl}/users/current/statusbar/today?api_key=${apiKey}`;
try {
const response = await fetch(url, {
method: 'GET',
Expand Down Expand Up @@ -626,7 +643,8 @@ export class WakaTime {

this.logger.debug('Fetching devs for currently focused file from api.');
const apiKey = this.config.get('wakatime.apiKey');
const url = `https://api.wakatime.com/api/v1/users/current/file_experts?api_key=${apiKey}`;
const apiUrl = this.getApiUrl();
const url = `${apiUrl}/users/current/file_experts?api_key=${apiKey}`;

const payload = {
entity: file,
Expand Down Expand Up @@ -798,6 +816,17 @@ export class WakaTime {
return null;
}

private getApiUrl(): string {
let apiUrl: string = this.config.get('wakatime.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;
}

private countSlashesInPath(path: string): number {
if (!path) return 0;

Expand Down

0 comments on commit 44a84bf

Please sign in to comment.