-
-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathTrueNASApiTrait.php
More file actions
238 lines (206 loc) · 6.92 KB
/
TrueNASApiTrait.php
File metadata and controls
238 lines (206 loc) · 6.92 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
namespace App\SupportedApps\TrueNASCORE;
use App\Helpers\TrueNASWebSocketClient;
use Illuminate\Support\Facades\Log;
/**
* Shared trait for TrueNAS API communication.
*
* Provides WebSocket (JSON-RPC 2.0) and REST API support with automatic fallback.
* Required for TrueNAS 25.04+ as the REST API is deprecated in 26.04.
*/
trait TrueNASApiTrait
{
private ?TrueNASWebSocketClient $wsClient = null;
private ?string $lastApiMode = null;
/**
* Get the configured API mode.
*
* @return string 'auto', 'websocket', or 'rest'
*/
public function getApiMode(): string
{
return $this->getConfigValue('api_mode', 'auto');
}
/**
* Check if WebSocket library is available.
*
* @return bool
*/
public function isWebSocketAvailable(): bool
{
return class_exists('WebSocket\Client');
}
/**
* Make an API call using the configured transport.
*
* @param string $method The method name (e.g., 'system.info' or 'alert.list')
* @param array $params Optional parameters
* @return mixed The result from the API
* @throws \Exception If the call fails
*/
public function apiCall(string $method, array $params = [])
{
$mode = $this->getApiMode();
if ($mode === 'websocket') {
return $this->callWebSocket($method, $params);
}
if ($mode === 'rest') {
return $this->callRest($method, $params);
}
// Auto mode: try WebSocket first, fall back to REST
if ($this->isWebSocketAvailable()) {
try {
return $this->callWebSocket($method, $params);
} catch (\Exception $e) {
Log::debug('WebSocket failed, falling back to REST: ' . $e->getMessage());
}
}
return $this->callRest($method, $params);
}
/**
* Make a WebSocket JSON-RPC 2.0 call.
*
* @param string $method The JSON-RPC method name
* @param array $params Optional parameters
* @return mixed The result
* @throws \Exception If the call fails
*/
private function callWebSocket(string $method, array $params = [])
{
$this->ensureWebSocketConnected();
$this->lastApiMode = 'websocket';
return $this->wsClient->call($method, $params);
}
/**
* Make a REST API call.
*
* @param string $method The method name (will be converted to REST endpoint)
* @param array $params Optional parameters (sent as JSON body for POST)
* @return mixed The result
* @throws \Exception If the call fails
*/
private function callRest(string $method, array $params = [])
{
$this->lastApiMode = 'rest';
// Map JSON-RPC methods to REST endpoints
$endpointMap = [
'core.ping' => 'core/ping',
'system.info' => 'system/info',
'alert.list' => 'alert/list',
];
$endpoint = $endpointMap[$method] ?? str_replace('.', '/', $method);
$url = $this->url($endpoint);
$attrs = $this->attrs();
if (!empty($params)) {
$attrs['json'] = $params;
}
$res = parent::execute($url, $attrs);
if ($res === null) {
throw new \Exception('REST API call failed');
}
$body = $res->getBody()->getContents();
// Handle simple string responses (like "pong")
$decoded = json_decode($body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
// Return raw response for non-JSON (e.g., "pong" string)
return trim($body, '"');
}
return $decoded;
}
/**
* Ensure WebSocket client is connected.
*
* @throws \Exception If connection fails
*/
private function ensureWebSocketConnected(): void
{
if ($this->wsClient !== null && $this->wsClient->isConnected()) {
return;
}
if (!$this->isWebSocketAvailable()) {
throw new \Exception('WebSocket library not available');
}
$baseUrl = parent::normaliseurl($this->config->url, false);
$apiKey = $this->config->apikey;
$ignoreTls = $this->getConfigValue('ignore_tls', false);
$this->wsClient = new TrueNASWebSocketClient($baseUrl, $apiKey, $ignoreTls);
$this->wsClient->connect();
}
/**
* Close WebSocket connection if open.
*/
public function disconnectWebSocket(): void
{
if ($this->wsClient !== null) {
$this->wsClient->disconnect();
$this->wsClient = null;
}
}
/**
* Test API connection.
*
* @return object Test result with code, status, and response
*/
public function testApi(): object
{
if (empty($this->config->url)) {
return (object) [
'code' => 404,
'status' => 'No URL has been specified',
'response' => 'No URL has been specified',
];
}
$mode = $this->getApiMode();
// Try WebSocket if enabled
if ($mode !== 'rest' && $this->isWebSocketAvailable()) {
try {
$this->ensureWebSocketConnected();
$result = $this->wsClient->ping();
if ($result) {
return (object) [
'code' => 200,
'status' => 'Successfully communicated with the API (WebSocket)',
'response' => 'pong',
];
}
} catch (\Exception $e) {
if ($mode === 'websocket') {
return (object) [
'code' => null,
'status' => 'WebSocket connection failed: ' . $e->getMessage(),
'response' => 'Connection failed',
];
}
Log::debug('WebSocket test failed, trying REST: ' . $e->getMessage());
} finally {
$this->disconnectWebSocket();
}
}
// Try REST if WebSocket failed or not available
if ($mode !== 'websocket') {
$test = parent::appTest($this->url('core/ping'), $this->attrs());
if ($test->code === 200) {
if ($test->response != '"pong"') {
$test->status = 'Failed: ' . $test->response;
} else {
$test->status = 'Successfully communicated with the API (REST)';
}
}
return $test;
}
return (object) [
'code' => null,
'status' => 'WebSocket not available and REST mode disabled',
'response' => 'Connection failed',
];
}
/**
* Get the API mode that was used for the last call.
*
* @return string|null 'websocket' or 'rest', or null if no call made
*/
public function getLastApiMode(): ?string
{
return $this->lastApiMode;
}
}