Make API URL configurable and listen to config changes

Also prevents useless API call when no API key is set
This commit is contained in:
Mikko Ahlroth 2017-08-27 20:51:01 +03:00 committed by Juha Ristolainen
parent 24ee812c7d
commit 2bff22d53f
3 changed files with 28 additions and 12 deletions

View File

@ -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!"
}
}
},

View File

@ -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());

View File

@ -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);
}
}