Skip to content

Commit

Permalink
Enhancement: support speedtest v1.2 API (#4695)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamoon authored Feb 3, 2025
1 parent cbacf45 commit 07dff4c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
9 changes: 8 additions & 1 deletion docs/widgets/services/speedtest-tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ Learn more about [Speedtest Tracker](https://github.com/alexjustesen/speedtest-t

No extra configuration is required.

This widget is compatible with both [alexjustesen/speedtest-tracker](https://github.com/alexjustesen/speedtest-tracker) and [henrywhitaker3/Speedtest-Tracker](https://github.com/henrywhitaker3/Speedtest-Tracker).
Version 1 of the widget is compatible with both [alexjustesen/speedtest-tracker](https://github.com/alexjustesen/speedtest-tracker) and [henrywhitaker3/Speedtest-Tracker](https://github.com/henrywhitaker3/Speedtest-Tracker), while version 2 is only compatible with [alexjustesen/speedtest-tracker](https://github.com/alexjustesen/speedtest-tracker).

| Speedtest Version (AJ) | Speedtest Version (HW) | Homepage Widget Version |
| ---------------------- | ---------------------- | ----------------------- |
| < 1.2.1 | ≤ 1.12.0 | 1 (default) |
| >= 1.2.1 | N/A | 2 |

Allowed fields: `["download", "upload", "ping"]`.

```yaml
widget:
type: speedtest
url: http://speedtest.host.or.ip
version: 1 # optional, default is 1
key: speedtestapikey # required for version 2
bitratePrecision: 3 # optional, default is 0
```
4 changes: 2 additions & 2 deletions src/utils/config/service-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export function cleanServiceGroups(groups) {
// frigate
enableRecentEvents,

// beszel, glances, immich, mealie, pihole, pfsense
// beszel, glances, immich, mealie, pihole, pfsense, speedtest
version,

// glances
Expand Down Expand Up @@ -610,7 +610,7 @@ export function cleanServiceGroups(groups) {
if (snapshotHost) widget.snapshotHost = snapshotHost;
if (snapshotPath) widget.snapshotPath = snapshotPath;
}
if (["beszel", "glances", "immich", "mealie", "pfsense", "pihole"].includes(type)) {
if (["beszel", "glances", "immich", "mealie", "pfsense", "pihole", "speedtest"].includes(type)) {
if (version) widget.version = parseInt(version, 10);
}
if (type === "glances") {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/proxy/handlers/credentialed.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ export default async function credentialedProxyHandler(req, res, map) {
headers.Authorization = widget.password;
} else if (widget.type === "gitlab") {
headers["PRIVATE-TOKEN"] = widget.key;
} else if (widget.type === "speedtest") {
if (widget.key) {
// v1 does not require a key
headers.Authorization = `Bearer ${widget.key}`;
}
} else {
headers["X-API-Key"] = `${widget.key}`;
}
Expand Down
9 changes: 5 additions & 4 deletions src/widgets/speedtest/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ export default function Component({ service }) {

const { widget } = service;

const { data: speedtestData, error: speedtestError } = useWidgetAPI(widget, "speedtest/latest");
const endpoint = widget.version === 2 ? "latestv2" : "latestv1";
const { data: speedtestData, error: speedtestError } = useWidgetAPI(widget, endpoint);

const bitratePrecision =
!widget?.bitratePrecision || Number.isNaN(widget?.bitratePrecision) || widget?.bitratePrecision < 0
? 0
: widget.bitratePrecision;

if (speedtestError) {
return <Container service={service} error={speedtestError} />;
if (speedtestError || speedtestData?.error) {
return <Container service={service} error={speedtestError ?? speedtestData.error} />;
}

if (!speedtestData) {
if (!speedtestData?.data) {
return (
<Container service={service}>
<Block label="speedtest.download" />
Expand Down
8 changes: 6 additions & 2 deletions src/widgets/speedtest/widget.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import genericProxyHandler from "utils/proxy/handlers/generic";
import genericProxyHandler from "utils/proxy/handlers/credentialed";

const widget = {
api: "{url}/api/{endpoint}",
proxyHandler: genericProxyHandler,

mappings: {
"speedtest/latest": {
latestv1: {
endpoint: "speedtest/latest",
validate: ["data"],
},
latestv2: {
endpoint: "v1/results/latest",
validate: ["data"],
},
},
};

Expand Down

0 comments on commit 07dff4c

Please sign in to comment.