62 lines
1.6 KiB
PHP
62 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Clients;
|
|
|
|
use App\Contracts\PriceClient;
|
|
use Exception;
|
|
use Illuminate\Http\Client\Factory;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
class ZonneplanPriceClient implements PriceClient
|
|
{
|
|
public function __construct(
|
|
private readonly Factory $http,
|
|
) {}
|
|
|
|
public function getElectricityPrices(): Collection
|
|
{
|
|
return collect($this->get('/energy-prices/electricity/upcoming'));
|
|
}
|
|
|
|
public function getGasPrices(): Collection
|
|
{
|
|
return collect($this->get('/energy-prices/gas/upcoming'));
|
|
}
|
|
|
|
/** @return array<int, array<string, mixed>> */
|
|
private function get(string $path): array
|
|
{
|
|
try {
|
|
$response = $this->http
|
|
->baseUrl(config('api.baseurl'))
|
|
->withQueryParameters([
|
|
'secret' => config('api.key'),
|
|
])
|
|
->timeout(10)
|
|
->retry(2, 100)
|
|
->get($path);
|
|
|
|
$response->throw();
|
|
|
|
Log::info('Zonneplan API opgehaald', [
|
|
'path' => $path,
|
|
'status' => $response->status(),
|
|
]);
|
|
|
|
return $response->json('data', []);
|
|
} catch (Throwable $e) {
|
|
Log::error('Zonneplan API aanroep mislukt', [
|
|
'path' => $path,
|
|
'message' => $e->getMessage(),
|
|
]);
|
|
|
|
throw new Exception(
|
|
"Kon energieprijzen niet ophalen voor {$path}",
|
|
previous: $e,
|
|
);
|
|
}
|
|
}
|
|
}
|