Skip to content

Commit

Permalink
updated request handler to close all requests after they're used to p…
Browse files Browse the repository at this point in the history
…revent leakage, separated skills enum from the skill class, updated parser to use a more favourable loop
  • Loading branch information
BasketBandit committed May 26, 2019
1 parent e1941dc commit 512fae3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/baseketbandit/runeapi/RuneAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
public class RuneAPI {

public static Map<String, Skill> getStats(String username) {
return RequestHandler.doRequest(username.replace(" ", "%20"));
return RequestHandler.doGetRequest(username.replace(" ", "%20"));
}
}
26 changes: 0 additions & 26 deletions src/main/java/com/baseketbandit/runeapi/entity/Skill.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/baseketbandit/runeapi/entity/Skills.java
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 6 additions & 8 deletions src/main/java/com/baseketbandit/runeapi/io/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ class Parser {
* @return Map<String, Skill> HashMap of objects of type Skill
*/
static Map<String, Skill> parseStats(String data) {
List<Enum> skillNames = Arrays.asList(Skill.Name.values());
List<Enum> names = Arrays.asList(Skills.values());
Map<String, Skill> 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;
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,36 @@
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.
*
* @param username the username to do the request against
* @return Map<String, Skill> HashMap of objects of type Skill or an `empty` map if no results are found.
*/
public static Map<String, Skill> doRequest(String username) {
public static Map<String, Skill> 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<>();
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/baseketbandit/runeapi/RuneAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ public void getStatsMapped() {
Skill cooking = stats.get("Cooking");
System.out.println(cooking.getLevel());
}

@Test
public void getStatsMappedLowercase() {
Map<String, Skill> stats = RuneAPI.getStats("xwr");
Skill cooking = stats.get("Cooking");
System.out.println(cooking.getLevel());
}
}

0 comments on commit 512fae3

Please sign in to comment.