diff --git a/app/Services/Bambuddy.php b/app/Services/Bambuddy.php index c79b8be..07c780b 100644 --- a/app/Services/Bambuddy.php +++ b/app/Services/Bambuddy.php @@ -51,34 +51,24 @@ class Bambuddy } /** @param array $query */ - private function get(string $path, array $query = []): array + private function get(string $path, array $query = [], int $timeout = 8): array { if (! $this->configured()) { throw new RuntimeException('BamBuddy API key is not configured.'); } + $base = preg_replace('#/api(/v1)?$#', '', rtrim(trim($this->url()), '/')); - return Http::withHeaders(['X-API-Key' => $this->settings->for('bambuddy')['api_key']]) - ->acceptJson()->connectTimeout(3)->timeout(8) - ->get(rtrim($this->url(), '/').'/api/v1/'.ltrim($path, '/'), $query)->throw()->json() ?? []; + return Http::withHeaders(['X-API-Key' => trim((string) $this->settings->for('bambuddy')['api_key'])]) + ->acceptJson()->connectTimeout(3)->timeout($timeout) + ->get($base.'/api/v1/'.ltrim($path, '/'), $query)->throw()->json() ?? []; } private function fetchPrinters(): array { - $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 { + return collect($this->get('printers/'))->filter(fn ($printer) => is_array($printer) && isset($printer['id'])) + ->take(20)->map(function (array $printer): 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]; + return $printer + ['live' => $this->get('printers/'.(int) $printer['id'].'/status', timeout: 5)]; } catch (Throwable $exception) { report($exception); diff --git a/tests/Feature/BambuddyPageTest.php b/tests/Feature/BambuddyPageTest.php index 8b89455..0a3ea37 100644 --- a/tests/Feature/BambuddyPageTest.php +++ b/tests/Feature/BambuddyPageTest.php @@ -24,7 +24,7 @@ class BambuddyPageTest extends TestCase ['id' => 1, 'tray_color' => null, 'tray_type' => null, 'remain' => 0, 'exists' => false], ]]], ]), - 'https://3d.example.test/api/v1/printers' => Http::response([['id' => 1, 'name' => 'Printy Boii 2', 'model' => 'X1C']]), + 'https://3d.example.test/api/v1/printers/' => Http::response([['id' => 1, 'name' => 'Printy Boii 2', 'model' => 'X1C']]), 'https://3d.example.test/api/v1/queue/' => Http::response([ ['id' => 5, 'status' => 'pending', 'position' => 1, 'archive_name' => 'Queued_Bracket', 'completed_at' => null], ['id' => 4, 'status' => 'completed', 'position' => 0, 'archive_name' => 'Old_Job', 'completed_at' => '2026-10-01T10:00:00'], diff --git a/tests/Feature/DashboardAccessTest.php b/tests/Feature/DashboardAccessTest.php index bf354e0..427a015 100644 --- a/tests/Feature/DashboardAccessTest.php +++ b/tests/Feature/DashboardAccessTest.php @@ -34,7 +34,7 @@ class DashboardAccessTest extends TestCase ['receipt_id' => 77, 'name' => 'Buyer One', 'create_timestamp' => now()->timestamp], ]]), 'https://api.etsy.com/v3/application/shops/42/listings*' => Http::response(['count' => 12, 'results' => []]), - 'https://3d.example.test/api/v1/printers' => Http::response([['id' => 1, 'name' => 'X1 Carbon']]), + 'https://3d.example.test/api/v1/printers/' => Http::response([['id' => 1, 'name' => 'X1 Carbon']]), 'https://3d.example.test/api/v1/printers/1/status' => Http::response(['connected' => true, 'state' => 'RUNNING', 'progress' => 45]), 'https://auth.example.test/token' => Http::response(['access_token' => 'shipping-token']), 'https://shipping.example.test/api/me/orders/detail' => Http::response(['Orders' => [ diff --git a/tests/Feature/IntegrationsTest.php b/tests/Feature/IntegrationsTest.php index e2a95ff..c81d8e5 100644 --- a/tests/Feature/IntegrationsTest.php +++ b/tests/Feature/IntegrationsTest.php @@ -55,7 +55,7 @@ class IntegrationsTest extends TestCase { Http::preventStrayRequests(); Http::fake([ - 'https://3d.example.test/api/v1/printers' => Http::response([['id' => 1, 'name' => 'X1 Carbon', 'status' => 'printing']]), + 'https://3d.example.test/api/v1/printers/' => Http::response([['id' => 1, 'name' => 'X1 Carbon', 'status' => 'printing']]), 'https://3d.example.test/api/v1/printers/1/status' => Http::response(['connected' => true, 'state' => 'RUNNING', 'progress' => 45, 'temperatures' => ['nozzle' => 220, 'bed' => 60]]), ]); config()->set('services.bambuddy', ['url' => 'https://3d.example.test', 'api_key' => 'read-key']);