From 8f35d050dfaac74a2de8cb4f0a9fa5ca16d7c256 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 20 Jun 2020 17:09:58 -0700 Subject: [PATCH] Removed okhttp3 dependency, using Utils.readUrl() isntead. --- build.gradle | 4 +- .../erik/mobibot/modules/StockQuote.java | 84 +++++++++---------- .../erik/mobibot/modules/Weather2.java | 5 +- 3 files changed, 42 insertions(+), 51 deletions(-) diff --git a/build.gradle b/build.gradle index 11a21b5..5925622 100644 --- a/build.gradle +++ b/build.gradle @@ -48,7 +48,7 @@ dependencies { implementation 'commons-cli:commons-cli:1.4' implementation 'commons-net:commons-net:3.6' - implementation 'com.squareup.okhttp3:okhttp:4.7.2' + //implementation 'com.squareup.okhttp3:okhttp:4.7.2' implementation 'com.rometools:rome:1.13.1' @@ -63,7 +63,7 @@ dependencies { implementation platform('org.jetbrains.kotlin:kotlin-bom') implementation 'org.jetbrains.kotlin:kotlin-stdlib' - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7") + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7' testImplementation 'org.testng:testng:7.2.0' testImplementation 'org.assertj:assertj-core:3.16.1' diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java index 03b6ebb..4e5c1c9 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java @@ -39,18 +39,15 @@ import net.thauvin.erik.mobibot.msg.ErrorMessage; import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.NoticeMessage; import net.thauvin.erik.mobibot.msg.PublicMessage; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; +import java.net.URL; import java.util.ArrayList; import java.util.List; -import java.util.Objects; /** * The StockQuote module. @@ -88,43 +85,39 @@ public final class StockQuote extends ThreadedModule { @SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification = "false positive?") - private static JSONObject getJsonResponse(final Response response, final String debugMessage) - throws IOException, ModuleException { - if (response.isSuccessful()) { - if (response.body() != null) { - final JSONObject json = new JSONObject(Objects.requireNonNull(response.body()).string()); + private static JSONObject getJsonResponse(final String response, final String debugMessage) + throws ModuleException { + if (StringUtils.isNotBlank(response)) { + final JSONObject json = new JSONObject(response); - try { - final String info = json.getString("Information"); - if (!info.isEmpty()) { - throw new ModuleException(debugMessage, Utils.unescapeXml(info)); - } - } catch (JSONException ignore) { - // Do nothing + try { + final String info = json.getString("Information"); + if (!info.isEmpty()) { + throw new ModuleException(debugMessage, Utils.unescapeXml(info)); } - - try { - final String error = json.getString("Note"); - if (!error.isEmpty()) { - throw new ModuleException(debugMessage, Utils.unescapeXml(error)); - } - } catch (JSONException ignore) { - // Do nothing - } - - try { - final String error = json.getString("Error Message"); - if (!error.isEmpty()) { - throw new ModuleException(debugMessage, Utils.unescapeXml(error)); - } - } catch (JSONException ignore) { - // Do nothing - } - - return json; - } else { - throw new ModuleException(debugMessage, "Invalid Response (" + response.code() + ')'); + } catch (JSONException ignore) { + // Do nothing } + + try { + final String error = json.getString("Note"); + if (!error.isEmpty()) { + throw new ModuleException(debugMessage, Utils.unescapeXml(error)); + } + } catch (JSONException ignore) { + // Do nothing + } + + try { + final String error = json.getString("Error Message"); + if (!error.isEmpty()) { + throw new ModuleException(debugMessage, Utils.unescapeXml(error)); + } + } catch (JSONException ignore) { + // Do nothing + } + + return json; } else { throw new ModuleException(debugMessage, "Empty Response."); } @@ -146,13 +139,13 @@ public final class StockQuote extends ThreadedModule { if (StringUtils.isNotBlank(symbol)) { final String debugMessage = "getQuote(" + symbol + ')'; final ArrayList messages = new ArrayList<>(); - final OkHttpClient client = new OkHttpClient(); + String response; try { // Search for symbol/keywords - Request request = new Request.Builder().url( - ALAPHAVANTAGE_URL + "SYMBOL_SEARCH&keywords=" + symbol + "&apikey=" + apiKey).build(); - Response response = client.newCall(request).execute(); + response = Utils.urlReader(new URL( + ALAPHAVANTAGE_URL + "SYMBOL_SEARCH&keywords=" + Utils.encodeUrl(symbol) + "&apikey=" + + Utils.encodeUrl(apiKey))); JSONObject json = getJsonResponse(response, debugMessage); @@ -165,10 +158,9 @@ public final class StockQuote extends ThreadedModule { final JSONObject symbolInfo = symbols.getJSONObject(0); // Get quote for symbol - request = new Request.Builder().url( - ALAPHAVANTAGE_URL + "GLOBAL_QUOTE&symbol=" + symbolInfo.getString("1. symbol") + "&apikey=" - + apiKey).build(); - response = client.newCall(request).execute(); + response = Utils.urlReader(new URL( + ALAPHAVANTAGE_URL + "GLOBAL_QUOTE&symbol=" + Utils.encodeUrl(symbolInfo.getString("1. symbol")) + + "&apikey=" + Utils.encodeUrl(apiKey))); json = getJsonResponse(response, debugMessage); diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java index 9ef1426..e1910ac 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java @@ -183,9 +183,8 @@ public class Weather2 extends ThreadedModule { messages.add(new NoticeMessage("https://openweathermap.org/city/" + cwd.getCityId(), Colors.GREEN)); } else { - final String url = "https://openweathermap.org/find?q=" + - Utils.encodeUrl(city + ',' + StringUtils.upperCase(country)); - messages.add(new NoticeMessage(url, Colors.GREEN)); + messages.add(new NoticeMessage("https://openweathermap.org/find?q=" + Utils.encodeUrl( + city + ',' + StringUtils.upperCase(country)), Colors.GREEN)); } } }