Some changes to WX template layout and cache the API call out #61

This commit is contained in:
Nabeel Shahzad 2018-04-02 07:06:00 -05:00
parent f84ff8de92
commit c000034cab
3 changed files with 55 additions and 28 deletions

View File

@ -4,6 +4,7 @@ namespace App\Widgets;
use App\Interfaces\Widget;
use App\Support\Http;
use Illuminate\Support\Facades\Cache;
/**
* This is a widget for the 3rd party CheckWX service
@ -23,15 +24,23 @@ class CheckWx extends Widget
if (!config('checkwx.api_key')) {
$data = null;
} else {
$url = config('checkwx.url').'/metar/'.$this->config['icao'].'/decoded';
$data = Http::get($url, [
'headers' => [
'X-API-Key' => config('checkwx.api_key'),
'content-type' => 'application/json',
]
]);
// Cache the request so we don't need to repeatedly call out
$cache = config('cache.keys.WEATHER_LOOKUP');
$key = $cache['key'].$this->config['icao'];
$data = Cache::remember($key, $cache['time'], function () {
$url = config('checkwx.url').'/metar/'.$this->config['icao'].'/decoded';
$data = Http::get($url, [
'headers' => [
'X-API-Key' => config('checkwx.api_key'),
'content-type' => 'application/json',
]
]);
$data = json_decode($data);
return $data;
});
$data = json_decode($data);
if($data->results === 1) {
$data = $data->data[0];

View File

@ -7,15 +7,19 @@ return [
'keys' => [
'AIRPORT_VACENTRAL_LOOKUP' => [
'key' => 'airports:lookup:',
'key' => 'airports.lookup:',
'time' => 60 * 30,
],
'WEATHER_LOOKUP' => [
'key' => 'airports.weather.', // append icao
'time' => 60 * 30, // Cache for 30 minutes
],
'RANKS_PILOT_LIST' => [
'key' => 'ranks:pilot_list',
'key' => 'ranks.pilot_list',
'time' => 60 * 10,
],
'USER_API_KEY' => [
'key' => 'user:apikey',
'key' => 'user.apikey',
'time' => 60 * 5, // 5 min
],
],

View File

@ -1,17 +1,13 @@
{{--
If you want to edit this, you can reference the CheckWX API docs:
https://api.checkwx.com/#metar-decoded
--}}
@if(!$data)
<p>METAR/TAF data could not be retrieved</p>
@else
<table class="table">
<tr>
<td colspan="2">
<div style="line-height:1.5em;min-height: 3em;">
{{$data->raw_text}}
</div>
</td>
</tr>
<tr>
<td colspan="2">Updated: {{$data->observed}}</td>
</tr>
<tr>
<td>Conditions</td>
<td>
@ -21,14 +17,20 @@
@else
{{$data->temperature->celsius}}°C
@endif
, visibility
@if($unit_dist === 'km')
{{intval(str_replace(',', '', $data->visibility->meters)) / 1000}}
@else
{{$data->visibility->miles}}
@endif
@if($data->visibility->miles)
, visibility
@if($unit_dist === 'km')
{{intval(str_replace(',', '', $data->visibility->meters)) / 1000}}
@else
{{$data->visibility->miles}}
@endif
&nbsp;
{{$data->humidity_percent}}% humidity
@endif
@if($data->humidity_percent)
{{$data->humidity_percent}}% humidity
@endif
</td>
</tr>
<tr>
@ -60,5 +62,17 @@
@endif
</td>
</tr>
<tr>
<td>Metar</td>
<td>
<div style="line-height:1.5em;min-height: 3em;">
{{$data->raw_text}}
</div>
</td>
</tr>
<tr>
<td>Updated</td>
<td>{{$data->observed}}</td>
</tr>
</table>
@endif