Skip to content

Commit 2d40d98

Browse files
[Chore] Consolidate Results, Speedtest & Stats API Endpoints into Dedicated Controllers (#2225)
Co-authored-by: Alex Justesen <alexjustesen@users.noreply.github.com>
1 parent d8d31be commit 2d40d98

21 files changed

+768
-497
lines changed

app/Actions/GetOoklaSpeedtestServers.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Actions;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Facades\Http;
67
use Illuminate\Support\Facades\Log;
78
use Lorisleiva\Actions\Concerns\AsAction;
@@ -11,7 +12,22 @@ class GetOoklaSpeedtestServers
1112
{
1213
use AsAction;
1314

15+
/**
16+
* For UI: return the ID, Sponsor, and Name to start a manual test
17+
*/
1418
public function handle(): array
19+
{
20+
return collect(self::fetch())->mapWithKeys(function (array $item) {
21+
return [
22+
$item['id'] => ($item['sponsor'] ?? 'Unknown').' ('.($item['name'] ?? 'Unknown').', '.$item['id'].')',
23+
];
24+
})->toArray();
25+
}
26+
27+
/**
28+
* Fetch the raw Ookla server array from the Ookla API.
29+
*/
30+
public static function fetch(): array
1531
{
1632
$query = [
1733
'engine' => 'js',
@@ -23,17 +39,37 @@ public function handle(): array
2339
$response = Http::retry(3, 250)
2440
->timeout(5)
2541
->get(url: 'https://www.speedtest.net/api/js/servers', query: $query);
42+
43+
return $response->json();
2644
} catch (Throwable $e) {
2745
Log::error('Unable to retrieve Ookla servers.', [$e->getMessage()]);
2846

2947
return [
3048
'⚠️ Unable to retrieve Ookla servers, check internet connection and see logs.',
3149
];
3250
}
51+
}
52+
53+
/**
54+
* For API: return array of structured server objects
55+
*/
56+
public static function forApi(): array
57+
{
58+
$servers = self::fetch();
59+
60+
// If the first item is not an array, treat as error or empty
61+
if (empty($servers) || ! is_array($servers) || (isset($servers[0]) && ! is_array($servers[0]))) {
62+
// Optionally, you could return an error message here, but to match the controller's behavior, return an empty array
63+
return [];
64+
}
3365

34-
return $response->collect()->mapWithKeys(function (array $item, int $key) {
66+
return collect($servers)->map(function (array $item) {
3567
return [
36-
$item['id'] => $item['sponsor'].' ('.$item['name'].', '.$item['id'].')',
68+
'id' => $item['id'],
69+
'host' => Arr::get($item, 'host', 'Unknown'),
70+
'name' => Arr::get($item, 'sponsor', 'Unknown'),
71+
'location' => Arr::get($item, 'name', 'Unknown'),
72+
'country' => Arr::get($item, 'country', 'Unknown'),
3773
];
3874
})->toArray();
3975
}

app/Filament/Resources/ApiTokenResource.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public static function getTokenFormSchema(): array
5353
->required()
5454
->bulkToggleable()
5555
->descriptions([
56-
'results:read' => 'Allow this token to read results.',
57-
'speedtests:run' => 'Allow this token to run speedtests.',
58-
'ookla:list-servers' => 'Allow this token to list servers.',
56+
'results:read' => 'Grant this token permission to read results and statistics.',
57+
'speedtests:run' => 'Grant this token permission to run speedtests.',
58+
'ookla:list-servers' => 'Grant this token permission to list available servers.',
5959
]),
6060
DateTimePicker::make('expires_at')
6161
->label('Expires at')

app/Http/Controllers/Api/V1/LatestResult.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

app/Http/Controllers/Api/V1/ListResults.php

Lines changed: 0 additions & 86 deletions
This file was deleted.

app/Http/Controllers/Api/V1/ListSpeedtestServers.php

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\V1;
4+
5+
use App\Actions\GetOoklaSpeedtestServers;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Http\Response;
8+
9+
class OoklaController extends ApiController
10+
{
11+
/**
12+
* GET /api/v1/ookla/list-servers
13+
* List available Ookla speedtest servers.
14+
*/
15+
public function __invoke(Request $request)
16+
{
17+
if ($request->user()->tokenCant('ookla:list-servers')) {
18+
return $this->sendResponse(
19+
data: null,
20+
message: 'You do not have permission to view speedtest servers.',
21+
code: Response::HTTP_FORBIDDEN,
22+
);
23+
}
24+
25+
$servers = GetOoklaSpeedtestServers::forApi();
26+
27+
return $this->sendResponse(
28+
data: $servers,
29+
message: 'Speedtest servers fetched successfully.'
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)