diff --git a/app/Services/ImamAVService.php b/app/Services/ImamAVService.php index 16431052..862cc589 100644 --- a/app/Services/ImamAVService.php +++ b/app/Services/ImamAVService.php @@ -695,6 +695,92 @@ class ImamAVService } } + /** + * GET terautentikasi dengan retry sekali saat token kedaluwarsa (401). + * Mengembalikan hasil decode JSON, atau null jika gagal. + */ + private function authorizedGet(string $path): ?array + { + try { + $token = $this->getBearerToken(); + + if (! $token) { + Log::error("ImamAV GET {$path}: gagal autentikasi"); + + return null; + } + + $response = $this->client->get($path, [ + 'headers' => ['Authorization' => 'Bearer '.$token], + 'http_errors' => false, + ]); + + if ($response->getStatusCode() === 401) { + Cache::forget('imamav_token'); + $newToken = $this->login(); + + if (! $newToken) { + return null; + } + + $response = $this->client->get($path, [ + 'headers' => ['Authorization' => 'Bearer '.$newToken], + 'http_errors' => false, + ]); + } + + if ($response->getStatusCode() !== 200) { + Log::error("ImamAV GET {$path} gagal. Status: ".$response->getStatusCode()); + + return null; + } + + $data = json_decode($response->getBody()->getContents(), true); + + return is_array($data) ? $data : null; + } catch (Throwable $th) { + Log::error("ImamAV GET {$path} error: ".$th->getMessage()); + + return null; + } + } + + /** Daftar posisi (kompatibel API lama /api/v1/posisi). */ + public function getPosisiLama(): ?array + { + return $this->authorizedGet('api/v1/posisi'); + } + + /** Daftar posisi (kompatibel API lama /api/v1/posisis). */ + public function getPosisis(): ?array + { + return $this->authorizedGet('api/v1/posisis'); + } + + /** Rubrik berdasarkan posisi (kompatibel API lama /api/v1/siska1/{posisi}). */ + public function getRubrikByPosisi(string $posisi): ?array + { + return $this->authorizedGet('api/v1/siska1/'.rawurlencode($posisi)); + } + + /** Rubrik berdasarkan kategori (kompatibel API lama /api/v1/siska2/{idkategori}). */ + public function getRubrikByKategori(int $idKategori): ?array + { + return $this->authorizedGet("api/v1/siska2/{$idKategori}"); + } + + /** Daftar posisi SISKA (kompatibel API lama /api/v1/siska3). */ + public function getPosisiSiska(): ?array + { + return $this->authorizedGet('api/v1/siska3'); + } + + /** Rubrik pembimbing/penguji (API lama /api/v1/sks). */ + public function getRubrikSks(): ?array + { + return $this->authorizedGet('api/v1/sks'); + } + public function attendanceSimulasi2026(string $nip, array $params = []): array { try { diff --git a/app/Services/SiskaService.php b/app/Services/SiskaService.php index c2d11f37..410ca7e7 100644 --- a/app/Services/SiskaService.php +++ b/app/Services/SiskaService.php @@ -7,7 +7,6 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ServerException; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; @@ -287,35 +286,24 @@ class SiskaService public function getPosisi(): array { return Cache::remember('siska_posisi', 3600, function () { - try { - $response = Http::get('http://172.16.40.201:9098/api/v1/siska3/'); - - return json_decode($response->getBody()->getContents(), true) ?? []; - } catch (Throwable $th) { - return []; - } + return app(ImamAVService::class)->getPosisiSiska() ?? []; }); } public function getKategoriByPosisi(string $posisi, array $exclude = []): ?array { - $isPembimbingPenguji = str_contains(strtolower($posisi), 'pembimbing') || str_contains(strtolower($posisi), 'penguji'); + // Posisi ber-"/" tidak bisa dipanggil lewat /api/v1/siska1/{posisi} (server 404), tetapi ada di /api/v1/sks + $isPembimbingPenguji = str_contains(strtolower($posisi), 'pembimbing') + || str_contains(strtolower($posisi), 'penguji') + || str_contains($posisi, '/'); $cacheKey = $isPembimbingPenguji ? 'siska_kategori_sks' : 'siska_kategori_'.md5(strtolower($posisi)); $rawData = Cache::remember($cacheKey, 3600, function () use ($posisi, $isPembimbingPenguji) { - try { - $uri = $isPembimbingPenguji - ? 'http://172.16.40.201:9098/api/v1/sks' - : "http://172.16.40.201:9098/api/v1/siska1/$posisi"; + $imamAV = app(ImamAVService::class); - $response = Http::get($uri); - - return json_decode($response->getBody()->getContents(), true) ?? []; - } catch (Throwable $th) { - Log::error('SISKA : '.$th->getMessage()); - - return []; - } + return ($isPembimbingPenguji + ? $imamAV->getRubrikSks() + : $imamAV->getRubrikByPosisi($posisi)) ?? []; }); $kategoris = collect($rawData) @@ -348,18 +336,7 @@ class SiskaService public function getRubriksByKategoriId(int $kategori_id): ?array { - try { - $uri = "http://172.16.40.201:9098/api/v1/siska2/$kategori_id"; - - $response = Http::get($uri); - $rubriks = json_decode($response->getBody()->getContents(), true); - } catch (Throwable $th) { - Log::error('SISKA: '.$th->getMessage()); - - return null; - } - - return $rubriks; + return app(ImamAVService::class)->getRubrikByKategori($kategori_id); } public function searchSks(string $keyword, string $status = 'belum-verif', int $page = 1, int $per_page = 10, ?int $id_unit = null): ?array