71 lines
2.7 KiB
PHP
71 lines
2.7 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 ObrasController extends BaseController
|
|
{
|
|
public function Obras(Request $request)
|
|
{
|
|
$query = $request->only(['start', 'end', 'type', 'action']);
|
|
$query = array_merge($query, ['action' => 'index', 'type' => 'api'], $query);
|
|
|
|
// Caché por 2 minutos (datos más dinámicos)
|
|
$cacheKey = "obras_data_" . md5(serialize($query));
|
|
|
|
if ($cachedData = Cache::get($cacheKey)) {
|
|
return response()->json($cachedData, 200)
|
|
->header('Content-Type', 'application/json');
|
|
}
|
|
|
|
try {
|
|
$response = Http::timeout(5)->get('https://obras-information.comalcalco.gob.mx/api/controller.php', $query);
|
|
|
|
if (! $response->successful()) {
|
|
Log::warning('Obras service error', [
|
|
'status' => $response->status(),
|
|
'body' => $response->body(),
|
|
'query' => $query
|
|
]);
|
|
return response()->json(['error' => 'External service error'], $response->status());
|
|
}
|
|
|
|
$mainData = $response->json();
|
|
|
|
$counterQuery = array_merge($query, ['action' => 'getCounters']);
|
|
$countersResponse = Http::timeout(5)->get('https://obras-information.comalcalco.gob.mx/api/controller.php', $counterQuery);
|
|
|
|
$countersData = [];
|
|
if ($countersResponse->successful()) {
|
|
$countersData = $countersResponse->json();
|
|
}
|
|
|
|
$usersQuery = array_merge($query, ['action' => 'actionsOfSupervisors']);
|
|
$userResponse = Http::timeout(5)->get('https://obras-information.comalcalco.gob.mx/api/controller.php', $usersQuery);
|
|
|
|
$usersData = [];
|
|
if($userResponse->successful()) {
|
|
$usersData = $userResponse->json();
|
|
}
|
|
|
|
// Combinar ambas respuestas
|
|
$combinedData = array_merge($mainData, ['counters' => $countersData, 'users' => $usersData]);
|
|
|
|
// Cachear por 2 minutos
|
|
Cache::put($cacheKey, $combinedData, now()->addMinutes(2));
|
|
|
|
return response()->json($combinedData, $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);
|
|
}
|
|
}
|
|
}
|