Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EveApi.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2009 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.evetool.api;
  23. import java.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.InputStreamReader;
  26. import java.sql.Connection;
  27. import java.sql.ResultSet;
  28. import java.sql.SQLException;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.logging.Level;
  32. import java.util.logging.Logger;
  33. import uk.co.md87.evetool.api.io.ApiDownloader;
  34. import uk.co.md87.evetool.api.io.DBCache;
  35. import uk.co.md87.evetool.api.parser.ApiElement;
  36. import uk.co.md87.evetool.api.parser.ApiParser;
  37. import uk.co.md87.evetool.api.parser.ApiResult;
  38. import uk.co.md87.evetool.api.wrappers.CharacterList;
  39. import uk.co.md87.evetool.api.wrappers.CharacterSheet;
  40. import uk.co.md87.evetool.api.wrappers.SkillInTraining;
  41. import uk.co.md87.evetool.api.wrappers.SkillList;
  42. /**
  43. * Allows access to the EVE Api (see http://api.eve-online.com/).
  44. *
  45. * @author chris
  46. */
  47. public class EveApi {
  48. /** SQL tables required by the API. */
  49. private static final String[] TABLES = {"PageCache"};
  50. /** Logger to use for this class. */
  51. private static final Logger LOGGER = Logger.getLogger(EveApi.class.getName());
  52. /** Date format for dates returned by the API. */
  53. public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
  54. /** The database connection to use. */
  55. private final Connection conn;
  56. /** The downloader to use. */
  57. private final ApiDownloader downloader;
  58. /** The client's user ID, if specified. */
  59. private String userID;
  60. /** The client's character ID, if specified. */
  61. private String charID;
  62. /** The client's API key, if specified. */
  63. private String apiKey;
  64. /**
  65. * Creates a new instance of the EVE API client using the specified database
  66. * connection.
  67. *
  68. * @param sqlConnection A connection to a database to use
  69. */
  70. public EveApi(final Connection sqlConnection) {
  71. this.conn = sqlConnection;
  72. checkTables();
  73. this.downloader = new ApiDownloader(new DBCache(conn), new ApiParser());
  74. }
  75. /**
  76. * Sets the API key used by this client.
  77. *
  78. * @param apiKey The user's API key
  79. */
  80. public void setApiKey(final String apiKey) {
  81. this.apiKey = apiKey;
  82. downloader.setApiKey(apiKey);
  83. }
  84. /**
  85. * Sets the character ID used by this client.
  86. *
  87. * @param charID The user's chosen character ID
  88. */
  89. public void setCharID(final String charID) {
  90. this.charID = charID;
  91. downloader.setCharID(charID);
  92. }
  93. /**
  94. * Sets the user ID used by this client.
  95. *
  96. * @param userID The user's user ID
  97. */
  98. public void setUserID(final String userID) {
  99. this.userID = userID;
  100. downloader.setUserID(userID);
  101. }
  102. /**
  103. * Retrieves the character list for the user's account.
  104. * Requires a limited API key and user ID.
  105. *
  106. * @return The user's character list
  107. */
  108. public ApiResponse<CharacterList> getCharacterList() {
  109. return getResponse("/account/Characters.xml.aspx", CharacterList.class, true, false);
  110. }
  111. /**
  112. * Retrieves the character sheet for the specified character.
  113. * Requires a limited API key, user ID and character ID.
  114. *
  115. * @return The character's extended information
  116. */
  117. public ApiResponse<CharacterSheet> getCharacterSheet() {
  118. return getResponse("/char/CharacterSheet.xml.aspx", CharacterSheet.class, true, true);
  119. }
  120. /**
  121. * Retrieves the skill that's currently in training.
  122. * Requires a limited API key, user ID and character ID.
  123. *
  124. * @return The character's currently training skill
  125. */
  126. public ApiResponse<SkillInTraining> getSkillInTraining() {
  127. return getResponse("/char/SkillInTraining.xml.aspx", SkillInTraining.class, true, true);
  128. }
  129. /**
  130. * Retrieves the full skill tree for EVE.
  131. * Does not require an API key.
  132. *
  133. * @return The complete EVE skill tree
  134. */
  135. public ApiResponse<SkillList> getSkillTree() {
  136. return getResponse("/eve/SkillTree.xml.aspx ", SkillList.class, false, true);
  137. }
  138. /**
  139. * Utility method to send a request to the API and manufacture the
  140. * appropaite response objects.
  141. *
  142. * @param <T> The type of object that will be returned on success
  143. * @param method The method (path) of the API being used
  144. * @param type The class of object to return on success
  145. * @param needKey Whether or not an API key is needed
  146. * @param needChar Whether or not a character ID is needed
  147. * @return An appropriate ApiResponse encapsulating the request
  148. */
  149. protected <T> ApiResponse<T> getResponse(final String method, final Class<T> type,
  150. final boolean needKey, final boolean needChar) {
  151. // TODO: Require userid + apikey
  152. final ApiResult result = downloader.getPage(method, null);
  153. if (result.wasSuccessful()) {
  154. try {
  155. return new ApiResponse<T>(type
  156. .getConstructor(ApiElement.class)
  157. .newInstance(result.getResultElement()), result);
  158. } catch (Exception ex) {
  159. LOGGER.log(Level.SEVERE, "Unable to create response object", ex);
  160. }
  161. return null;
  162. } else {
  163. return new ApiResponse<T>(result.getError(), result);
  164. }
  165. }
  166. // TODO: Abstract db maintenance
  167. // TODO: Version tables somehow
  168. /**
  169. * Creates the table with the specified name. SQL is read from the
  170. * <code>db</code> package.
  171. *
  172. * @param table The table to be created
  173. */
  174. protected void createTable(final String table) {
  175. LOGGER.log(Level.FINE, "Creating table " + table);
  176. final StringBuilder sql = new StringBuilder();
  177. final BufferedReader stream = new BufferedReader(new InputStreamReader(
  178. getClass().getResourceAsStream("db/" + table.toLowerCase() + ".sql")));
  179. String line;
  180. try {
  181. do {
  182. line = stream.readLine();
  183. if (line != null) {
  184. sql.append(line);
  185. }
  186. } while (line != null);
  187. } catch (IOException ex) {
  188. LOGGER.log(Level.SEVERE, "Error when reading SQL for table: " + table, ex);
  189. return;
  190. }
  191. try {
  192. conn.createStatement().execute(sql.toString());
  193. } catch (SQLException ex) {
  194. LOGGER.log(Level.SEVERE, "Error creating table: " + table, ex);
  195. }
  196. }
  197. /**
  198. * Checks to ensure that all required tables exist. If any table is missing,
  199. * it will be created.
  200. *
  201. * @see #createTable(java.lang.String)
  202. */
  203. protected void checkTables() {
  204. LOGGER.log(Level.FINEST, "Checking that tables exist");
  205. final List<String> tables = getTables();
  206. for (String table : TABLES) {
  207. if (!tables.contains(table.toUpperCase())) {
  208. createTable(table);
  209. }
  210. }
  211. LOGGER.log(Level.FINEST, "Done checking tables");
  212. }
  213. /**
  214. * Retrieves a list of tables that exist in the database.
  215. *
  216. * @return A list of table names that exist
  217. */
  218. protected List<String> getTables() {
  219. final List<String> tables = new ArrayList<String>();
  220. try {
  221. final ResultSet rs = conn.getMetaData().getTables(null, null, null, null);
  222. while (rs.next()) {
  223. tables.add(rs.getString("TABLE_NAME"));
  224. }
  225. rs.close();
  226. } catch (SQLException ex) {
  227. LOGGER.log(Level.SEVERE, "SQL Error when checking for tables", ex);
  228. }
  229. return tables;
  230. }
  231. }