From 512fae36fc7857ab4830b993cfc7ccf7001ca47e Mon Sep 17 00:00:00 2001 From: BasketBandit Date: Sun, 26 May 2019 21:25:53 +0100 Subject: [PATCH] updated request handler to close all requests after they're used to prevent leakage, separated skills enum from the skill class, updated parser to use a more favourable loop --- .../com/baseketbandit/runeapi/RuneAPI.java | 2 +- .../baseketbandit/runeapi/entity/Skill.java | 26 ----------------- .../baseketbandit/runeapi/entity/Skills.java | 28 +++++++++++++++++++ .../com/baseketbandit/runeapi/io/Parser.java | 14 ++++------ .../runeapi/io/RequestHandler.java | 16 +++++++++-- .../baseketbandit/runeapi/RuneAPITest.java | 7 +++++ 6 files changed, 55 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/baseketbandit/runeapi/entity/Skills.java diff --git a/src/main/java/com/baseketbandit/runeapi/RuneAPI.java b/src/main/java/com/baseketbandit/runeapi/RuneAPI.java index dc16074..dad2842 100644 --- a/src/main/java/com/baseketbandit/runeapi/RuneAPI.java +++ b/src/main/java/com/baseketbandit/runeapi/RuneAPI.java @@ -8,6 +8,6 @@ public class RuneAPI { public static Map getStats(String username) { - return RequestHandler.doRequest(username.replace(" ", "%20")); + return RequestHandler.doGetRequest(username.replace(" ", "%20")); } } diff --git a/src/main/java/com/baseketbandit/runeapi/entity/Skill.java b/src/main/java/com/baseketbandit/runeapi/entity/Skill.java index 2d702c6..e707341 100644 --- a/src/main/java/com/baseketbandit/runeapi/entity/Skill.java +++ b/src/main/java/com/baseketbandit/runeapi/entity/Skill.java @@ -30,30 +30,4 @@ public int getExperience() { return experience; } - public enum Name { - Overall, - Attack, - Defence, - Strength, - Hitpoints, - Ranged, - Prayer, - Magic, - Cooking, - Woodcutting, - Fletching, - Fishing, - Firemaking, - Crafting, - Smithing, - Mining, - Herblore, - Agility, - Thieving, - Slayer, - Farming, - Runecraft, - Hunter, - Construction - } } diff --git a/src/main/java/com/baseketbandit/runeapi/entity/Skills.java b/src/main/java/com/baseketbandit/runeapi/entity/Skills.java new file mode 100644 index 0000000..9003846 --- /dev/null +++ b/src/main/java/com/baseketbandit/runeapi/entity/Skills.java @@ -0,0 +1,28 @@ +package com.baseketbandit.runeapi.entity; + +public enum Skills { + Overall, + Attack, + Defence, + Strength, + Hitpoints, + Ranged, + Prayer, + Magic, + Cooking, + Woodcutting, + Fletching, + Fishing, + Firemaking, + Crafting, + Smithing, + Mining, + Herblore, + Agility, + Thieving, + Slayer, + Farming, + Runecraft, + Hunter, + Construction +} diff --git a/src/main/java/com/baseketbandit/runeapi/io/Parser.java b/src/main/java/com/baseketbandit/runeapi/io/Parser.java index 38ba056..89bcedc 100644 --- a/src/main/java/com/baseketbandit/runeapi/io/Parser.java +++ b/src/main/java/com/baseketbandit/runeapi/io/Parser.java @@ -13,16 +13,14 @@ class Parser { * @return Map HashMap of objects of type Skill */ static Map parseStats(String data) { - List skillNames = Arrays.asList(Skill.Name.values()); + List names = Arrays.asList(Skills.values()); Map skills = new HashMap<>(); - String[] rawSkill = data.split("\n"); + String[] raw = data.split("\n"); - int i = 0; - for(String raw: rawSkill) { - if(i < Skill.Name.values().length) { - String[] r = raw.split(","); - skills.put(skillNames.get(i).name(), new Skill(skillNames.get(i).name(), Integer.parseInt(r[0]), Integer.parseInt(r[1]), Integer.parseInt(r[2]))); - i++; + for(int i = 0; i < raw.length; i++) { + if(i < Skills.values().length) { + String[] r = raw[i].split(","); + skills.put(names.get(i).name(), new Skill(names.get(i).name(), Integer.parseInt(r[0]), Integer.parseInt(r[1]), Integer.parseInt(r[2]))); continue; } break; diff --git a/src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java b/src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java index 621b293..a9dc217 100644 --- a/src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java +++ b/src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java @@ -14,6 +14,7 @@ public class RequestHandler { private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); private static final OkHttpClient client = new OkHttpClient(); + private static final String API_BASE = "https://secure.runescape.com/m=hiscore_oldschool/index_lite.ws?player="; /** * A OkHttp method used to connect to and return a HashMap of data from the server. @@ -21,19 +22,28 @@ public class RequestHandler { * @param username the username to do the request against * @return Map HashMap of objects of type Skill or an `empty` map if no results are found. */ - public static Map doRequest(String username) { + public static Map doGetRequest(String username) { try { Request request = new Request.Builder() - .url("https://secure.runescape.com/m=hiscore_oldschool/index_lite.ws?player=" + username) + .url(API_BASE + username) .build(); Response response = client.newCall(request).execute(); if(response.code() != 200) { log.error(response.code() + " - No results found for username `" + username.replace("%20", " ") + "`"); + response.close(); return new HashMap<>(); } - return Parser.parseStats(response.body().string()); + if(response.body() == null) { + response.close(); + return new HashMap<>(); + } + + final String data = response.body().string(); + response.close(); + + return Parser.parseStats(data); } catch(IOException e) { log.error("There was a problem processing that request. [Highscores OSRS]"); return new HashMap<>(); diff --git a/src/test/java/com/baseketbandit/runeapi/RuneAPITest.java b/src/test/java/com/baseketbandit/runeapi/RuneAPITest.java index 7e11878..eff8ed4 100644 --- a/src/test/java/com/baseketbandit/runeapi/RuneAPITest.java +++ b/src/test/java/com/baseketbandit/runeapi/RuneAPITest.java @@ -41,4 +41,11 @@ public void getStatsMapped() { Skill cooking = stats.get("Cooking"); System.out.println(cooking.getLevel()); } + + @Test + public void getStatsMappedLowercase() { + Map stats = RuneAPI.getStats("xwr"); + Skill cooking = stats.get("Cooking"); + System.out.println(cooking.getLevel()); + } } \ No newline at end of file