Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.rabrg.smite;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.nio.charset.Charset;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.TimeZone;
- import me.rabrg.smite.response.LeagueLeaderboardResponse;
- import me.rabrg.smite.response.SessionResponse;
- import com.google.gson.Gson;
- /**
- * The core class of the Smite API.
- * @author Ryan Greene
- *
- */
- public final class SmiteApi {
- /**
- * An instance of the UTF-8 charset.
- */
- private static final Charset UTF_8_CHARSET = Charset.forName("UTF-8");
- /**
- * The date format used by Smite's API.
- */
- private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
- /**
- * The developer's identification number.
- */
- private final int devId;
- /**
- * The developer's authentication key.
- */
- private final String authKey;
- /**
- * The MessageDigest instance used to create signatures.
- */
- private final MessageDigest messageDigest;
- /**
- * The Gson instance for deserializing responses.
- */
- private final Gson gson;
- /**
- * The current session id.
- */
- private String sessionId;
- /**
- * Sets the DateFormat instanc's timezone to Smite API's.
- */
- static {
- DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
- }
- /**
- * Constructs a new SmiteAPI instance with the specified developer credentials.
- * @param devId The developer's identification number.
- * @param authKey The developer's authentication key.
- */
- public SmiteApi(final int devId, final String authKey) {
- this.devId = devId;
- this.authKey = authKey;
- try {
- messageDigest = MessageDigest.getInstance("MD5");
- } catch (NoSuchAlgorithmException e) {
- throw new IllegalStateException("The provider for the MD5 algorithm does not exist");
- }
- gson = new Gson();
- createNewSession();
- }
- /**
- * Creates a new session, setting the session id for future requests.
- */
- public void createNewSession() {
- sessionId = getSessionResponse().getSessionId();
- }
- public SessionResponse getSessionResponse() {
- return gson.fromJson(getTextResponse("createsession"), SessionResponse.class);
- }
- public LeagueLeaderboardResponse[] getLeagueLeaderboardResponse(final int queue, final int tier, final int season) {
- return gson.fromJson(getTextResponse("getleagueleaderboard", "" + queue, "" + tier, "" + season), LeagueLeaderboardResponse[].class);
- }
- private String getTextResponse(final String method, final String... arguments) {
- final StringBuffer urlStringBuffer = new StringBuffer("http://api.smitegame.com/smiteapi.svc/").append(method).append("json");
- final String date = getDate();
- if (!method.equals("ping")) {
- urlStringBuffer.append('/').append(devId).append('/').append(createSignature(method, date));
- if (!method.equals("createsession")) {
- urlStringBuffer.append('/').append(sessionId);
- }
- urlStringBuffer.append('/').append(date);
- for (final String argument : arguments) {
- urlStringBuffer.append("/" + argument);
- }
- }
- System.out.println(urlStringBuffer);
- try {
- final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(urlStringBuffer.toString()).openStream()));
- final StringBuffer response = new StringBuffer();
- String line;
- while((line = reader.readLine()) != null) {
- response.append(line);
- }
- return response.toString();
- } catch (IOException e) {
- throw new IllegalStateException("Could not get response for method " + method + " and arguments " + arguments);
- }
- }
- /**
- * Creates a signature for the specified method name and with specified date.
- * @param methodName The name of the method the signature is created for.
- * @param date The date the signature is created at.
- * @return The created signature.
- */
- private String createSignature(final String methodName, final String date) {
- final StringBuilder signature = new StringBuilder();
- for (final byte b : messageDigest.digest(new StringBuffer().append(devId).append(methodName).append(authKey).append(date).toString().getBytes(UTF_8_CHARSET))) {
- signature.append(String.format("%02x", b).replaceAll("\\s", ""));
- }
- return signature.toString();
- }
- /**
- * Gets the developer's identification number.
- * @return The developer's identification number.
- */
- public int getDevId() {
- return devId;
- }
- /**
- * Gets the developer's authentication key.
- * @return The developer's authentication key.
- */
- public String getAuthKey() {
- return authKey;
- }
- /**
- * Gets the current formated date.
- * @return The current formated date.
- */
- public String getDate() {
- return DATE_FORMAT.format(new Date());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement