Skip to content

Commit e93e279

Browse files
authored
[Chore] Refactored getting Ookla speedtest servers (alexjustesen#1109)
1 parent 4512235 commit e93e279

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Actions;
4+
5+
use Illuminate\Http\Client\RequestException;
6+
use Illuminate\Http\Client\Response;
7+
use Illuminate\Support\Facades\Http;
8+
use Illuminate\Support\Facades\Log;
9+
use Lorisleiva\Actions\Concerns\AsAction;
10+
11+
class GetOoklaSpeedtestServers
12+
{
13+
use AsAction;
14+
15+
public function handle(): array
16+
{
17+
$query = [
18+
'engine' => 'js',
19+
'https_functional' => true,
20+
'limit' => 20,
21+
];
22+
23+
$response = Http::retry(3, 250)
24+
->timeout(5)
25+
->get(url: 'https://www.speedtest.net/api/js/servers', query: $query)
26+
->throw(function (Response $response, RequestException $e) {
27+
Log::error($e);
28+
29+
return [
30+
'0' => 'There was an issue retrieving Ookla speedtest servers, check the logs for more info.',
31+
];
32+
})
33+
->collect();
34+
35+
return $response->mapWithKeys(function (array $item, int $key) {
36+
return [
37+
$item['id'] => $item['id'].': '.$item['name'].' ('.$item['sponsor'].')',
38+
];
39+
})->toArray();
40+
}
41+
}

app/Filament/Pages/Settings/GeneralPage.php

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

33
namespace App\Filament\Pages\Settings;
44

5+
use App\Actions\GetOoklaSpeedtestServers;
56
use App\Helpers\TimeZoneHelper;
67
use App\Rules\Cron;
78
use App\Settings\GeneralSettings;
@@ -97,28 +98,8 @@ public function form(Form $form): Form
9798
->maxItems(10)
9899
->multiple()
99100
->nullable()
100-
->preload(false)
101101
->searchable()
102-
->options(function (): array {
103-
$response = Http::get(
104-
url: 'https://www.speedtest.net/api/js/servers',
105-
query: [
106-
'engine' => 'js',
107-
'https_functional' => true,
108-
'limit' => 20,
109-
]
110-
);
111-
112-
if ($response->failed()) {
113-
return [
114-
'' => 'There was an error retrieving Speedtest servers',
115-
];
116-
}
117-
118-
return $response->collect()->mapWithKeys(function (array $item, int $key) {
119-
return [$item['id'] => $item['id'].': '.$item['name'].' ('.$item['sponsor'].')'];
120-
})->toArray();
121-
})
102+
->options(GetOoklaSpeedtestServers::run())
122103
->getSearchResultsUsing(fn (string $search): array => $this->getServerSearchOptions($search))
123104
->getOptionLabelsUsing(fn (array $values): array => $this->getServerLabels($values))
124105
->columnSpan('full'),

0 commit comments

Comments
 (0)