Removed okhttp3 dependency, using Utils.readUrl() isntead.
This commit is contained in:
parent
875269f5a6
commit
8f35d050df
3 changed files with 42 additions and 51 deletions
|
@ -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'
|
||||
|
|
|
@ -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<Message> 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);
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue