forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetOoklaSpeedtestServers.php
More file actions
41 lines (34 loc) · 1.11 KB
/
GetOoklaSpeedtestServers.php
File metadata and controls
41 lines (34 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace App\Actions;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Lorisleiva\Actions\Concerns\AsAction;
class GetOoklaSpeedtestServers
{
use AsAction;
public function handle(): array
{
$query = [
'engine' => 'js',
'https_functional' => true,
'limit' => 20,
];
$response = Http::retry(3, 250)
->timeout(5)
->get(url: 'https://www.speedtest.net/api/js/servers', query: $query)
->throw(function (Response $response, RequestException $e) {
Log::error($e);
return [
'0' => 'There was an issue retrieving Ookla speedtest servers, check the logs for more info.',
];
})
->collect();
return $response->mapWithKeys(function (array $item, int $key) {
return [
$item['id'] => $item['id'].': '.$item['name'].' ('.$item['sponsor'].')',
];
})->toArray();
}
}