Files
HickLab3D_Dashboard/app/Services/Bambuddy.php
T
Hickmeister ff9960fe06
Deploy HickLab3D Dashboard / Deploy to production (push) Successful in 1m42s
Move integration credentials into encrypted dashboard settings
2026-10-11 16:15:02 +01:00

52 lines
1.7 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use RuntimeException;
use Throwable;
class Bambuddy
{
public function __construct(private IntegrationSettings $settings) {}
public function url(): string
{
return (string) ($this->settings->for('bambuddy')['url'] ?? '');
}
public function configured(): bool
{
return filled($this->url()) && filled($this->settings->for('bambuddy')['api_key'] ?? null);
}
public function printers(): array
{
if (! $this->configured()) {
throw new RuntimeException('BamBuddy API key is not configured.');
}
$url = rtrim($this->url(), '/').'/api/v1/printers';
$apiKey = $this->settings->for('bambuddy')['api_key'];
$printers = Http::withHeaders(['X-API-Key' => $apiKey])
->acceptJson()->connectTimeout(3)->timeout(8)->get($url)->throw()->json() ?? [];
return collect($printers)->filter(fn ($printer) => is_array($printer) && isset($printer['id']))
->take(20)->map(function (array $printer) use ($apiKey): array {
try {
$status = Http::withHeaders(['X-API-Key' => $apiKey])
->acceptJson()->connectTimeout(3)->timeout(5)
->get(rtrim($this->url(), '/').'/api/v1/printers/'.(int) $printer['id'].'/status')
->throw()->json() ?? [];
return $printer + ['live' => $status];
} catch (Throwable $exception) {
report($exception);
return $printer + ['live' => []];
}
})->values()->all();
}
}