This commit is contained in:
2025-09-03 17:21:27 +02:00
parent d5ecf0da96
commit b99533ced8
6 changed files with 19 additions and 86 deletions

View File

@ -10,23 +10,8 @@ class LxdService
$this->baseUrl = $_ENV['LXD_API_URL'] ?? 'https://localhost:8443';
}
/**
* Sends an HTTP request to the LXD API.
*
* @param string $method HTTP method (GET, POST, PUT, etc.)
* @param string $endpoint API endpoint
* @param array $body Request body (optional)
* @return array Response from the API
* @throws Exception
*/
private function request(string $method, string $endpoint, array $body = []): array {
// if (!isset($_ENV['LXD_CLIENT_CERT'], $_ENV['LXD_CLIENT_KEY'])) {
// throw new \Exception("LXD_CLIENT_CERT and LXD_CLIENT_KEY must be set in .env");
// }
$ch = curl_init("{$this->baseUrl}{$endpoint}");
private function curlInit($url, $method = '') {
$ch = curl_init($url);
// Paths to client certificate and key for TLS authentication
$clientCert = $_ENV['LXD_CLIENT_CERT'] ?? '/etc/ssl/lxdapp/client.crt';
@ -38,13 +23,25 @@ class LxdService
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($method) curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
return $ch;
}
/**
* Sends an HTTP request to the LXD API.
*
* @param string $method HTTP method (GET, POST, PUT, etc.)
* @param string $endpoint API endpoint
* @param array $body Request body (optional)
* @return array Response from the API
* @throws Exception
*/
private function request(string $method, string $endpoint, array $body = []): array {
$ch = $this->curlInit("{$this->baseUrl}{$endpoint}", $method);
if (!empty($body)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
@ -77,17 +74,7 @@ class LxdService
*/
private function requestRaw(string $endpoint): string {
$url = "{$this->baseUrl}{$endpoint}";
$ch = curl_init($url);
$clientCert = $_ENV['LXD_CLIENT_CERT'] ?? '/etc/ssl/lxdapp/client.crt';
$clientKey = $_ENV['LXD_CLIENT_KEY'] ?? '/etc/ssl/lxdapp/client.key';
curl_setopt($ch, CURLOPT_SSLCERT, $clientCert);
curl_setopt($ch, CURLOPT_SSLKEY, $clientKey);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$ch = $this->curlInit($url);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);