Skip to content

Commit

Permalink
perf: Optimize request parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Nov 20, 2023
1 parent 95de7e0 commit 8aa9208
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/com.gabe565.nightscout.sdPlugin/js/Nightscout.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ class Nightscout {
}
this.template = new Template();
this.context = context;
this.url = "";
this.request = { headers: {} };
this.settings = settings;
nightscoutMap[context] = this;
}

set settings(settings) {
this._settings = settings;
if (this.settings.nightscoutUrl) {
this.url = new URL(this.settings.nightscoutUrl);
this.url.pathname += "api/v2/properties";
if (this.settings.token) {
this.request.headers["Api-Secret"] = this.settings.token;
}
this.beginTick();
}
}
Expand All @@ -23,18 +30,12 @@ class Nightscout {
}

async tick() {
if (!this.settings.nightscoutUrl) {
if (!this.url) {
return;
}

try {
const url = new URL(this.settings.nightscoutUrl);
url.pathname += "api/v2/properties";
const headers = {};
if (this.settings.token) {
headers["Api-Secret"] = this.settings.token;
}
const response = await fetch(url, { headers });
const response = await fetch(this.url, this.request);
const data = await response.json();
$SD.setImage(this.context, this.template.render(data));
} catch (err) {
Expand All @@ -44,7 +45,7 @@ class Nightscout {
}

beginTick() {
if (!this.settings.nightscoutUrl) {
if (!this.url) {
return;
}

Expand Down

0 comments on commit 8aa9208

Please sign in to comment.