Update Harga Internasional
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Contracts\Cache\Repository;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Throwable;
|
||||
|
||||
class AlphaVantageMarket
|
||||
{
|
||||
public function quotes(Repository $cache): array
|
||||
{
|
||||
if (!$key = config('services.alpha_vantage.api_key')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Retain existing cache keys so cleanup does not spend another API request.
|
||||
$cacheKey = 'market.additional.v1.alpha:COFFEE';
|
||||
$lastKey = 'market.additional.last.v1.alpha:COFFEE';
|
||||
if (is_array($quotes = $cache->get($cacheKey))) {
|
||||
return $quotes;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::acceptJson()
|
||||
->withOptions(['verify' => config('services.yahoo_finance.ca_bundle') ?: true])
|
||||
->connectTimeout(3)->timeout(6)
|
||||
->get('https://www.alphavantage.co/query', [
|
||||
'function' => 'COFFEE', 'interval' => 'monthly', 'apikey' => $key,
|
||||
])->throw();
|
||||
$data = $response->json();
|
||||
if (!is_array($data) || ($data['interval'] ?? null) !== 'monthly'
|
||||
|| strtolower($data['unit'] ?? '') !== 'cents per pound' || !is_array($data['data'] ?? null)) {
|
||||
throw new \RuntimeException('Invalid Alpha Vantage response');
|
||||
}
|
||||
$latest = null;
|
||||
foreach ($data['data'] as $row) {
|
||||
if (!is_array($row) || !is_numeric($row['value'] ?? null) || !is_finite((float) $row['value']) || $row['value'] <= 0
|
||||
|| !is_string($row['date'] ?? null) || !preg_match('/^\d{4}-(0[1-9]|1[0-2])-01$/', $row['date'])) {
|
||||
continue;
|
||||
}
|
||||
if (!$latest || $row['date'] > $latest['date']) $latest = $row;
|
||||
}
|
||||
if (!$latest) throw new \RuntimeException('No valid Alpha Vantage price');
|
||||
$quote = [
|
||||
'id' => 'alpha:COFFEE', 'name' => 'Kopi Arabica',
|
||||
'source' => 'Alpha Vantage (data IMF via FRED)',
|
||||
'source_url' => 'https://www.alphavantage.co/documentation/#coffee',
|
||||
'price' => (float) $latest['value'], 'currency' => 'USX', 'unit' => 'sen AS / lb',
|
||||
'period' => substr($latest['date'], 0, 7), 'frequency' => 'monthly', 'stale' => false,
|
||||
'description' => 'Biji kopi Arabica (Other Milds). Estimasi konversi harga acuan rata-rata bulanan IMF melalui Alpha Vantage.',
|
||||
'update_info' => 'Data Alpha Vantage diperiksa setiap hari untuk menjaga kuota API. Harga merupakan rata-rata bulanan dari IMF via FRED.',
|
||||
];
|
||||
$cache->put($lastKey, $quote, now()->addDays(30));
|
||||
$cache->put($cacheKey, [$quote], 86400);
|
||||
return [$quote];
|
||||
} catch (Throwable) {
|
||||
// Request exceptions can contain the API key in their URL; do not log them.
|
||||
$quotes = [];
|
||||
if (is_array($last = $cache->get($lastKey))) {
|
||||
$last['stale'] = true;
|
||||
$quotes[] = $last;
|
||||
}
|
||||
$cache->put($cacheKey, $quotes, 3600);
|
||||
return $quotes;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user