104 lines
3.5 KiB
PHP
104 lines
3.5 KiB
PHP
<?php
|
|
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
|
|
require_once __DIR__ . '/class/VisualLimsApiClient.class.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Disable PHP error display
|
|
ini_set('display_errors', '0');
|
|
error_reporting(E_ALL);
|
|
|
|
try {
|
|
$api = VisualLimsApiClient::getInstance(); // also loads dotenv
|
|
|
|
// In simulate mode: return fake clients built from idclient values already in datadb.
|
|
if (($_ENV['SIMULATE_EXPORT_LIMS'] ?? '') === 'true') {
|
|
require_once __DIR__ . '/class/db-functions.php';
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
$stmt = $pdo->query("
|
|
SELECT DISTINCT idclient
|
|
FROM datadb
|
|
WHERE idclient IS NOT NULL
|
|
AND idclient > 0
|
|
ORDER BY idclient ASC
|
|
");
|
|
|
|
$ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
$fakeClients = array_map(fn($id) => [
|
|
'IdCliente' => (int) $id,
|
|
'Nominativo' => "Cliente Simulato {$id}",
|
|
'CodiceCliente' => "SIM_{$id}",
|
|
], $ids);
|
|
|
|
echo json_encode(['value' => $fakeClients], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
// Endpoint senza filtri: recupera tutto
|
|
$endpoint = 'Cliente?$top=50';
|
|
|
|
// Funzione per eseguire la chiamata con retry
|
|
function makeApiRequest($api, $endpoint, $maxRetries = 3)
|
|
{
|
|
for ($retry = 0; $retry < $maxRetries; $retry++) {
|
|
try {
|
|
$data = $api->get($endpoint);
|
|
|
|
// Save response for debug
|
|
file_put_contents(
|
|
__DIR__ . '/clienti_response.json',
|
|
json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
|
|
);
|
|
|
|
return $data;
|
|
} catch (Exception $e) {
|
|
$errorMessage = $e->getMessage();
|
|
|
|
// Retry only for specific auth/token related issue
|
|
if (
|
|
strpos($errorMessage, 'HTTP 400') !== false &&
|
|
strpos($errorMessage, 'Cannot persist the object') !== false
|
|
) {
|
|
try {
|
|
if (method_exists($api, 'refreshToken')) {
|
|
$api->refreshToken();
|
|
error_log("Tentativo {$retry}: refresh token eseguito per endpoint {$endpoint}");
|
|
} else {
|
|
throw new Exception('Il metodo refreshToken() non esiste in VisualLimsApiClient');
|
|
}
|
|
} catch (Exception $refreshEx) {
|
|
error_log("Errore durante il refresh del token: " . $refreshEx->getMessage());
|
|
throw new Exception("Impossibile eseguire il refresh del token: " . $refreshEx->getMessage());
|
|
}
|
|
|
|
usleep(500000); // 500ms
|
|
continue;
|
|
}
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
throw new Exception("Massimo numero di tentativi raggiunto per {$endpoint}");
|
|
}
|
|
|
|
// Esegui la chiamata
|
|
$data = makeApiRequest($api, $endpoint);
|
|
|
|
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
|
|
$errorResponse = [
|
|
'error' => $e->getMessage(),
|
|
'file' => $e->getFile(),
|
|
'line' => $e->getLine(),
|
|
'trace' => $e->getTraceAsString()
|
|
];
|
|
|
|
error_log("Errore in get_clienti.php: " . json_encode($errorResponse, JSON_UNESCAPED_UNICODE));
|
|
echo json_encode($errorResponse, JSON_UNESCAPED_UNICODE);
|
|
}
|