Skip to content

Commit e868be6

Browse files
author
Damian Staszewski
committed
more better, quite look code
1 parent c6cb1c7 commit e868be6

File tree

7 files changed

+62
-20
lines changed

7 files changed

+62
-20
lines changed

src/main/java/pl/stachuofficial/hirezapi/HiRezAPI.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public synchronized String setProperty(String key, String value) {
5151
* @param authKey Authorization Key (AuthKey)
5252
*/
5353
public HiRezAPI(String devId, String authKey) {
54+
if (!Boolean.valueOf(System.getProperty("hirez.debug"))) {
55+
System.setProperty("hirez.debug","false");
56+
}
5457
this.devId = devId;
5558
this.authKey = authKey;
5659
try {

src/main/java/pl/stachuofficial/hirezapi/api/HttpClient.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import okhttp3.Request;
55
import okhttp3.Response;
66

7-
import java.io.IOException;
87
import java.util.Arrays;
98
import java.util.concurrent.TimeUnit;
109

@@ -36,14 +35,23 @@ protected String request(String endpoint, String... args) {
3635
url += (Arrays.asList(args).toArray().length > 0) ?
3736
String.join("/", Arrays.asList(args)) :
3837
"";
38+
if (Boolean.valueOf(System.getProperty("hirez.debug"))) {
39+
System.out.println(url);
40+
}
3941

4042
Request request = new Request.Builder()
4143
.url(url)
4244
.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36")
4345
.build();
4446

4547
Response response = client.newCall(request).execute();
46-
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
48+
if (!response.isSuccessful()) {
49+
String res_data = "Protocol: " + response.protocol().toString();
50+
res_data += ";\r\nCode: " + response.code();
51+
res_data += ";\r\nMessage: " + response.message();
52+
res_data += ";\r\nURL: " + url;
53+
throw new SessionException("Unexpected code \r\n" + res_data);
54+
}
4755

4856
return response.body().string();
4957
} catch (Exception ex) {

src/main/java/pl/stachuofficial/hirezapi/api/Session.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
*/
1111
public class Session extends HttpClient {
1212
private final String platform;
13-
private final String devId;
14-
private final String authKey;
1513
private final Signature sig;
1614

1715
public Session(Smite.Platform platform, String devId, String authKey) {
1816
super(platform.getUrl());
1917
this.platform = "SMITE_" + platform.name();
20-
this.devId = devId;
21-
this.authKey = authKey;
2218
this.sig = new Signature(platform, devId, authKey);
2319
if (!HiRezAPI.sessions.containsKey(this.platform)) this.generateSession();
2420
if (!testSession().toString().startsWith("\"This was a successful test with the following parameters added:")) this.generateSession();
@@ -27,8 +23,6 @@ public Session(Smite.Platform platform, String devId, String authKey) {
2723
public Session(Paladins.Platform platform, String devId, String authKey) {
2824
super(platform.getUrl());
2925
this.platform = "PALADINS_" + platform.name();
30-
this.devId = devId;
31-
this.authKey = authKey;
3226
this.sig = new Signature(platform, devId, authKey);
3327
if (!HiRezAPI.sessions.containsKey(this.platform)) this.generateSession();
3428
if (!testSession().toString().startsWith("\"This was a successful test with the following parameters added:")) this.generateSession();
@@ -38,15 +32,16 @@ public StringData get(String endpoint, String... args) {
3832
return new StringData(request(this.sig.generateUrl(endpoint), args));
3933
}
4034

41-
public void generateSession() {
35+
public StringData generateSession() {
36+
StringData data = new StringData(request(this.sig.createSession()));
4237
try {
43-
StringData data = new StringData(request(this.sig.createSession()));
4438
if (data.toJsonObject().getString("ret_msg").equals("Approved")) {
4539
HiRezAPI.sessions.setProperty(this.platform, data.toJsonObject().getString("session_id"));
46-
} else throw new Exception(data.toJsonObject().getString("ret_msg"));
47-
} catch (Exception ex) {
40+
} else throw new SessionException(data.toJsonObject().getString("ret_msg"));
41+
} catch (SessionException ex) {
4842
ex.printStackTrace();
4943
}
44+
return data;
5045
}
5146

5247
public String testSession() {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pl.stachuofficial.hirezapi.api;
2+
3+
class SessionException extends Exception {
4+
5+
SessionException(String message) {
6+
super(message);
7+
}
8+
9+
SessionException(Throwable message) {
10+
super(message);
11+
}
12+
13+
}

src/main/java/pl/stachuofficial/hirezapi/api/StringData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public StringData(String string) {
2525
*/
2626
@Override
2727
public String toString() {
28-
return string;
28+
return string.replace("\\\"", "");
2929
}
3030

3131
/**

src/main/java/pl/stachuofficial/hirezapi/games/HiRez.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public StringData get(String endpoint, String... args) {
4141
* @return A quick way of validating access to the Hi-Rez API.
4242
*/
4343
public String ping() {
44-
return request("pingJson").replace("\\", "");
44+
return new StringData(request("pingJson")).toString();
4545
}
4646
/**
4747
* A required step to Authenticate the developerId/signature for further API use.
4848
*/
49-
public void createSession() { this.session.generateSession(); }
49+
public StringData createSession() { return this.session.generateSession(); }
5050
/**
5151
* Testing session when it is established.
5252
* @return A means of validating that a session is established.
@@ -193,8 +193,8 @@ public StringData getMatchIDsByQueue(Smite.Queue queue, Date date, boolean allDa
193193
SimpleDateFormat date_path = (allDay) ? new SimpleDateFormat("yyyyMMdd/-1") : new SimpleDateFormat("yyyyMMdd/HH");
194194
SimpleDateFormat minute = new SimpleDateFormat("mm");
195195
String min = String.valueOf(Integer.parseInt(minute.format(date)) - (Integer.parseInt(minute.format(date)) % 10));
196-
String path = date_path.format(date) + ((allDay) ? "," + min : "");
197-
return get("getmatchidsbyqueue", path);
196+
String path = date_path.format(date) + ((!allDay) ? "," + min : "");
197+
return get("getmatchidsbyqueue", Integer.toString(queue.getId()), path);
198198
}
199199

200200
/**
@@ -208,8 +208,8 @@ public StringData getMatchIDsByQueue(Paladins.Queue queue, Date date, boolean al
208208
SimpleDateFormat date_path = (allDay) ? new SimpleDateFormat("yyyyMMdd/-1") : new SimpleDateFormat("yyyyMMdd/HH");
209209
SimpleDateFormat minute = new SimpleDateFormat("mm");
210210
String min = String.valueOf(Integer.parseInt(minute.format(date)) - (Integer.parseInt(minute.format(date)) % 10));
211-
String path = date_path.format(date) + ((allDay) ? "," + min : "");
212-
return get("getmatchidsbyqueue", path);
211+
String path = date_path.format(date) + ((!allDay) ? "," + min : "");
212+
return get("getmatchidsbyqueue", Integer.toString(queue.getId()), path);
213213
}
214214

215215
/**
@@ -231,7 +231,7 @@ public StringData getLeagueLeaderboard(Smite.Queue queue, TierLeauge tier, Strin
231231
* @return Returns the top players for a particular league (as indicated by the queue/tier/season parameters).
232232
*/
233233
public StringData getLeagueLeaderboard(Paladins.Queue queue, TierLeauge tier, String season) {
234-
return get("getleagueleaderboard", String.valueOf(queue.getId()), String.valueOf(tier.getId()), season);
234+
return get("getleagueleaderboard", Integer.toString(queue.getId()), String.valueOf(tier.getId()), season);
235235
}
236236

237237
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package pl.stachuofficial.hirezapi.games.hirez;
2+
3+
import org.json.JSONObject;
4+
import pl.stachuofficial.hirezapi.api.StringData;
5+
6+
import java.util.Map;
7+
8+
public class Player {
9+
private final Map<String, Object> playerdata;
10+
11+
public Player(StringData player, StringData achievements) {
12+
JSONObject stats = achievements.toJsonObject();
13+
JSONObject user = player.toJsonObject();
14+
stats.remove("Id");
15+
String tag = user.getString("Name").replace(stats.getString("Name"), "");
16+
user.put("Name", stats.getString("Name"));
17+
user.put("Team_Tag", tag.substring(1, tag.length() - 1));
18+
stats.remove("Name");
19+
user.put("Stats", stats);
20+
21+
this.playerdata = user.toMap();
22+
}
23+
}

0 commit comments

Comments
 (0)