Advertisement
Chiddix

Smite API v2.0

Dec 18th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. package me.rabrg.smite;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7. import java.nio.charset.Charset;
  8. import java.security.MessageDigest;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.text.DateFormat;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Date;
  13. import java.util.TimeZone;
  14.  
  15. import me.rabrg.smite.response.LeagueLeaderboardResponse;
  16. import me.rabrg.smite.response.SessionResponse;
  17.  
  18. import com.google.gson.Gson;
  19.  
  20. /**
  21.  * The core class of the Smite API.
  22.  * @author Ryan Greene
  23.  *
  24.  */
  25. public final class SmiteApi {
  26.  
  27.     /**
  28.      * An instance of the UTF-8 charset.
  29.      */
  30.     private static final Charset UTF_8_CHARSET = Charset.forName("UTF-8");
  31.  
  32.     /**
  33.      * The date format used by Smite's API.
  34.      */
  35.     private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
  36.  
  37.     /**
  38.      * The developer's identification number.
  39.      */
  40.     private final int devId;
  41.  
  42.     /**
  43.      * The developer's authentication key.
  44.      */
  45.     private final String authKey;
  46.  
  47.     /**
  48.      * The MessageDigest instance used to create signatures.
  49.      */
  50.     private final MessageDigest messageDigest;
  51.  
  52.     /**
  53.      * The Gson instance for deserializing responses.
  54.      */
  55.     private final Gson gson;
  56.  
  57.     /**
  58.      * The current session id.
  59.      */
  60.     private String sessionId;
  61.  
  62.     /**
  63.      * Sets the DateFormat instanc's timezone to Smite API's.
  64.      */
  65.     static {
  66.         DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
  67.     }
  68.  
  69.     /**
  70.      * Constructs a new SmiteAPI instance with the specified developer credentials.
  71.      * @param devId The developer's identification number.
  72.      * @param authKey The developer's authentication key.
  73.      */
  74.     public SmiteApi(final int devId, final String authKey) {
  75.         this.devId = devId;
  76.         this.authKey = authKey;
  77.         try {
  78.             messageDigest = MessageDigest.getInstance("MD5");
  79.         } catch (NoSuchAlgorithmException e) {
  80.             throw new IllegalStateException("The provider for the MD5 algorithm does not exist");
  81.         }
  82.         gson = new Gson();
  83.         createNewSession();
  84.     }
  85.  
  86.     /**
  87.      * Creates a new session, setting the session id for future requests.
  88.      */
  89.     public void createNewSession() {
  90.         sessionId = getSessionResponse().getSessionId();
  91.     }
  92.  
  93.     public SessionResponse getSessionResponse() {
  94.         return gson.fromJson(getTextResponse("createsession"), SessionResponse.class);
  95.     }
  96.  
  97.     public LeagueLeaderboardResponse[] getLeagueLeaderboardResponse(final int queue, final int tier, final int season) {
  98.         return gson.fromJson(getTextResponse("getleagueleaderboard", "" + queue, "" + tier, "" + season), LeagueLeaderboardResponse[].class);
  99.     }
  100.  
  101.     private String getTextResponse(final String method, final String... arguments) {
  102.         final StringBuffer urlStringBuffer = new StringBuffer("http://api.smitegame.com/smiteapi.svc/").append(method).append("json");
  103.         final String date = getDate();
  104.         if (!method.equals("ping")) {
  105.             urlStringBuffer.append('/').append(devId).append('/').append(createSignature(method, date));
  106.             if (!method.equals("createsession")) {
  107.                 urlStringBuffer.append('/').append(sessionId);
  108.             }
  109.             urlStringBuffer.append('/').append(date);
  110.             for (final String argument : arguments) {
  111.                 urlStringBuffer.append("/" + argument);
  112.             }
  113.         }
  114.         System.out.println(urlStringBuffer);
  115.         try {
  116.             final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(urlStringBuffer.toString()).openStream()));
  117.             final StringBuffer response = new StringBuffer();
  118.             String line;
  119.             while((line = reader.readLine()) != null) {
  120.                 response.append(line);
  121.             }
  122.             return response.toString();
  123.         } catch (IOException e) {
  124.             throw new IllegalStateException("Could not get response for method " + method + " and arguments " + arguments);
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Creates a signature for the specified method name and with specified date.
  130.      * @param methodName The name of the method the signature is created for.
  131.      * @param date The date the signature is created at.
  132.      * @return The created signature.
  133.      */
  134.     private String createSignature(final String methodName, final String date) {
  135.         final StringBuilder signature = new StringBuilder();
  136.         for (final byte b : messageDigest.digest(new StringBuffer().append(devId).append(methodName).append(authKey).append(date).toString().getBytes(UTF_8_CHARSET))) {
  137.             signature.append(String.format("%02x", b).replaceAll("\\s", ""));
  138.         }
  139.         return signature.toString();
  140.     }
  141.  
  142.     /**
  143.      * Gets the developer's identification number.
  144.      * @return The developer's identification number.
  145.      */
  146.     public int getDevId() {
  147.         return devId;
  148.     }
  149.  
  150.     /**
  151.      * Gets the developer's authentication key.
  152.      * @return The developer's authentication key.
  153.      */
  154.     public String getAuthKey() {
  155.         return authKey;
  156.     }
  157.  
  158.     /**
  159.      * Gets the current formated date.
  160.      * @return The current formated date.
  161.      */
  162.     public String getDate() {
  163.         return DATE_FORMAT.format(new Date());
  164.     }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement