diff --git a/package.json b/package.json index ab65d44..26192ce 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,11 @@ "type": "string", "default": "", "description": "Code::Stats API key for this machine." + }, + "codestats.apiurl": { + "type": "string", + "default": "https://codestats.net/api/my/pulses", + "description": "Code::Stats API URL. Only change if you know what you are doing!" } } }, @@ -52,4 +57,4 @@ "dependencies": { "axios": "0.15.3" } -} \ No newline at end of file +} diff --git a/src/code-stats-api.ts b/src/code-stats-api.ts index 20381ab..df17a22 100644 --- a/src/code-stats-api.ts +++ b/src/code-stats-api.ts @@ -6,11 +6,12 @@ import * as axios from "axios"; export class CodeStatsAPI { private API_KEY = null; private UPDATE_URL = "https://codestats.net/api/my/pulses"; - private axios; + private axios = null; - constructor(apiKey: string) { + constructor(apiKey: string, apiURL: string) { this.API_KEY = apiKey; - if (this.API_KEY === null || this.API_KEY === undefined) { + this.UPDATE_URL = apiURL; + if (this.API_KEY === null || this.API_KEY === undefined || this.API_KEY === '') { return; } @@ -26,6 +27,11 @@ export class CodeStatsAPI { } public sendUpdate(pulse: Pulse): void { + // If we did not have API key, don't try to update + if (this.axios === null) { + return null; + } + // tslint:disable-next-line:typedef const data = new ApiJSON(new Date()); diff --git a/src/xp-counter.ts b/src/xp-counter.ts index c25d561..133b24b 100644 --- a/src/xp-counter.ts +++ b/src/xp-counter.ts @@ -26,14 +26,7 @@ export class XpCounter { ); */ - let config: WorkspaceConfiguration = workspace.getConfiguration("codestats"); - if (!config) { - return; - } - - // tslint:disable-next-line:typedef - let apiKey = config.get("apikey"); - this.api = new CodeStatsAPI(`${apiKey}`); + this.initAPI(); if (!this.statusBarItem) { this.statusBarItem = window.createStatusBarItem(StatusBarAlignment.Left); @@ -41,6 +34,7 @@ export class XpCounter { let subscriptions: Disposable[] = []; workspace.onDidChangeTextDocument(this.onTextDocumentChanged, this, subscriptions); + workspace.onDidChangeConfiguration(this.initAPI, this, subscriptions); this.combinedDisposable = Disposable.from(...subscriptions); } @@ -117,4 +111,15 @@ export class XpCounter { return true; } + private initAPI() { + let config: WorkspaceConfiguration = workspace.getConfiguration("codestats"); + if (!config) { + return; + } + + const apiKey: string = config.get("apikey"); + const apiURL: string = config.get("apiurl"); + console.log("code-stats-vscode setting up with API URL", apiURL, "and key", apiKey); + this.api = new CodeStatsAPI(apiKey, apiURL); + } }