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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.util.HashMap;
  24. import java.util.Map;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import uk.co.md87.evetool.api.io.ApiCache;
  28. import uk.co.md87.evetool.api.io.ApiDownloader;
  29. import uk.co.md87.evetool.api.parser.ApiElement;
  30. import uk.co.md87.evetool.api.parser.ApiParser;
  31. import uk.co.md87.evetool.api.parser.ApiResult;
  32. import uk.co.md87.evetool.api.wrappers.CertificateTree;
  33. import uk.co.md87.evetool.api.wrappers.CharacterList;
  34. import uk.co.md87.evetool.api.wrappers.CharacterSheet;
  35. import uk.co.md87.evetool.api.wrappers.MarketOrders;
  36. import uk.co.md87.evetool.api.wrappers.RaceList;
  37. import uk.co.md87.evetool.api.wrappers.ShipList;
  38. import uk.co.md87.evetool.api.wrappers.SkillInTraining;
  39. import uk.co.md87.evetool.api.wrappers.SkillList;
  40. import uk.co.md87.evetool.api.wrappers.SkillQueue;
  41. /**
  42. * Allows access to the EVE Api (see http://api.eve-online.com/).
  43. *
  44. * @author chris
  45. */
  46. public class EveApi implements Cloneable {
  47. /** Logger to use for this class. */
  48. private static final Logger LOGGER = Logger.getLogger(EveApi.class.getName());
  49. /** Date format for dates returned by the API. */
  50. public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
  51. /** The downloader to use. */
  52. private final ApiDownloader downloader;
  53. /** The cache to use. */
  54. private final ApiCache cache;
  55. /** The client's user ID, if specified. */
  56. private int userID;
  57. /** The client's character ID, if specified. */
  58. private int charID;
  59. /** The client's API key, if specified. */
  60. private String apiKey;
  61. /**
  62. * Creates a new instance of the EVE API client using the specified database
  63. * connection.
  64. *
  65. * @param cache The Cache implementation to use.
  66. */
  67. public EveApi(final ApiCache cache) {
  68. this.cache = cache;
  69. this.downloader = new ApiDownloader(cache, new ApiParser());
  70. }
  71. /**
  72. * Sets the API key used by this client.
  73. *
  74. * @param apiKey The user's API key
  75. */
  76. public void setApiKey(final String apiKey) {
  77. this.apiKey = apiKey;
  78. }
  79. /**
  80. * Sets the character ID used by this client.
  81. *
  82. * @param charID The user's chosen character ID
  83. */
  84. public void setCharID(final int charID) {
  85. this.charID = charID;
  86. }
  87. /**
  88. * Sets the user ID used by this client.
  89. *
  90. * @param userID The user's user ID
  91. */
  92. public void setUserID(final int userID) {
  93. this.userID = userID;
  94. }
  95. /**
  96. * Retrieves the character list for the user's account.
  97. * Requires a limited API key and user ID.
  98. *
  99. * @return The user's character list
  100. */
  101. public ApiResponse<CharacterList> getCharacterList() {
  102. return getResponse("/account/Characters.xml.aspx", CharacterList.class, true, false);
  103. }
  104. /**
  105. * Retrieves the character sheet for the specified character.
  106. * Requires a limited API key, user ID and character ID.
  107. *
  108. * @return The character's extended information
  109. */
  110. public ApiResponse<CharacterSheet> getCharacterSheet() {
  111. return getResponse("/char/CharacterSheet.xml.aspx", CharacterSheet.class, true, true);
  112. }
  113. /**
  114. * Retrieves the skill that's currently in training.
  115. * Requires a limited API key, user ID and character ID.
  116. *
  117. * @return The character's currently training skill
  118. */
  119. public ApiResponse<SkillInTraining> getSkillInTraining() {
  120. return getResponse("/char/SkillInTraining.xml.aspx", SkillInTraining.class, true, true);
  121. }
  122. /**
  123. * Retrieves the skill queue for the specified character.
  124. * Requires a limited API key, user ID and character ID.
  125. *
  126. * @return The character's current skill queue
  127. */
  128. public ApiResponse<SkillQueue> getSkillQueue() {
  129. return getResponse("/char/SkillQueue.xml.aspx", SkillQueue.class, true, true);
  130. }
  131. /**
  132. * Retrieves a list of market orders placed by the character.
  133. * Requires a limited API key, user ID and character ID.
  134. *
  135. * @return The character's market orders
  136. */
  137. public ApiResponse<MarketOrders> getMarketOrders() {
  138. return getResponse("/char/MarketOrders.xml.aspx", MarketOrders.class, true, true);
  139. }
  140. /**
  141. * Retrieves the full skill tree for EVE.
  142. * Does not require an API key.
  143. *
  144. * @return The complete EVE skill tree
  145. */
  146. public ApiResponse<SkillList> getSkillTree() {
  147. return getResponse("/eve/SkillTree.xml.aspx", SkillList.class, false, false);
  148. }
  149. /**
  150. * Retrieves the full certificate tree for EVE.
  151. * Does not require an API key.
  152. *
  153. * @return The complete EVE certificate tree
  154. */
  155. public ApiResponse<CertificateTree> getCertificateTree() {
  156. return getResponse("/eve/CertificateTree.xml.aspx", CertificateTree.class, false, false);
  157. }
  158. /**
  159. * Retrieves a list of ships in EVE.
  160. * Does not require an API key.
  161. *
  162. * @return A list of ships in EVE
  163. */
  164. public ApiResponse<ShipList> getShipList() {
  165. return getResponse("http://evetool.md87.co.uk/api/types.php?category=6"
  166. + "&published&attributes=requiredSkill%&groups", ShipList.class, false, false);
  167. }
  168. /**
  169. * Retrieves a list of races in EVE.
  170. * Does not require an API key.
  171. *
  172. * @return A list of races in EVE
  173. */
  174. public ApiResponse<RaceList> getRaceList() {
  175. return getResponse("http://evetool.md87.co.uk/api/races.php",
  176. RaceList.class, false, false);
  177. }
  178. /**
  179. * Utility method to send a request to the API and manufacture the
  180. * appropaite response objects.
  181. *
  182. * @param <T> The type of object that will be returned on success
  183. * @param method The method (path) of the API being used
  184. * @param type The class of object to return on success
  185. * @param needKey Whether or not an API key is needed
  186. * @param needChar Whether or not a character ID is needed
  187. * @return An appropriate ApiResponse encapsulating the request
  188. */
  189. protected <T> ApiResponse<T> getResponse(final String method, final Class<T> type,
  190. final boolean needKey, final boolean needChar) {
  191. final Map<String, String> args = new HashMap<String, String>();
  192. if (needKey) {
  193. args.put("userID", String.valueOf(userID));
  194. args.put("apiKey", apiKey);
  195. if (needChar) {
  196. args.put("characterID", String.valueOf(charID));
  197. }
  198. }
  199. final ApiResult result = downloader.getPage(method, args);
  200. if (result.wasSuccessful()) {
  201. try {
  202. return new ApiResponse<T>(type
  203. .getConstructor(ApiElement.class)
  204. .newInstance(result.getResultElement()), result);
  205. } catch (Exception ex) {
  206. LOGGER.log(Level.SEVERE, "Unable to create response object", ex);
  207. }
  208. return null;
  209. } else {
  210. return new ApiResponse<T>(result.getError(), result);
  211. }
  212. }
  213. /** {@inheritDoc} */
  214. @Override
  215. public EveApi clone() {
  216. final EveApi copy = new EveApi(cache);
  217. copy.setApiKey(apiKey);
  218. copy.setUserID(userID);
  219. copy.setCharID(charID);
  220. return copy;
  221. }
  222. }