Skip to content

Commit 55c7100

Browse files
author
Damian Staszewski
authored
Merge branch 'dev' into v2.x
2 parents dac7377 + e868be6 commit 55c7100

File tree

13 files changed

+1499
-0
lines changed

13 files changed

+1499
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package pl.stachuofficial.hirezapi;
2+
3+
import pl.stachuofficial.hirezapi.games.Paladins;
4+
import pl.stachuofficial.hirezapi.games.Smite;
5+
6+
import java.io.*;
7+
import java.nio.file.Paths;
8+
import java.util.Properties;
9+
10+
11+
/**
12+
* @author <a href="damian@stachuofficial.pl">Damian Staszewski</a>
13+
* @since 1.8
14+
*/
15+
public class HiRezAPI {
16+
private static final File filesession = Paths.get(System.getProperty("java.io.tmpdir"), ".sessions").toFile();
17+
18+
/**
19+
* Session keys for specific base API url type
20+
*/
21+
public static final Properties sessions = new Properties() {
22+
@Override
23+
public synchronized String setProperty(String key, String value) {
24+
put(key, value);
25+
try {
26+
HiRezAPI.store();
27+
} catch (IOException ex) {
28+
ex.printStackTrace();
29+
}
30+
return value;
31+
}
32+
};
33+
34+
/**
35+
* Developer ID (DevId)
36+
* @see HiRezAPI#HiRezAPI(String, String)
37+
*/
38+
private final String devId;
39+
40+
/**
41+
* Authorization Key (AuthKey)
42+
* @see HiRezAPI#HiRezAPI(String, String)
43+
*/
44+
private final String authKey;
45+
46+
/**
47+
* <p>Initialize Hi-Rez API. All stuff will delivered by Hi-Rez employer via E-Mail.</p>
48+
* <p>Please fill <a href="https://fs12.formsite.com/HiRez/form48/secure_index.html">this form first</a> to using script.</p>
49+
* <p>After acceptation your request, you can proceed to action.</p>
50+
* @param devId Developer ID (DevId)
51+
* @param authKey Authorization Key (AuthKey)
52+
*/
53+
public HiRezAPI(String devId, String authKey) {
54+
if (!Boolean.valueOf(System.getProperty("hirez.debug"))) {
55+
System.setProperty("hirez.debug","false");
56+
}
57+
this.devId = devId;
58+
this.authKey = authKey;
59+
try {
60+
initialize();
61+
} catch (Exception ex) {
62+
ex.printStackTrace();
63+
}
64+
}
65+
66+
private static void store() throws IOException {
67+
Writer writer = new FileWriter(filesession);
68+
sessions.store(writer, "");
69+
}
70+
71+
private void initialize() throws IOException {
72+
if (!filesession.exists()) {
73+
filesession.createNewFile();
74+
}
75+
Reader reader = new FileReader(filesession);
76+
sessions.load(reader);
77+
}
78+
79+
/**
80+
* Smite API
81+
* @param platform Platform
82+
* @return {@link Smite}
83+
*/
84+
public Smite smite(Smite.Platform platform) {
85+
return new Smite(platform, devId, authKey);
86+
}
87+
88+
/**
89+
* Paladins API
90+
* @param platform Platform
91+
* @return {@link Paladins}
92+
*/
93+
public Paladins paladins(Paladins.Platform platform) {
94+
return new Paladins(platform, devId, authKey);
95+
}
96+
97+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package pl.stachuofficial.hirezapi.api;
2+
3+
import okhttp3.OkHttpClient;
4+
import okhttp3.Request;
5+
import okhttp3.Response;
6+
7+
import java.util.Arrays;
8+
import java.util.concurrent.TimeUnit;
9+
10+
/**
11+
* @author <a href="damian@stachuofficial.pl">Damian Staszewski</a>
12+
* @since 1.8
13+
*/
14+
public class HttpClient {
15+
private final String base_url;
16+
private final int timeout = 10; // default timeout is 10 seconds
17+
private final OkHttpClient client;
18+
19+
public HttpClient(String base_url) {
20+
this.base_url = base_url;
21+
this.client = new OkHttpClient.Builder()
22+
.connectTimeout(timeout, TimeUnit.SECONDS)
23+
.readTimeout(timeout, TimeUnit.SECONDS)
24+
.writeTimeout(timeout, TimeUnit.SECONDS)
25+
.build();
26+
}
27+
28+
protected String request(String endpoint, String... args) {
29+
try {
30+
String url = (base_url.endsWith("/")) ? base_url : base_url + "/";
31+
url += (endpoint.startsWith("/")) ? endpoint.substring(1) : endpoint;
32+
if (!url.endsWith("/") && Arrays.asList(args).toArray().length > 0) {
33+
url += "/";
34+
}
35+
url += (Arrays.asList(args).toArray().length > 0) ?
36+
String.join("/", Arrays.asList(args)) :
37+
"";
38+
if (Boolean.valueOf(System.getProperty("hirez.debug"))) {
39+
System.out.println(url);
40+
}
41+
42+
Request request = new Request.Builder()
43+
.url(url)
44+
.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")
45+
.build();
46+
47+
Response response = client.newCall(request).execute();
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+
}
55+
56+
return response.body().string();
57+
} catch (Exception ex) {
58+
ex.printStackTrace();
59+
return null;
60+
}
61+
62+
}
63+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package pl.stachuofficial.hirezapi.api;
2+
3+
import pl.stachuofficial.hirezapi.HiRezAPI;
4+
import pl.stachuofficial.hirezapi.games.Paladins;
5+
import pl.stachuofficial.hirezapi.games.Smite;
6+
7+
/**
8+
* @author <a href="damian@stachuofficial.pl">Damian Staszewski</a>
9+
* @since 1.8
10+
*/
11+
public class Session extends HttpClient {
12+
private final String platform;
13+
private final Signature sig;
14+
15+
public Session(Smite.Platform platform, String devId, String authKey) {
16+
super(platform.getUrl());
17+
this.platform = "SMITE_" + platform.name();
18+
this.sig = new Signature(platform, devId, authKey);
19+
if (!HiRezAPI.sessions.containsKey(this.platform)) this.generateSession();
20+
if (!testSession().toString().startsWith("\"This was a successful test with the following parameters added:")) this.generateSession();
21+
}
22+
23+
public Session(Paladins.Platform platform, String devId, String authKey) {
24+
super(platform.getUrl());
25+
this.platform = "PALADINS_" + platform.name();
26+
this.sig = new Signature(platform, devId, authKey);
27+
if (!HiRezAPI.sessions.containsKey(this.platform)) this.generateSession();
28+
if (!testSession().toString().startsWith("\"This was a successful test with the following parameters added:")) this.generateSession();
29+
}
30+
31+
public StringData get(String endpoint, String... args) {
32+
return new StringData(request(this.sig.generateUrl(endpoint), args));
33+
}
34+
35+
public StringData generateSession() {
36+
StringData data = new StringData(request(this.sig.createSession()));
37+
try {
38+
if (data.toJsonObject().getString("ret_msg").equals("Approved")) {
39+
HiRezAPI.sessions.setProperty(this.platform, data.toJsonObject().getString("session_id"));
40+
} else throw new SessionException(data.toJsonObject().getString("ret_msg"));
41+
} catch (SessionException ex) {
42+
ex.printStackTrace();
43+
}
44+
return data;
45+
}
46+
47+
public String testSession() {
48+
return get("testsession").toString();
49+
}
50+
51+
public StringData getDataUsage() {
52+
return get("getdataused");
53+
}
54+
}
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+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package pl.stachuofficial.hirezapi.api;
2+
3+
import pl.stachuofficial.hirezapi.HiRezAPI;
4+
import pl.stachuofficial.hirezapi.games.Paladins;
5+
import pl.stachuofficial.hirezapi.games.Smite;
6+
7+
import java.security.MessageDigest;
8+
import java.text.SimpleDateFormat;
9+
import java.util.Date;
10+
import java.util.SimpleTimeZone;
11+
12+
public class Signature {
13+
private final String devId;
14+
private final String authKey;
15+
private final String platform;
16+
17+
Signature(Smite.Platform platform, String devId, String authKey) {
18+
this.devId = devId;
19+
this.authKey = authKey;
20+
this.platform = "SMITE_" + platform.name();
21+
}
22+
23+
Signature(Paladins.Platform platform, String devId, String authKey) {
24+
this.devId = devId;
25+
this.authKey = authKey;
26+
this.platform = "PALADINS_" + platform.name();
27+
}
28+
29+
private String generateTimestamp() {
30+
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
31+
sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
32+
return sdf.format(new Date());
33+
}
34+
35+
private String generateSignature(String method) {
36+
try {
37+
String sig = devId + method + authKey + generateTimestamp();
38+
StringBuilder sb = new StringBuilder();
39+
40+
41+
MessageDigest md = MessageDigest.getInstance("MD5");
42+
md.update(sig.getBytes());
43+
byte bytes[] = md.digest();
44+
45+
for (byte bit : bytes) {
46+
String hex = Integer.toHexString(0xff & bit);
47+
if (hex.length() == 1) sb.append("0");
48+
sb.append(hex);
49+
}
50+
return sb.toString();
51+
} catch (Exception ex) {
52+
ex.printStackTrace();
53+
return null;
54+
}
55+
}
56+
57+
public String generateUrl(String method) {
58+
String url = method + "Json/";
59+
url += devId + "/";
60+
url += generateSignature(method) + "/";
61+
url += HiRezAPI.sessions.getProperty(platform) + "/";
62+
url += generateTimestamp();
63+
64+
return url;
65+
}
66+
67+
public String createSession() {
68+
String url = "createsessionJson/";
69+
url += devId + "/";
70+
url += generateSignature("createsession") + "/";
71+
url += generateTimestamp();
72+
73+
return url;
74+
}
75+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package pl.stachuofficial.hirezapi.api;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
/**
7+
* @author <a href="damian@stachuofficial.pl">Damian Staszewski</a>
8+
* @since 1.8
9+
*/
10+
public class StringData {
11+
12+
private final String string;
13+
14+
/**
15+
* String Data
16+
* @param string string
17+
*/
18+
public StringData(String string) {
19+
this.string = string;
20+
}
21+
22+
/**
23+
* Getting data to {@link String}
24+
* @return {@link String}
25+
*/
26+
@Override
27+
public String toString() {
28+
return string.replace("\\\"", "");
29+
}
30+
31+
/**
32+
* Getting data to {@link JSONArray}
33+
* @return {@link JSONArray}
34+
*/
35+
public JSONArray toJsonArray() {
36+
return new JSONArray(string);
37+
}
38+
39+
/**
40+
* Getting data to {@link JSONObject}
41+
* @return {@link JSONObject}
42+
*/
43+
public JSONObject toJsonObject() {
44+
return new JSONObject(string);
45+
}
46+
47+
// TODO: supporting on next release.
48+
// /**
49+
// * Getting data to XML {@link Document}
50+
// * @return {@link Document}
51+
// */
52+
// public Document toXmlData() {
53+
// try {
54+
// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
55+
// DocumentBuilder builder;
56+
//
57+
// builder = factory.newDocumentBuilder();
58+
// return builder.parse(new InputSource(new StringReader(string)));
59+
// } catch (Exception ex) {
60+
// ex.printStackTrace();
61+
// return null;
62+
// }
63+
// }
64+
}

0 commit comments

Comments
 (0)