Handle openweathermap API key error properly
parent
3e6f50e575
commit
0f6505fb59
|
@ -27,6 +27,7 @@ class WeatherController extends Controller {
|
||||||
private $settingsMapper;
|
private $settingsMapper;
|
||||||
private $apiKey;
|
private $apiKey;
|
||||||
private $metric;
|
private $metric;
|
||||||
|
private $curl;
|
||||||
private static $apiWeatherURL = "http://api.openweathermap.org/data/2.5/weather?mode=json&q=";
|
private static $apiWeatherURL = "http://api.openweathermap.org/data/2.5/weather?mode=json&q=";
|
||||||
private static $apiForecastURL = "http://api.openweathermap.org/data/2.5/forecast?mode=json&q=";
|
private static $apiForecastURL = "http://api.openweathermap.org/data/2.5/forecast?mode=json&q=";
|
||||||
|
|
||||||
|
@ -37,6 +38,11 @@ class WeatherController extends Controller {
|
||||||
$this->settingsMapper = $settingsMapper;
|
$this->settingsMapper = $settingsMapper;
|
||||||
$this->apiKey = $settingsMapper->getApiKey($this->userId);
|
$this->apiKey = $settingsMapper->getApiKey($this->userId);
|
||||||
$this->metric = $settingsMapper->getMetric($this->userId);
|
$this->metric = $settingsMapper->getMetric($this->userId);
|
||||||
|
$this->curl = curl_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __destruct () {
|
||||||
|
curl_close($this->curl);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -46,18 +52,19 @@ class WeatherController extends Controller {
|
||||||
public function get($name) {
|
public function get($name) {
|
||||||
$cityInfos = $this->getCityInformations($name);
|
$cityInfos = $this->getCityInformations($name);
|
||||||
if (!$cityInfos) {
|
if (!$cityInfos) {
|
||||||
return new JSONResponse(array(), Http::STATUS_NOT_FOUND);
|
return new JSONResponse(array(), $this->errorCode);
|
||||||
}
|
}
|
||||||
return new JSONResponse($cityInfos);
|
return new JSONResponse($cityInfos);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getCityInformations ($name) {
|
private function getCityInformations ($name) {
|
||||||
// @TODO setting for metric
|
$reqContent = $this->curlGET(WeatherController::$apiWeatherURL.$name."&APPID=".$this->apiKey."&units=".$this->metric);
|
||||||
$cityDatas = json_decode(file_get_contents(WeatherController::$apiWeatherURL.$name."&APPID=".$this->apiKey."&units=".$this->metric), true);
|
if ($reqContent[0] != Http::STATUS_OK) {
|
||||||
if ($cityDatas['cod'] != '200') {
|
$this->errorCode = $reqContent[0];
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$cityDatas = json_decode($reqContent[1], true);
|
||||||
$cityDatas["forecast"] = array();
|
$cityDatas["forecast"] = array();
|
||||||
$forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$this->apiKey."&units=".$this->metric), true);
|
$forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$this->apiKey."&units=".$this->metric), true);
|
||||||
if ($forecast['cod'] == '200' && isset($forecast['cnt']) && is_numeric($forecast['cnt'])) {
|
if ($forecast['cod'] == '200' && isset($forecast['cnt']) && is_numeric($forecast['cnt'])) {
|
||||||
|
@ -82,6 +89,13 @@ class WeatherController extends Controller {
|
||||||
return $cityDatas;
|
return $cityDatas;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function curlGET ($URL) {
|
||||||
|
curl_setopt($this->curl, CURLOPT_URL, $URL);
|
||||||
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
|
||||||
|
$output = curl_exec($this->curl);
|
||||||
|
return array(curl_getinfo($this->curl, CURLINFO_HTTP_CODE), $output);
|
||||||
|
}
|
||||||
|
|
||||||
private function windDegToString($deg) {
|
private function windDegToString($deg) {
|
||||||
if ($deg > 0 && $deg < 23 ||
|
if ($deg > 0 && $deg < 23 ||
|
||||||
$deg > 333) {
|
$deg > 333) {
|
||||||
|
|
|
@ -146,6 +146,11 @@
|
||||||
font-size: 1.5em;
|
font-size: 1.5em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.city-load-error a {
|
||||||
|
color: #FF3333;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
#city-weather-panel, #city-forecast-panel {
|
#city-weather-panel, #city-forecast-panel {
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
padding: 30px;
|
padding: 30px;
|
||||||
|
|
|
@ -33,6 +33,7 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
|
||||||
$scope.addCityError = '';
|
$scope.addCityError = '';
|
||||||
|
|
||||||
$scope.cityLoadError = '';
|
$scope.cityLoadError = '';
|
||||||
|
$scope.cityLoadNeedsAPIKey = false;
|
||||||
$scope.currentCity = null;
|
$scope.currentCity = null;
|
||||||
$scope.selectedCityId = 0;
|
$scope.selectedCityId = 0;
|
||||||
$scope.domCity = null;
|
$scope.domCity = null;
|
||||||
|
@ -216,17 +217,25 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
|
||||||
else {
|
else {
|
||||||
$scope.cityLoadError = 'Failed to get city weather informations. Please contact your administrator';
|
$scope.cityLoadError = 'Failed to get city weather informations. Please contact your administrator';
|
||||||
}
|
}
|
||||||
|
$scope.cityLoadNeedsAPIKey = false;
|
||||||
}).
|
}).
|
||||||
error(function (data, status, headers, config) {
|
error(function (data, status, headers, config) {
|
||||||
if (status == 404) {
|
if (status == 404) {
|
||||||
$scope.cityLoadError = "No city with this name found.";
|
$scope.cityLoadError = "No city with this name found.";
|
||||||
|
$scope.cityLoadNeedsAPIKey = false;
|
||||||
|
}
|
||||||
|
else if (status == 401) {
|
||||||
|
$scope.cityLoadError = "Your OpenWeatherMap API key is invalid. Please provide a working API Key.";
|
||||||
|
$scope.cityLoadNeedsAPIKey = true;
|
||||||
}
|
}
|
||||||
else if (status == 500) {
|
else if (status == 500) {
|
||||||
$scope.cityLoadError = g_error500;
|
$scope.cityLoadError = g_error500;
|
||||||
|
$scope.cityLoadNeedsAPIKey = false;
|
||||||
}
|
}
|
||||||
}).
|
}).
|
||||||
fail(function (data, status, headers, config) {
|
fail(function (data, status, headers, config) {
|
||||||
$scope.cityLoadError = g_error500;
|
$scope.cityLoadError = g_error500;
|
||||||
|
$scope.cityLoadNeedsAPIKey = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,10 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="city-right" ng-show="cityLoadError != ''">
|
<div id="city-right" ng-show="cityLoadError != ''">
|
||||||
<span class="city-load-error">{{ cityLoadError }}</span>
|
<span class="city-load-error">
|
||||||
|
{{ cityLoadError }}<br /><br />
|
||||||
|
<a href="http://home.openweathermap.org/users/sign_in" ng-show="cityLoadNeedsAPIKey == true">Click here to get an API key</a>
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div id="city-right" ng-show="cityLoadError == '' && currentCity != null" style="background-image: url('{{ owncloudAppImgPath }}/img/{{ currentCity.image }}');">
|
<div id="city-right" ng-show="cityLoadError == '' && currentCity != null" style="background-image: url('{{ owncloudAppImgPath }}/img/{{ currentCity.image }}');">
|
||||||
<div id="city-weather-panel">
|
<div id="city-weather-panel">
|
||||||
|
|
Loading…
Reference in New Issue