From 3e96b195a6582dccaec18e991c6d28f7445ddf6c Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 14 May 2018 11:20:38 -0500 Subject: [PATCH] Add short timeout to HTTP calls by default; cache METAR lookups --- .../Controllers/Api/AirportController.php | 3 +-- app/Services/Metar/AviationWeather.php | 23 +++++++++++++++---- app/Support/Http.php | 4 ++++ config/cache.php | 2 +- tests/MetarTest.php | 10 ++++++++ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/AirportController.php b/app/Http/Controllers/Api/AirportController.php index 06f39642..1ea3c499 100644 --- a/app/Http/Controllers/Api/AirportController.php +++ b/app/Http/Controllers/Api/AirportController.php @@ -84,13 +84,12 @@ class AirportController extends Controller { $airport = Cache::remember( config('cache.keys.AIRPORT_VACENTRAL_LOOKUP.key').$id, - config('cache.keys.RANKS_PILOT_LIST.time'), + config('cache.keys.AIRPORT_VACENTRAL_LOOKUP.time'), function () use ($id) { try { return AirportLookup::get($id); } catch (\VaCentral\HttpException $e) { Log::error($e); - return []; } } diff --git a/app/Services/Metar/AviationWeather.php b/app/Services/Metar/AviationWeather.php index f3442cca..136efeac 100644 --- a/app/Services/Metar/AviationWeather.php +++ b/app/Services/Metar/AviationWeather.php @@ -4,6 +4,7 @@ namespace App\Services\Metar; use App\Interfaces\Metar; use App\Support\Http; +use Cache; /** * Return the raw METAR string from the NOAA Aviation Weather Service @@ -20,13 +21,25 @@ class AviationWeather extends Metar * Implement the METAR - Return the string * @param $icao * @return string - * @throws \GuzzleHttp\Exception\GuzzleException */ protected function metar($icao): string { - $url = static::METAR_URL.$icao; - $res = Http::get($url, []); - $xml = simplexml_load_string($res); - return $xml->data->METAR->raw_text->__toString(); + $metar = Cache::remember( + config('cache.keys.WEATHER_LOOKUP.key').$icao, + config('cache.keys.WEATHER_LOOKUP.time'), + function () use ($icao) { + $url = static::METAR_URL.$icao; + try { + $res = Http::get($url, []); + $xml = simplexml_load_string($res); + return $xml->data->METAR->raw_text->__toString(); + + } catch (\Exception $e) { + return ''; + } + } + ); + + return $metar; } } diff --git a/app/Support/Http.php b/app/Support/Http.php index 79d6fe05..f5d2ba31 100644 --- a/app/Support/Http.php +++ b/app/Support/Http.php @@ -20,6 +20,10 @@ class Http */ public static function get($uri, array $opts) { + $opts = array_merge([ + 'connect_timeout' => 2, // wait two seconds by default + ], $opts); + $client = new Client(); $response = $client->request('GET', $uri, $opts); diff --git a/config/cache.php b/config/cache.php index 6a7e234e..f9611ce1 100755 --- a/config/cache.php +++ b/config/cache.php @@ -12,7 +12,7 @@ return [ ], 'WEATHER_LOOKUP' => [ 'key' => 'airports.weather.', // append icao - 'time' => 60 * 30, // Cache for 30 minutes + 'time' => 60 * 60, // Cache for 60 minutes ], 'RANKS_PILOT_LIST' => [ 'key' => 'ranks.pilot_list', diff --git a/tests/MetarTest.php b/tests/MetarTest.php index 765172f1..85c1bfde 100644 --- a/tests/MetarTest.php +++ b/tests/MetarTest.php @@ -16,6 +16,16 @@ class MetarTest extends TestCase $this->settingsRepo = app(SettingRepository::class); } + /** + * Make sure a blank metar doesn't give problems + */ + public function testBlankMetar() + { + $metar = ''; + $parsed = Metar::parse($metar); + $this->assertEquals('', $parsed['raw']); + } + /** * Test adding/subtracting a percentage */