http-service: move several hardcoded urls to config

Also make an OkHttpClient bean and use it everywhere
This commit is contained in:
Adam
2021-12-22 12:46:52 -05:00
parent cc3ba2d633
commit b6e17cd15e
10 changed files with 109 additions and 66 deletions

View File

@@ -68,7 +68,7 @@ import org.sql2o.quirks.NoQuirks;
public class SpringBootWebApplication extends SpringBootServletInitializer
{
@Bean
protected ServletContextListener listener()
protected ServletContextListener listener(OkHttpClient client)
{
return new ServletContextListener()
{
@@ -82,7 +82,6 @@ public class SpringBootWebApplication extends SpringBootServletInitializer
public void contextDestroyed(ServletContextEvent sce)
{
// Destroy okhttp client
OkHttpClient client = RuneLiteAPI.CLIENT;
client.dispatcher().executorService().shutdown();
client.connectionPool().evictAll();
try
@@ -200,6 +199,12 @@ public class SpringBootWebApplication extends SpringBootServletInitializer
}
}
@Bean
public OkHttpClient okHttpClient()
{
return RuneLiteAPI.CLIENT;
}
public static void main(String[] args)
{
SpringApplication.run(SpringBootWebApplication.class, args);

View File

@@ -25,29 +25,11 @@
package net.runelite.http.service.account;
import java.util.UUID;
import lombok.Data;
@Data
public class State
{
private UUID uuid;
private String apiVersion;
public UUID getUuid()
{
return uuid;
}
public void setUuid(UUID uuid)
{
this.uuid = uuid;
}
public String getApiVersion()
{
return apiVersion;
}
public void setApiVersion(String apiVersion)
{
this.apiVersion = apiVersion;
}
}

View File

@@ -33,13 +33,15 @@ import java.util.List;
import java.util.Locale;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.feed.FeedItem;
import net.runelite.http.api.feed.FeedItemType;
import net.runelite.http.service.util.exception.InternalServerErrorException;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -50,16 +52,28 @@ import org.xml.sax.SAXException;
@Service
public class BlogService
{
private static final HttpUrl RSS_URL = HttpUrl.parse("https://runelite.net/atom.xml");
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
private final OkHttpClient okHttpClient;
private final HttpUrl rssUrl;
@Autowired
public BlogService(
OkHttpClient okHttpClient,
@Value("${runelite.feed.rssUrl}") String rssUrl
)
{
this.okHttpClient = okHttpClient;
this.rssUrl = HttpUrl.get(rssUrl);
}
public List<FeedItem> getBlogPosts() throws IOException
{
Request request = new Request.Builder()
.url(RSS_URL)
.url(rssUrl)
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
try (Response response = okHttpClient.newCall(request).execute())
{
if (!response.isSuccessful())
{

View File

@@ -33,13 +33,15 @@ import java.util.List;
import java.util.Locale;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.feed.FeedItem;
import net.runelite.http.api.feed.FeedItemType;
import net.runelite.http.service.util.exception.InternalServerErrorException;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -50,16 +52,28 @@ import org.xml.sax.SAXException;
@Service
public class OSRSNewsService
{
private static final HttpUrl RSS_URL = HttpUrl.parse("https://services.runescape.com/m=news/latest_news.rss?oldschool=true");
private static final SimpleDateFormat PUB_DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy '00:00:00 GMT'", Locale.US);
private final OkHttpClient okHttpClient;
private final HttpUrl rssUrl;
@Autowired
public OSRSNewsService(
OkHttpClient okHttpClient,
@Value("${runelite.osrsnews.rssUrl}") String rssUrl
)
{
this.okHttpClient = okHttpClient;
this.rssUrl = HttpUrl.get(rssUrl);
}
public List<FeedItem> getNews() throws IOException
{
Request request = new Request.Builder()
.url(RSS_URL)
.url(rssUrl)
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
try (Response response = okHttpClient.newCall(request).execute())
{
if (!response.isSuccessful())
{

View File

@@ -39,6 +39,7 @@ import net.runelite.http.api.feed.FeedItemType;
import net.runelite.http.service.util.exception.InternalServerErrorException;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
@@ -54,6 +55,7 @@ public class TwitterService
private final String credentials;
private final String listId;
private final OkHttpClient okHttpClient;
private String token;
@@ -61,11 +63,13 @@ public class TwitterService
public TwitterService(
@Value("${runelite.twitter.consumerkey}") String consumerKey,
@Value("${runelite.twitter.secretkey}") String consumerSecret,
@Value("${runelite.twitter.listid}") String listId
@Value("${runelite.twitter.listid}") String listId,
OkHttpClient okHttpClient
)
{
this.credentials = consumerKey + ":" + consumerSecret;
this.listId = listId;
this.okHttpClient = okHttpClient;
}
public List<FeedItem> getTweets() throws IOException
@@ -91,7 +95,7 @@ public class TwitterService
.header("Authorization", "Bearer " + token)
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
try (Response response = okHttpClient.newCall(request).execute())
{
if (!response.isSuccessful())
{
@@ -143,7 +147,7 @@ public class TwitterService
.post(new FormBody.Builder().add("grant_type", "client_credentials").build())
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
try (Response response = okHttpClient.newCall(request).execute())
{
if (!response.isSuccessful())
{

View File

@@ -39,10 +39,12 @@ import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.item.ItemType;
import net.runelite.http.service.cache.CacheService;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.sql2o.Connection;
@@ -53,10 +55,6 @@ import org.sql2o.Sql2o;
@Slf4j
public class ItemService
{
private static final String BASE = "https://services.runescape.com/m=itemdb_oldschool";
private static final HttpUrl RS_ITEM_URL = HttpUrl.parse(BASE + "/api/catalogue/detail.json");
private static final HttpUrl RS_PRICE_URL = HttpUrl.parse(BASE + "/api/graph");
private static final String CREATE_ITEMS = "CREATE TABLE IF NOT EXISTS `items` (\n"
+ " `id` int(11) NOT NULL,\n"
+ " `name` tinytext NOT NULL,\n"
@@ -77,16 +75,27 @@ public class ItemService
private final Sql2o sql2o;
private final CacheService cacheService;
private final OkHttpClient okHttpClient;
private final HttpUrl itemUrl;
private final HttpUrl priceUrl;
private int[] tradeableItems;
private final Random random = new Random();
@Autowired
public ItemService(@Qualifier("Runelite SQL2O") Sql2o sql2o,
CacheService cacheService)
public ItemService(
@Qualifier("Runelite SQL2O") Sql2o sql2o,
CacheService cacheService,
OkHttpClient okHttpClient,
@Value("${runelite.item.itemUrl}") String itemUrl,
@Value("${runelite.item.priceUrl}") String priceUrl
)
{
this.sql2o = sql2o;
this.cacheService = cacheService;
this.okHttpClient = okHttpClient;
this.itemUrl = HttpUrl.get(itemUrl);
this.priceUrl = HttpUrl.get(priceUrl);
try (Connection con = sql2o.open())
{
@@ -195,7 +204,7 @@ public class ItemService
private RSItem fetchRSItem(int itemId) throws IOException
{
HttpUrl itemUrl = RS_ITEM_URL
HttpUrl itemUrl = this.itemUrl
.newBuilder()
.addQueryParameter("item", "" + itemId)
.build();
@@ -211,7 +220,7 @@ public class ItemService
private RSPrices fetchRSPrices(int itemId) throws IOException
{
HttpUrl priceUrl = RS_PRICE_URL
HttpUrl priceUrl = this.priceUrl
.newBuilder()
.addPathSegment(itemId + ".json")
.build();
@@ -223,9 +232,9 @@ public class ItemService
return fetchJson(request, RSPrices.class);
}
private static <T> T fetchJson(Request request, Class<T> clazz) throws IOException
private <T> T fetchJson(Request request, Class<T> clazz) throws IOException
{
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
try (Response response = okHttpClient.newCall(request).execute())
{
if (!response.isSuccessful())
{

View File

@@ -31,6 +31,7 @@ import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
@@ -57,14 +58,19 @@ public class WikiPriceService
") ENGINE=InnoDB;";
private final Sql2o sql2o;
@Value("${runelite.wiki.url}")
private String url;
private final OkHttpClient okHttpClient;
private final HttpUrl wikiUrl;
@Autowired
public WikiPriceService(@Qualifier("Runelite SQL2O") Sql2o sql2o)
public WikiPriceService(
@Qualifier("Runelite SQL2O") Sql2o sql2o,
OkHttpClient okHttpClient,
@Value("${runelite.wiki.url}") String url
)
{
this.sql2o = sql2o;
this.okHttpClient = okHttpClient;
this.wikiUrl = HttpUrl.get(url);
try (Connection con = sql2o.open())
{
@@ -112,13 +118,12 @@ public class WikiPriceService
private PriceResult getPrices() throws IOException
{
HttpUrl httpUrl = HttpUrl.parse(url);
Request request = new Request.Builder()
.url(httpUrl)
.url(wikiUrl)
.header("User-Agent", "RuneLite")
.build();
try (Response responseOk = RuneLiteAPI.CLIENT.newCall(request).execute())
try (Response responseOk = okHttpClient.newCall(request).execute())
{
if (!responseOk.isSuccessful())
{

View File

@@ -29,21 +29,32 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.worlds.World;
import net.runelite.http.api.worlds.WorldResult;
import net.runelite.http.api.worlds.WorldType;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class WorldsService
{
private static final HttpUrl WORLD_URL = HttpUrl.parse("http://www.runescape.com/g=oldscape/slr.ws?order=LPWM");
private final OkHttpClient okHttpClient;
private final HttpUrl url;
private HttpUrl url = WORLD_URL;
@Autowired
public WorldsService(
OkHttpClient okHttpClient,
@Value("${runelite.worlds.url}") String url
)
{
this.okHttpClient = okHttpClient;
this.url = HttpUrl.get(url);
}
public WorldResult getWorlds() throws IOException
{
@@ -53,7 +64,7 @@ public class WorldsService
byte[] b;
try (Response okresponse = RuneLiteAPI.CLIENT.newCall(okrequest).execute())
try (Response okresponse = okHttpClient.newCall(okrequest).execute())
{
b = okresponse.body().bytes();
}
@@ -118,14 +129,4 @@ public class WorldsService
return sb.toString();
}
public HttpUrl getUrl()
{
return url;
}
public void setUrl(HttpUrl url)
{
this.url = url;
}
}

View File

@@ -47,3 +47,12 @@ runelite:
url: https://prices.runescape.wiki/api/v1/osrs/latest
price:
cache: 30 # minutes
feed:
rssUrl: https://runelite.net/atom.xml
worlds:
url: http://www.runescape.com/g=oldscape/slr.ws?order=LPWM
osrsnews:
rssUrl: https://services.runescape.com/m=news/latest_news.rss?oldschool=true
item:
itemUrl: https://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json
priceUrl: https://services.runescape.com/m=itemdb_oldschool/api/graph