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
84 lines (71 loc) · 2.6 KB
/
GetOoklaSpeedtestServers.php
File metadata and controls
84 lines (71 loc) · 2.6 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace App\Actions;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Lorisleiva\Actions\Concerns\AsAction;
use Throwable;
class GetOoklaSpeedtestServers
{
use AsAction;
/**
* For UI: return the ID, Sponsor, and Name to start a manual test
*/
public function handle(): array
{
$servers = self::fetch();
// If the first item is not an array, treat as error or empty
if (empty($servers) || ! is_array($servers) || (isset($servers[0]) && ! is_array($servers[0]))) {
// Return error message as a single option so user can see the error
return ['error' => $servers[0] ?? 'Unable to retrieve servers'];
}
return collect($servers)->mapWithKeys(function (array $item) {
return [
$item['id'] => ($item['sponsor'] ?? 'Unknown').' ('.($item['name'] ?? 'Unknown').', '.$item['id'].')',
];
})->toArray();
}
/**
* Fetch the raw Ookla server array from the Ookla API.
*/
public static function fetch(): array
{
$query = [
'engine' => 'js',
'https_functional' => true,
'limit' => 20,
];
try {
$response = Http::retry(3, 250)
->timeout(5)
->get(url: 'https://www.speedtest.net/api/js/servers', query: $query);
return $response->json();
} catch (Throwable $e) {
Log::error('Unable to retrieve Ookla servers.', [$e->getMessage()]);
return [
'⚠️ Unable to retrieve Ookla servers, check internet connection and see logs.',
];
}
}
/**
* For API: return array of structured server objects
*/
public static function forApi(): array
{
$servers = self::fetch();
// If the first item is not an array, treat as error or empty
if (empty($servers) || ! is_array($servers) || (isset($servers[0]) && ! is_array($servers[0]))) {
// Optionally, you could return an error message here, but to match the controller's behavior, return an empty array
return [];
}
return collect($servers)->map(function (array $item) {
return [
'id' => $item['id'],
'host' => Arr::get($item, 'host', 'Unknown'),
'name' => Arr::get($item, 'sponsor', 'Unknown'),
'location' => Arr::get($item, 'name', 'Unknown'),
'country' => Arr::get($item, 'country', 'Unknown'),
];
})->toArray();
}
}