Removed okhttp3 dependency, using Utils.readUrl() isntead.

This commit is contained in:
Erik C. Thauvin 2020-06-20 17:09:58 -07:00
parent 875269f5a6
commit 8f35d050df
3 changed files with 42 additions and 51 deletions

View file

@ -48,7 +48,7 @@ dependencies {
implementation 'commons-cli:commons-cli:1.4' implementation 'commons-cli:commons-cli:1.4'
implementation 'commons-net:commons-net:3.6' 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' implementation 'com.rometools:rome:1.13.1'
@ -63,7 +63,7 @@ dependencies {
implementation platform('org.jetbrains.kotlin:kotlin-bom') implementation platform('org.jetbrains.kotlin:kotlin-bom')
implementation 'org.jetbrains.kotlin:kotlin-stdlib' 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.testng:testng:7.2.0'
testImplementation 'org.assertj:assertj-core:3.16.1' testImplementation 'org.assertj:assertj-core:3.16.1'

View file

@ -39,18 +39,15 @@ import net.thauvin.erik.mobibot.msg.ErrorMessage;
import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.Message;
import net.thauvin.erik.mobibot.msg.NoticeMessage; import net.thauvin.erik.mobibot.msg.NoticeMessage;
import net.thauvin.erik.mobibot.msg.PublicMessage; import net.thauvin.erik.mobibot.msg.PublicMessage;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* The StockQuote module. * The StockQuote module.
@ -88,43 +85,39 @@ public final class StockQuote extends ThreadedModule {
@SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", @SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE",
justification = "false positive?") justification = "false positive?")
private static JSONObject getJsonResponse(final Response response, final String debugMessage) private static JSONObject getJsonResponse(final String response, final String debugMessage)
throws IOException, ModuleException { throws ModuleException {
if (response.isSuccessful()) { if (StringUtils.isNotBlank(response)) {
if (response.body() != null) { final JSONObject json = new JSONObject(response);
final JSONObject json = new JSONObject(Objects.requireNonNull(response.body()).string());
try { try {
final String info = json.getString("Information"); final String info = json.getString("Information");
if (!info.isEmpty()) { if (!info.isEmpty()) {
throw new ModuleException(debugMessage, Utils.unescapeXml(info)); throw new ModuleException(debugMessage, Utils.unescapeXml(info));
}
} catch (JSONException ignore) {
// Do nothing
} }
} catch (JSONException ignore) {
try { // Do nothing
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() + ')');
} }
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 { } else {
throw new ModuleException(debugMessage, "Empty Response."); throw new ModuleException(debugMessage, "Empty Response.");
} }
@ -146,13 +139,13 @@ public final class StockQuote extends ThreadedModule {
if (StringUtils.isNotBlank(symbol)) { if (StringUtils.isNotBlank(symbol)) {
final String debugMessage = "getQuote(" + symbol + ')'; final String debugMessage = "getQuote(" + symbol + ')';
final ArrayList<Message> messages = new ArrayList<>(); final ArrayList<Message> messages = new ArrayList<>();
final OkHttpClient client = new OkHttpClient();
String response;
try { try {
// Search for symbol/keywords // Search for symbol/keywords
Request request = new Request.Builder().url( response = Utils.urlReader(new URL(
ALAPHAVANTAGE_URL + "SYMBOL_SEARCH&keywords=" + symbol + "&apikey=" + apiKey).build(); ALAPHAVANTAGE_URL + "SYMBOL_SEARCH&keywords=" + Utils.encodeUrl(symbol) + "&apikey="
Response response = client.newCall(request).execute(); + Utils.encodeUrl(apiKey)));
JSONObject json = getJsonResponse(response, debugMessage); JSONObject json = getJsonResponse(response, debugMessage);
@ -165,10 +158,9 @@ public final class StockQuote extends ThreadedModule {
final JSONObject symbolInfo = symbols.getJSONObject(0); final JSONObject symbolInfo = symbols.getJSONObject(0);
// Get quote for symbol // Get quote for symbol
request = new Request.Builder().url( response = Utils.urlReader(new URL(
ALAPHAVANTAGE_URL + "GLOBAL_QUOTE&symbol=" + symbolInfo.getString("1. symbol") + "&apikey=" ALAPHAVANTAGE_URL + "GLOBAL_QUOTE&symbol=" + Utils.encodeUrl(symbolInfo.getString("1. symbol"))
+ apiKey).build(); + "&apikey=" + Utils.encodeUrl(apiKey)));
response = client.newCall(request).execute();
json = getJsonResponse(response, debugMessage); json = getJsonResponse(response, debugMessage);

View file

@ -183,9 +183,8 @@ public class Weather2 extends ThreadedModule {
messages.add(new NoticeMessage("https://openweathermap.org/city/" + cwd.getCityId(), messages.add(new NoticeMessage("https://openweathermap.org/city/" + cwd.getCityId(),
Colors.GREEN)); Colors.GREEN));
} else { } else {
final String url = "https://openweathermap.org/find?q=" + messages.add(new NoticeMessage("https://openweathermap.org/find?q=" + Utils.encodeUrl(
Utils.encodeUrl(city + ',' + StringUtils.upperCase(country)); city + ',' + StringUtils.upperCase(country)), Colors.GREEN));
messages.add(new NoticeMessage(url, Colors.GREEN));
} }
} }
} }