ipaddress = (ip != null ? ip : Http.Request.current().remoteAddress);
ip = ipaddress;
Cache.set(sessionId + "-ipaddress", ipaddress);
Logger.debug("[Weather.data] ipaddress = %s", ipaddress);
Data data = null;
try {
data = Data.get(ipaddress);
Logger.debug("[Weather.data] data = %s", data);
} catch (ConnectionException e) {
Logger.warn("[Weather.data] Error retrieving data object from DB.");
}
if(data == null) {
String url = WWO_LOCAL_WEATHER_URL + "?q=" + ipaddress
+ "&format=json"
+ "&num_of_days=5"
+ "&cc=no"
+ "&includeLocation=yes"
+ "&key=" + WWO_API_KEY;
Logger.debug("[Weather.data] url = %s", url);
Promise<WS.HttpResponse> promise = WS.url(url).getAsync();
WS.HttpResponse response = await(promise);
if (response.getStatus() / 100 != 2) {
Logger.error("[Weather.data] %d - %s",
response.getStatus(), response.getString());
error(response.getStatus(), "Unable to retrieve data.");
}
data = new Data(ipaddress, response.getString());
try {
data.store(TTL_DATA);
} catch (ConnectionException e) {
Logger.warn("[Weather.data] Error storing data object into the DB.");
}
}