feat: improve coingecko JSON response handling
Updated to always output JSON in response, ensuring the latest data is provided to the client. Previously, data output was conditional based on the update interval. This change guarantees a more consistent API behavior and better user experience by always serving the current state, even if no recent update occurred.
This commit is contained in:
parent
9a09f86460
commit
3a32bb2fd5
1 changed files with 7 additions and 3 deletions
|
@ -8,6 +8,8 @@ $excludedCurrencies = ['bits', 'sats'];
|
||||||
|
|
||||||
// Fetch the previously stored data
|
// Fetch the previously stored data
|
||||||
$previousData = json_decode(file_get_contents("coingecko.json"), true);
|
$previousData = json_decode(file_get_contents("coingecko.json"), true);
|
||||||
|
$output = $previousData;
|
||||||
|
|
||||||
$currentTime = time();
|
$currentTime = time();
|
||||||
|
|
||||||
// Check if five seconds have passed since the last update
|
// Check if five seconds have passed since the last update
|
||||||
|
@ -62,7 +64,9 @@ if (($currentTime - $previousData['time']) >= 5) {
|
||||||
file_put_contents("coingecko.json", json_encode($newData, JSON_PRETTY_PRINT));
|
file_put_contents("coingecko.json", json_encode($newData, JSON_PRETTY_PRINT));
|
||||||
file_put_contents("coingecko-original.json", json_encode($moneroData, JSON_PRETTY_PRINT));
|
file_put_contents("coingecko-original.json", json_encode($moneroData, JSON_PRETTY_PRINT));
|
||||||
|
|
||||||
echo "Data updated successfully.";
|
$output = $newData;
|
||||||
} else {
|
|
||||||
echo "No data update needed. Last update was less than five seconds ago.";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Output the data
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode($newData, JSON_PRETTY_PRINT);
|
Loading…
Reference in a new issue