50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class WeatherGovService
|
|
{
|
|
private string $apiBaseUrl = "https://api.weather.gov";
|
|
|
|
private function fetchFromRemoteApi(string $url): ?array
|
|
{
|
|
$response = Http::retry(3, 60000, throw: false)->acceptJson()->get($url);
|
|
return $response->json();
|
|
}
|
|
|
|
public function fetchOffice($latitude, $longitude): array
|
|
{
|
|
$apiUrl = "{$this->apiBaseUrl}/points/{$latitude}/{$longitude}";
|
|
return Cache::remember('lookup.offices', 86400, function () use ($apiUrl) {
|
|
return $this->fetchFromRemoteApi($apiUrl);
|
|
});
|
|
}
|
|
|
|
public function fetchHourlyReport(string $weatherOffice, int $gridX, int $gridY): array
|
|
{
|
|
$apiUrl = "{$this->apiBaseUrl}/gridpoints/{$weatherOffice}/{$gridX}/{$gridY}/forecast/hourly";
|
|
return Cache::remember('lookup.offices', 900, function () use ($apiUrl) {
|
|
return $this->fetchFromRemoteApi($apiUrl);
|
|
});
|
|
}
|
|
|
|
public function fetchDailyReport(string $weatherOffice, int $gridX, int $gridY): array
|
|
{
|
|
$apiUrl = "{$this->apiBaseUrl}/gridpoints/{$weatherOffice}/{$gridX}/{$gridY}/";
|
|
return Cache::remember('lookup.offices', 3600, function () use ($apiUrl) {
|
|
return $this->fetchFromRemoteApi($apiUrl);
|
|
});
|
|
}
|
|
|
|
public function fetchWeeklyReport(string $weatherOffice, int $gridX, int $gridY): array
|
|
{
|
|
$apiUrl = "{$this->apiBaseUrl}/gridpoints/{$weatherOffice}/{$gridX}/{$gridY}/forecast";
|
|
return Cache::remember('lookup.offices', 86400, function () use ($apiUrl) {
|
|
return $this->fetchFromRemoteApi($apiUrl);
|
|
});
|
|
}
|
|
}
|