55 lines
1.8 KiB
PHP
55 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class TramiteController extends BaseController
|
|
{
|
|
public function tramiteEspecial(Request $request)
|
|
{
|
|
$query = $request->only(['start', 'end', 'type']);
|
|
$query = array_merge(['type' => 'api'], $query);
|
|
|
|
// Caché por 2 minutos (datos más dinámicos)
|
|
$cacheKey = "tramites_data_" . md5(serialize($query));
|
|
|
|
if ($cachedData = Cache::get($cacheKey)) {
|
|
return response()->json($cachedData, 200)
|
|
->header('Content-Type', 'application/json');
|
|
}
|
|
|
|
try {
|
|
$response = Http::timeout(20)->get('https://tramites.comalcalco.gob.mx/reporte-especial', $query);
|
|
|
|
if (!$response->successful()) {
|
|
Log::warning('Tramites service error', [
|
|
'status' => $response->status(),
|
|
'body' => $response->body(),
|
|
'query' => $query
|
|
]);
|
|
return response()->json(['error' => 'External service error'], $response->status());
|
|
}
|
|
|
|
$data = $response->json();
|
|
|
|
// Cachear por 2 minutos
|
|
Cache::put($cacheKey, $data, now()->addMinutes(2));
|
|
|
|
return response()->json($data, $response->status())
|
|
->header('Content-Type', $response->header('Content-Type', 'application/json'));
|
|
|
|
} catch (\Throwable $e) {
|
|
Log::error('Proxy error: ' . $e->getMessage(), [
|
|
'query' => $query,
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
return response()->json(['error' => 'Unable to contact external service'], 502);
|
|
}
|
|
}
|
|
}
|