diff --git a/.env.example b/.env.example index dd29c90472..2dc2da7029 100644 --- a/.env.example +++ b/.env.example @@ -77,6 +77,11 @@ RECAPTCHAV3_SECRET= OTP_EXPIRY_MINUTES=5 TELEGRAM_BOT_TOKEN= +# Batas waktu pemanggilan HTTP keluar (detik), lihat config/http.php +HTTP_TIMEOUT=10 +HTTP_CONNECT_TIMEOUT=5 +HTTP_TTE_TIMEOUT=60 + # Scribe / OpenAPI # SCRIBE_AUTH_KEY=your-api-key-here # pengaturan cache untuk API, jika ingin menonaktifkan cache, ubah API_CACHE_ENABLED menjadi false diff --git a/app/Http/Controllers/Api/SuratController.php b/app/Http/Controllers/Api/SuratController.php index 86f135dd27..8ad104d8af 100644 --- a/app/Http/Controllers/Api/SuratController.php +++ b/app/Http/Controllers/Api/SuratController.php @@ -31,13 +31,13 @@ namespace App\Http\Controllers\Api; +use App\Support\OutboundHttp; use App\Enums\StatusVerifikasiSurat; use App\Http\Controllers\Controller; use App\Http\Resources\SuratResource; use App\Models\DataDesa; use App\Models\Penduduk; use App\Models\Surat; -use Http; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Log; @@ -130,7 +130,7 @@ public function store(Request $request) // jika api gabungan aktif, maka pengecekan penduduk melalui api gabungan if ($this->isDatabaseGabungan()) { $url = $this->settings['api_server_database_gabungan'].'/api/v1/opendk'; - $response = Http::withHeaders([ + $response = OutboundHttp::client()->withHeaders([ 'Accept' => 'application/ld+json', 'Content-Type' => 'application/json; charset=utf-8', 'Authorization' => 'Bearer ' . ($this->settings['api_key_database_gabungan'] ?? ''), diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 46117ca27b..8e18db13f1 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -31,34 +31,30 @@ namespace App\Http\Controllers; +use App\Traits\KirimTrackPantau; use App\Enums\JenisJabatan; -use App\Models\DataDesa; use App\Models\DataUmum; use App\Models\Event; use App\Models\Jabatan; -use App\Models\Keluarga; use App\Models\MediaSosial; use App\Models\Navigation; -use App\Models\Penduduk; use App\Models\Pengurus; use App\Models\Profil; -use App\Models\Program; use App\Models\SettingAplikasi; use App\Models\SinergiProgram; use App\Models\Slide; use App\Models\TipePotensi; use App\Services\DesaService; -use Exception; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Routing\Controller as BaseController; -use Illuminate\Support\Facades\Http; -use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\View; class Controller extends BaseController { + use KirimTrackPantau; + use AuthorizesRequests; use DispatchesJobs; use ValidatesRequests; @@ -151,60 +147,6 @@ public function __construct() } } - protected function kirimTrack() - { - if (config('app.demo') == true) { // jika posisi demo, matikan tracking - return; - } - - if (cache()->get('track') != null && cache()->get('track') == date('Y m d')) { - return; - } - - $host_pantau = config('app.host_pantau'); - $data = [ - 'url' => url('/'), - 'versi' => config('app.version'), - 'jml_desa' => DataDesa::count(), - 'desa' => json_encode(DataDesa::select(['desa_id', 'nama', 'sebutan_desa', 'path', 'website'])->get()), - 'jumlahdesa_sinkronisasi' => DataDesa::count(), - 'jumlah_penduduk' => Penduduk::where('status_dasar', 1)->count(), - 'jumlah_keluarga' => Keluarga::count(), - 'peta_wilayah' => $this->umum->path ?? '[[[[]]]]', - 'batas_wilayah' => json_encode([ - 'bts_wil_utara' => $this->umum->bts_wil_utara, - 'bts_wil_timur' => $this->umum->bts_wil_timur, - 'bts_wil_selatan' => $this->umum->bts_wil_selatan, - 'bts_wil_barat' => $this->umum->bts_wil_barat, - ]), - 'sebutan_wilayah' => $this->sebutan_wilayah, - 'alamat' => $this->profil->alamat, - 'jumlah_bantuan' => Program::count(), - 'kode_kecamatan' => $this->profil->kecamatan_id, - 'kode_kabupaten' => $this->profil->kabupaten_id, - 'kode_provinsi' => $this->profil->provinsi_id, - 'nama_kecamatan' => $this->profil->nama_kecamatan, - 'nama_kabupaten' => $this->profil->nama_kabupaten, - 'nama_provinsi' => $this->profil->nama_provinsi, - 'nama_camat' => $this->nama_camat, - 'lat' => $this->umum->lat, - 'lng' => $this->umum->lng, - ]; - - try { - $response = Http::withHeaders([ - 'token' => config('app.token_pantau'), - ])->post($host_pantau.'track/opendk?token='.config('app.token_pantau'), $data); - cache()->put('track', date('Y m d'), 60 * 60 * 24); - - return; - } catch (Exception $e) { - Log::notice($e); - - return; - } - } - protected function isDatabaseGabungan() { return ($this->settings['sinkronisasi_database_gabungan'] ?? null) === '1'; diff --git a/app/Http/Controllers/Data/DataDesaController.php b/app/Http/Controllers/Data/DataDesaController.php index 8e544da2a2..d34d335177 100644 --- a/app/Http/Controllers/Data/DataDesaController.php +++ b/app/Http/Controllers/Data/DataDesaController.php @@ -31,6 +31,7 @@ namespace App\Http\Controllers\Data; +use App\Support\OutboundHttp; use App\Exports\ExportDataDesa; use App\Http\Controllers\Controller; use App\Http\Requests\DesaRequest; @@ -49,7 +50,7 @@ class DataDesaController extends Controller public function __construct() { parent::__construct(); - $this->client = new Client(); + $this->client = new Client(OutboundHttp::guzzleOptions()); } /** diff --git a/app/Http/Controllers/Surat/PermohonanController.php b/app/Http/Controllers/Surat/PermohonanController.php index d101b9a0e0..ea9dd73392 100644 --- a/app/Http/Controllers/Surat/PermohonanController.php +++ b/app/Http/Controllers/Surat/PermohonanController.php @@ -38,6 +38,7 @@ use App\Models\DataDesa; use App\Models\LogTte; use App\Models\Surat; +use App\Support\OutboundHttp; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Psr7; use Illuminate\Http\Request; @@ -223,22 +224,22 @@ public function passphrase(Request $request, $id) { $surat = Surat::findOrFail($id); - DB::beginTransaction(); - - $client = new \GuzzleHttp\Client([ + // Panggilan ke server TTE dilakukan di luar transaksi DB agar server TTE yang lambat + // tidak menahan transaksi database. + $client = new \GuzzleHttp\Client(OutboundHttp::tteGuzzleOptions([ 'base_uri' => $this->settings['tte_api'], 'auth' => [ $this->settings['tte_username'], $this->settings['tte_password'], ], - ]); + ])); - try { - $file_path = public_path("storage/surat/{$surat->file}"); - $width = 90; - $height = 90; - $tag = '[qr_camat]'; + $file_path = public_path("storage/surat/{$surat->file}"); + $width = 90; + $height = 90; + $tag = '[qr_camat]'; + try { $response = $client->post('api/sign/pdf', [ 'headers' => ['X-Requested-With' => 'XMLHttpRequest'], 'multipart' => [ @@ -252,26 +253,6 @@ public function passphrase(Request $request, $id) ['name' => 'tag_koordinat', 'contents' => $tag], ], ]); - - $surat->update([ - 'status' => StatusSurat::Arsip, - 'log_verifikasi' => LogVerifikasiSurat::SudahTTE, - ]); - - DB::commit(); - - // overwrite dokumen lama dengan response dari bsre - if ($response->getStatusCode() == 200) { - $file = fopen($file_path, 'wb'); - fwrite($file, $response->getBody()->getContents()); - fclose($file); - } - - return $this->response([ - 'status' => true, - 'pesan_error' => 'success', - 'jenis' => 'success', - ]); } catch (ClientException $e) { Log::error('Permohonan Surat TTE signing failed', [ 'error' => $e->getMessage(), @@ -279,14 +260,39 @@ public function passphrase(Request $request, $id) 'surat_id' => $id, ]); - DB::rollback(); - return $this->response([ 'status' => false, 'pesan_error' => $e->getMessage(), 'jenis' => 'ClientException', ]); } + + DB::beginTransaction(); + try { + $surat->update([ + 'status' => StatusSurat::Arsip, + 'log_verifikasi' => LogVerifikasiSurat::SudahTTE, + ]); + + DB::commit(); + } catch (\Throwable $e) { + DB::rollBack(); + + throw $e; + } + + // overwrite dokumen lama dengan response dari bsre + if ($response->getStatusCode() == 200) { + $file = fopen($file_path, 'wb'); + fwrite($file, $response->getBody()->getContents()); + fclose($file); + } + + return $this->response([ + 'status' => true, + 'pesan_error' => 'success', + 'jenis' => 'success', + ]); } protected function response($notif = []) diff --git a/app/Http/Middleware/GlobalShareMiddleware.php b/app/Http/Middleware/GlobalShareMiddleware.php index 0b6bf4f239..b216239972 100644 --- a/app/Http/Middleware/GlobalShareMiddleware.php +++ b/app/Http/Middleware/GlobalShareMiddleware.php @@ -2,28 +2,24 @@ namespace App\Http\Middleware; +use App\Traits\KirimTrackPantau; use App\Enums\JenisJabatan; -use App\Models\DataDesa; use App\Models\DataUmum; use App\Models\Jabatan; -use App\Models\Keluarga; -use App\Models\Penduduk; use App\Models\Pengurus; use App\Models\Profil; -use App\Models\Program; use App\Models\SettingAplikasi; use App\Models\TipePotensi; use App\Models\Widget; use App\Services\DesaService; use Closure; -use Exception; use Illuminate\Support\Facades\View; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Http; -use Illuminate\Support\Facades\Log; class GlobalShareMiddleware { + use KirimTrackPantau; + protected $akun_camat; protected $akun_sekretaris; @@ -134,58 +130,4 @@ private function widgetAktif() return $item; }); } - - protected function kirimTrack() - { - if (config('app.demo') == true) { // jika posisi demo, matikan tracking - return; - } - - if (cache()->get('track') != null && cache()->get('track') == date('Y m d')) { - return; - } - - $host_pantau = config('app.host_pantau'); - $data = [ - 'url' => url('/'), - 'versi' => config('app.version'), - 'jml_desa' => DataDesa::count(), - 'desa' => json_encode(DataDesa::select(['desa_id', 'nama', 'sebutan_desa', 'path', 'website'])->get()), - 'jumlahdesa_sinkronisasi' => DataDesa::count(), - 'jumlah_penduduk' => Penduduk::where('status_dasar', 1)->count(), - 'jumlah_keluarga' => Keluarga::count(), - 'peta_wilayah' => $this->umum->path ?? '[[[[]]]]', - 'batas_wilayah' => json_encode([ - 'bts_wil_utara' => $this->umum->bts_wil_utara, - 'bts_wil_timur' => $this->umum->bts_wil_timur, - 'bts_wil_selatan' => $this->umum->bts_wil_selatan, - 'bts_wil_barat' => $this->umum->bts_wil_barat, - ]), - 'sebutan_wilayah' => $this->sebutan_wilayah, - 'alamat' => $this->profil->alamat, - 'jumlah_bantuan' => Program::count(), - 'kode_kecamatan' => $this->profil->kecamatan_id, - 'kode_kabupaten' => $this->profil->kabupaten_id, - 'kode_provinsi' => $this->profil->provinsi_id, - 'nama_kecamatan' => $this->profil->nama_kecamatan, - 'nama_kabupaten' => $this->profil->nama_kabupaten, - 'nama_provinsi' => $this->profil->nama_provinsi, - 'nama_camat' => $this->nama_camat, - 'lat' => $this->umum->lat, - 'lng' => $this->umum->lng, - ]; - - try { - $response = Http::withHeaders([ - 'token' => config('app.token_pantau'), - ])->post($host_pantau . 'track/opendk?token=' . config('app.token_pantau'), $data); - cache()->put('track', date('Y m d'), 60 * 60 * 24); - - return; - } catch (Exception $e) { - Log::notice($e); - - return; - } - } } diff --git a/app/Services/ApiService.php b/app/Services/ApiService.php index ccf628662b..bde4703821 100644 --- a/app/Services/ApiService.php +++ b/app/Services/ApiService.php @@ -2,9 +2,9 @@ namespace App\Services; +use App\Support\OutboundHttp; use App\Models\SettingAplikasi; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Storage; use GuzzleHttp\Client; use GuzzleHttp\Psr7; @@ -26,7 +26,7 @@ public function __construct() public function register($data) { - $response = Http::withHeaders([ + $response = OutboundHttp::client()->withHeaders([ 'X-Requested-With' => 'XMLHttpRequest', ])->timeout(10)->connectTimeout(5)->attach( 'permohonan', @@ -67,7 +67,7 @@ public function register($data) public function terdaftar($kode_kecamatan) { - $response = Http::withHeaders([ + $response = OutboundHttp::client()->withHeaders([ 'Authorization' => 'Bearer ' . $this->setting->layanan_opendesa_token, 'X-Requested-With' => 'XMLHttpRequest', ])->timeout(10)->connectTimeout(5)->post("{$this->server}/api/v1/pelanggan/terdaftar-kecamatan", [ @@ -92,7 +92,7 @@ public function terdaftar($kode_kecamatan) public function getFormRegister() { - $response = Http::withHeaders([ + $response = OutboundHttp::client()->withHeaders([ 'Authorization' => 'Bearer ' . $this->setting->layanan_opendesa_token, 'X-Requested-With' => 'XMLHttpRequest', ])->timeout(10)->connectTimeout(5)->get("{$this->server}/api/v1/pelanggan/form-register-kecamatan"); diff --git a/app/Services/BaseApiService.php b/app/Services/BaseApiService.php index 13ac15f86d..615fbac3e7 100644 --- a/app/Services/BaseApiService.php +++ b/app/Services/BaseApiService.php @@ -2,8 +2,8 @@ namespace App\Services; +use App\Support\OutboundHttp; use App\Models\SettingAplikasi; -use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class BaseApiService @@ -52,7 +52,7 @@ protected function apiRequest(string $endpoint, array $params = [], string $meth { try { // Buat permintaan API dengan Header dan Parameter - $response = Http::withHeaders($this->header)->{$this->resolveMethod($method)}($this->baseUrl . $endpoint, $params); + $response = OutboundHttp::client()->withHeaders($this->header)->{$this->resolveMethod($method)}($this->baseUrl . $endpoint, $params); session()->forget('error_api'); // Jika response gagal (non-2xx), kembalikan array kosong diff --git a/app/Services/KeluargaService.php b/app/Services/KeluargaService.php index bd2ef6aa04..07ed5a19ea 100644 --- a/app/Services/KeluargaService.php +++ b/app/Services/KeluargaService.php @@ -3,7 +3,6 @@ namespace App\Services; use App\Models\SettingAplikasi; -use Illuminate\Support\Facades\Http; class KeluargaService extends BaseApiService { diff --git a/app/Services/LaporanPendudukService.php b/app/Services/LaporanPendudukService.php index d5b63ebed8..76c0210452 100644 --- a/app/Services/LaporanPendudukService.php +++ b/app/Services/LaporanPendudukService.php @@ -2,8 +2,8 @@ namespace App\Services; +use App\Support\OutboundHttp; use App\Models\SettingAplikasi; -use Illuminate\Support\Facades\Http; class LaporanPendudukService { @@ -23,7 +23,7 @@ protected function apiRequest(string $endpoint, array $params = []) $baseUrl = $this->settings['api_server_database_gabungan']; // Buat permintaan API dengan Header dan Parameter - $response = Http::withHeaders([ + $response = OutboundHttp::client()->withHeaders([ 'Accept' => 'application/ld+json', 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $this->settings['api_key_database_gabungan'], diff --git a/app/Services/OtpService.php b/app/Services/OtpService.php index 00ff583d47..7152f6f8f3 100644 --- a/app/Services/OtpService.php +++ b/app/Services/OtpService.php @@ -31,12 +31,12 @@ namespace App\Services; +use App\Support\OutboundHttp; use App\Models\OtpToken; use App\Models\User; use App\Mail\OtpMail; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Mail; -use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; use Carbon\Carbon; @@ -130,7 +130,7 @@ public function sendViaTelegram(string $chatId, int $otp, string $purpose = 'log $message = $this->formatTelegramMessage($otp, $purpose); - $response = Http::post("https://api.telegram.org/bot{$botToken}/sendMessage", [ + $response = OutboundHttp::client()->post("https://api.telegram.org/bot{$botToken}/sendMessage", [ 'chat_id' => $chatId, 'text' => $message, 'parse_mode' => 'HTML', @@ -296,7 +296,7 @@ public function verifyTelegramChatId(string $chatId): bool return false; } - $response = Http::post("https://api.telegram.org/bot{$botToken}/getChat", [ + $response = OutboundHttp::client()->post("https://api.telegram.org/bot{$botToken}/getChat", [ 'chat_id' => $chatId, ]); diff --git a/app/Services/StatistikChartPendudukService.php b/app/Services/StatistikChartPendudukService.php index d4613d95bb..94edff2770 100644 --- a/app/Services/StatistikChartPendudukService.php +++ b/app/Services/StatistikChartPendudukService.php @@ -31,6 +31,7 @@ namespace App\Services; +use App\Support\OutboundHttp; use App\Models\Penduduk; use Illuminate\Support\Facades\Log; use GuzzleHttp\Client; @@ -50,7 +51,7 @@ public function chart($did, $listYears) if($did != 'Semua') { $filters['filter[desa]'] = $did; } - $client = new Client(['base_uri' => $this->baseUrl, 'headers' => $this->header]); + $client = new Client(OutboundHttp::guzzleOptions(['base_uri' => $this->baseUrl, 'headers' => $this->header])); $requests = []; foreach ($listYears as $year) { $filters['filter[tahun]'] = $year; diff --git a/app/Services/StatistikChartTingkatPendidikanService.php b/app/Services/StatistikChartTingkatPendidikanService.php index 3b6e97d7dc..cefc7b1560 100644 --- a/app/Services/StatistikChartTingkatPendidikanService.php +++ b/app/Services/StatistikChartTingkatPendidikanService.php @@ -31,6 +31,7 @@ namespace App\Services; +use App\Support\OutboundHttp; use App\Enums\LabelStatistik; use App\Models\DataDesa; use GuzzleHttp\Client; @@ -70,7 +71,7 @@ private function dataGabungan($did, $year) private function dataGabunganSemua() { - $client = new Client(['base_uri' => $this->baseUrl, 'headers' => $this->header]); + $client = new Client(OutboundHttp::guzzleOptions(['base_uri' => $this->baseUrl, 'headers' => $this->header])); $requests = []; $dataPendidikan = []; $filters = [ @@ -97,7 +98,7 @@ private function dataGabunganSemua() private function dataGabunganSemuaDesa($year) { - $client = new Client(['base_uri' => $this->baseUrl, 'headers' => $this->header]); + $client = new Client(OutboundHttp::guzzleOptions(['base_uri' => $this->baseUrl, 'headers' => $this->header])); $requests = []; $dataPendidikan = []; $filters = [ @@ -145,7 +146,7 @@ private function dataGabunganSpesifik($did, $year) private function dataGabunganSemuaTahun($did) { - $client = new Client(['base_uri' => $this->baseUrl, 'headers' => $this->header]); + $client = new Client(OutboundHttp::guzzleOptions(['base_uri' => $this->baseUrl, 'headers' => $this->header])); $requests = []; $dataPendidikan = []; $filters = [ diff --git a/app/Services/StatistikPendudukService.php b/app/Services/StatistikPendudukService.php index a923afc4e3..29741cdcb3 100644 --- a/app/Services/StatistikPendudukService.php +++ b/app/Services/StatistikPendudukService.php @@ -31,6 +31,7 @@ namespace App\Services; +use App\Support\OutboundHttp; use App\Enums\LabelStatistik; use App\Models\Penduduk; use GuzzleHttp\Client; @@ -115,7 +116,7 @@ public function dashboard($did, $year) $filtersAkta['filter[id]'] = 'akta-kelahiran'; $filtersAktaNikah['filter[id]'] = 'akta-nikah'; $filtersNikah['filter[id]'] = 'status-perkawinan'; - $client = new Client(['base_uri' => $this->baseUrl, 'headers' => $this->header]); + $client = new Client(OutboundHttp::guzzleOptions(['base_uri' => $this->baseUrl, 'headers' => $this->header])); $promises = Utils::unwrap([ 'penduduk' => $client->getAsync('/api/v1/statistik-web/penduduk', ['query' => $filters]), 'cacat' => $client->getAsync('/api/v1/statistik-web/penduduk', ['query' => $filtersCacat]), diff --git a/app/Support/OutboundHttp.php b/app/Support/OutboundHttp.php new file mode 100644 index 0000000000..6485cbb716 --- /dev/null +++ b/app/Support/OutboundHttp.php @@ -0,0 +1,58 @@ +connectTimeout(self::connectTimeout()); + } + + /** + * Opsi Guzzle dengan timeout standar. Opsi yang diberikan pemanggil tetap menang. + */ + public static function guzzleOptions(array $options = []): array + { + return array_merge([ + 'timeout' => self::timeout(), + 'connect_timeout' => self::connectTimeout(), + ], $options); + } + + /** + * Opsi Guzzle untuk server TTE, dengan timeout yang lebih panjang. + */ + public static function tteGuzzleOptions(array $options = []): array + { + return array_merge([ + 'timeout' => self::tteTimeout(), + 'connect_timeout' => self::connectTimeout(), + ], $options); + } +} diff --git a/app/Traits/KirimTrackPantau.php b/app/Traits/KirimTrackPantau.php new file mode 100644 index 0000000000..e03c3caa71 --- /dev/null +++ b/app/Traits/KirimTrackPantau.php @@ -0,0 +1,75 @@ +get('track') != null && cache()->get('track') == date('Y m d')) { + return; + } + + $host_pantau = config('app.host_pantau'); + $data = [ + 'url' => url('/'), + 'versi' => config('app.version'), + 'jml_desa' => DataDesa::count(), + 'desa' => json_encode(DataDesa::select(['desa_id', 'nama', 'sebutan_desa', 'path', 'website'])->get()), + 'jumlahdesa_sinkronisasi' => DataDesa::count(), + 'jumlah_penduduk' => Penduduk::where('status_dasar', 1)->count(), + 'jumlah_keluarga' => Keluarga::count(), + 'peta_wilayah' => $this->umum->path ?? '[[[[]]]]', + 'batas_wilayah' => json_encode([ + 'bts_wil_utara' => $this->umum->bts_wil_utara, + 'bts_wil_timur' => $this->umum->bts_wil_timur, + 'bts_wil_selatan' => $this->umum->bts_wil_selatan, + 'bts_wil_barat' => $this->umum->bts_wil_barat, + ]), + 'sebutan_wilayah' => $this->sebutan_wilayah, + 'alamat' => $this->profil->alamat, + 'jumlah_bantuan' => Program::count(), + 'kode_kecamatan' => $this->profil->kecamatan_id, + 'kode_kabupaten' => $this->profil->kabupaten_id, + 'kode_provinsi' => $this->profil->provinsi_id, + 'nama_kecamatan' => $this->profil->nama_kecamatan, + 'nama_kabupaten' => $this->profil->nama_kabupaten, + 'nama_provinsi' => $this->profil->nama_provinsi, + 'nama_camat' => $this->nama_camat, + 'lat' => $this->umum->lat, + 'lng' => $this->umum->lng, + ]; + + try { + $response = OutboundHttp::client()->withHeaders([ + 'token' => config('app.token_pantau'), + ])->post($host_pantau.'track/opendk?token='.config('app.token_pantau'), $data); + cache()->put('track', date('Y m d'), 60 * 60 * 24); + + return; + } catch (Exception $e) { + Log::notice($e); + + return; + } + } +} diff --git a/catatan_rilis.md b/catatan_rilis.md index c6ff6f1744..b7d5d4cd15 100644 --- a/catatan_rilis.md +++ b/catatan_rilis.md @@ -17,6 +17,7 @@ Terimakasih [isi disini] yang telah berkontribusi langsung mengembangkan aplikas 6. [#1802](https://github.com/OpenSID/OpenDK/issues/1802) Perbaikan Detail komplain slug tidak ada jadi 404, dan hapus 3 route user yatim. 7. [#1800](https://github.com/OpenSID/OpenDK/issues/1800) Cegah 500 pada respons non-JSON dan tambah timeout di ApiService. 8. [#1801](https://github.com/OpenSID/OpenDK/issues/1801) Route /api/desa dan /testEmail menghasilkan 500 untuk pengunjung publik. +9. [#1805](https://github.com/OpenSID/OpenDK/issues/1805) Tambahkan timeout dan penanganan error pada pemanggilan HTTP di luar ApiService. #### TEKNIS diff --git a/config/http.php b/config/http.php new file mode 100644 index 0000000000..08dda43167 --- /dev/null +++ b/config/http.php @@ -0,0 +1,22 @@ + (int) env('HTTP_TIMEOUT', 10), + + 'connect_timeout' => (int) env('HTTP_CONNECT_TIMEOUT', 5), + + // Server TTE dapat lambat, sehingga memakai batas waktu yang lebih panjang. + 'tte_timeout' => (int) env('HTTP_TTE_TIMEOUT', 60), + +]; diff --git a/tests/Unit/Support/OutboundHttpTest.php b/tests/Unit/Support/OutboundHttpTest.php new file mode 100644 index 0000000000..cc26428682 --- /dev/null +++ b/tests/Unit/Support/OutboundHttpTest.php @@ -0,0 +1,49 @@ +toBe(10) + ->and(OutboundHttp::connectTimeout())->toBe(5) + ->and(OutboundHttp::tteTimeout())->toBe(60); +}); + +it('applies timeout and connect timeout to the pending http request', function () { + $options = OutboundHttp::client()->getOptions(); + + expect($options['timeout'])->toBe(10) + ->and($options['connect_timeout'])->toBe(5); +}); + +it('merges timeouts into guzzle options without overriding explicit values', function () { + $options = OutboundHttp::guzzleOptions([ + 'base_uri' => 'https://example.test', + 'timeout' => 3, + ]); + + expect($options)->toMatchArray([ + 'base_uri' => 'https://example.test', + 'timeout' => 3, + 'connect_timeout' => 5, + ]); +}); + +it('uses the longer tte timeout for the tte guzzle options', function () { + $options = OutboundHttp::tteGuzzleOptions(['base_uri' => 'https://tte.test']); + + expect($options)->toMatchArray([ + 'base_uri' => 'https://tte.test', + 'timeout' => 60, + 'connect_timeout' => 5, + ]); +}); + +it('sends requests through the shared client without failing on non-json errors', function () { + Http::fake(['*' => Http::response('Bad Gateway', 502)]); + + $response = OutboundHttp::client()->get('https://example.test/ping'); + + expect($response->failed())->toBeTrue() + ->and($response->json('message'))->toBeNull(); +});