PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/connector/src/main/java/com/restfb/FacebookClient.java

https://github.com/chrbayer84/GoodData-CL
Java | 298 lines | 42 code | 20 blank | 236 comment | 1 complexity | 8e026cf9854ad4d6e9431063e8b367d7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (c) 2010-2011 Mark Allen.
  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
  20. * THE SOFTWARE.
  21. */
  22. package com.restfb;
  23. import com.restfb.util.ReflectionUtils;
  24. import java.io.InputStream;
  25. import java.util.Date;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * Specifies how a <a href="http://developers.facebook.com/docs/api">Facebook
  30. * Graph API</a> client must operate.
  31. * <p/>
  32. * Projects that need to access the <a
  33. * href="http://developers.facebook.com/docs/reference/rest/">old REST API</a>
  34. * should use {@link LegacyFacebookClient} instead. You might choose to do this
  35. * because you have a legacy codebase or you need functionality that is not yet
  36. * available in the Graph API.
  37. * <p/>
  38. * If you'd like to...
  39. * <p/>
  40. * <ul>
  41. * <li>Fetch an object: use {@link #fetchObject(String, Class, Parameter...)} or
  42. * {@link #fetchObjects(java.util.List, Class, Parameter...)}</li>
  43. * <li>Fetch a connection: use
  44. * {@link #fetchConnection(String, Class, Parameter...)}</li>
  45. * <li>Execute an FQL query: use
  46. * {@link #executeQuery(String, Class, Parameter...)} or
  47. * {@link #executeMultiquery(java.util.Map, Class, Parameter...)}</li>
  48. * <li>Publish data: use {@link #publish(String, Class, Parameter...)} or
  49. * {@link #publish(String, Class, java.io.InputStream, Parameter...)}</li>
  50. * <li>Delete an object: use {@link #deleteObject(String)}</li>
  51. * </ul>
  52. *
  53. * @author <a href="http://restfb.com">Mark Allen</a>
  54. * @author Scott Hernandez
  55. * @author raibaz
  56. */
  57. public interface FacebookClient {
  58. /**
  59. * Fetches a single <a
  60. * href="http://developers.facebook.com/docs/reference/api/">Graph API
  61. * object</a>, mapping the result to an instance of {@code objectType}.
  62. *
  63. * @param <T> Java type to map to.
  64. * @param object ID of the object to fetch, e.g. {@code "me"}.
  65. * @param objectType Object type token.
  66. * @param parameters URL parameters to include in the API call (optional).
  67. * @return An instance of type {@code objectType} which contains the requested
  68. * object's data.
  69. * @throws com.restfb.exception.FacebookException
  70. * If an error occurs while performing the API call.
  71. */
  72. <T> T fetchObject(String object, Class<T> objectType, Parameter... parameters);
  73. /**
  74. * Fetches multiple <a
  75. * href="http://developers.facebook.com/docs/reference/api/">Graph API
  76. * objects</a> in a single call, mapping the results to an instance of
  77. * {@code objectType}.
  78. * <p/>
  79. * You'll need to write your own container type ({@code objectType}) to hold
  80. * the results. See <a href="http://restfb.com">http://restfb.com</a> for an
  81. * example of how to do this.
  82. *
  83. * @param <T> Java type to map to.
  84. * @param ids IDs of the objects to fetch, e.g. {@code "me", "arjun"}.
  85. * @param objectType Object type token.
  86. * @param parameters URL parameters to include in the API call (optional).
  87. * @return An instance of type {@code objectType} which contains the requested
  88. * objects' data.
  89. * @throws com.restfb.exception.FacebookException
  90. * If an error occurs while performing the API call.
  91. */
  92. <T> T fetchObjects(List<String> ids, Class<T> objectType, Parameter... parameters);
  93. /**
  94. * Fetches a Graph API {@code Connection} type, mapping the result to an
  95. * instance of {@code connectionType}.
  96. *
  97. * @param <T> Java type to map to.
  98. * @param connection The name of the connection, e.g. {@code "me/feed"}.
  99. * @param connectionType Connection type token.
  100. * @param parameters URL parameters to include in the API call (optional).
  101. * @return An instance of type {@code connectionType} which contains the
  102. * requested Connection's data.
  103. * @throws com.restfb.exception.FacebookException
  104. * If an error occurs while performing the API call.
  105. */
  106. <T> Connection<T> fetchConnection(String connection, Class<T> connectionType, Parameter... parameters);
  107. /**
  108. * Fetches a previous/next page of a Graph API {@code Connection} type,
  109. * mapping the result to an instance of {@code connectionType}.
  110. *
  111. * @param <T> Java type to map to.
  112. * @param connectionPageUrl The URL of the connection page to fetch, usually retrieved via
  113. * {@link com.restfb.Connection#getPreviousPageUrl()} or
  114. * {@link com.restfb.Connection#getNextPageUrl()}.
  115. * @param connectionType Connection type token.
  116. * @return An instance of type {@code connectionType} which contains the
  117. * requested Connection's data.
  118. * @throws com.restfb.exception.FacebookException
  119. * If an error occurs while performing the API call.
  120. */
  121. <T> Connection<T> fetchConnectionPage(String connectionPageUrl, Class<T> connectionType);
  122. /**
  123. * Executes an <a
  124. * href="http://developers.facebook.com/docs/reference/fql/">FQL query</a>,
  125. * mapping the resultset to a {@code List} of instances of {@code objectType}.
  126. *
  127. * @param <T> Java type to map to.
  128. * @param query The FQL query to execute, e.g.
  129. * {@code "SELECT name FROM user WHERE uid=220439 or uid=7901103"}.
  130. * @param objectType Resultset object type token.
  131. * @param parameters URL parameters to include in the API call (optional).
  132. * @return A list of instances of {@code objectType} which map to the query
  133. * results.
  134. * @throws com.restfb.exception.FacebookException
  135. * If an error occurs while performing the API call.
  136. */
  137. <T> List<T> executeQuery(String query, Class<T> objectType, Parameter... parameters);
  138. /**
  139. * Executes an <a
  140. * href="http://developers.facebook.com/docs/reference/fql/">FQL
  141. * multiquery</a>, which allows you to batch multiple queries into a single
  142. * request.
  143. * <p/>
  144. * You'll need to write your own container type ({@code objectType}) to hold
  145. * the results. See <a href="http://restfb.com">http://restfb.com</a> for an
  146. * example of how to do this.
  147. *
  148. * @param <T> Java type to map to.
  149. * @param queries A mapping of query names to queries. This is marshaled to JSON and
  150. * sent over the wire to the Facebook API endpoint as the
  151. * {@code queries} parameter.
  152. * @param objectType Object type token.
  153. * @param parameters URL parameters to include in the API call (optional).
  154. * @return An instance of type {@code objectType} which contains the requested
  155. * objects' data.
  156. * @throws com.restfb.exception.FacebookException
  157. * If an error occurs while performing the API call.
  158. */
  159. <T> T executeMultiquery(Map<String, String> queries, Class<T> objectType, Parameter... parameters);
  160. /**
  161. * Performs a <a
  162. * href="http://developers.facebook.com/docs/api#publishing">Graph API
  163. * publish</a> operation on the given {@code connection}, mapping the result
  164. * to an instance of {@code objectType}.
  165. *
  166. * @param <T> Java type to map to.
  167. * @param connection The Connection to publish to.
  168. * @param objectType Object type token.
  169. * @param parameters URL parameters to include in the API call.
  170. * @return An instance of type {@code objectType} which contains the Facebook
  171. * response to your publish request.
  172. * @throws com.restfb.exception.FacebookException
  173. * If an error occurs while performing the API call.
  174. */
  175. <T> T publish(String connection, Class<T> objectType, Parameter... parameters);
  176. /**
  177. * Performs a <a
  178. * href="http://developers.facebook.com/docs/api#publishing">Graph API
  179. * publish</a> operation on the given {@code connection} and includes a file -
  180. * a photo, for example - in the publish request, and mapping the result to an
  181. * instance of {@code objectType}.
  182. *
  183. * @param <T> Java type to map to.
  184. * @param connection The Connection to publish to.
  185. * @param objectType Object type token.
  186. * @param binaryAttachment The file to include in the publish request - a photo, for example.
  187. * @param parameters URL parameters to include in the API call.
  188. * @return An instance of type {@code objectType} which contains the Facebook
  189. * response to your publish request.
  190. * @throws com.restfb.exception.FacebookException
  191. * If an error occurs while performing the API call.
  192. */
  193. <T> T publish(String connection, Class<T> objectType, InputStream binaryAttachment, Parameter... parameters);
  194. /**
  195. * Performs a <a href="http://developers.facebook.com/docs/api#deleting">Graph
  196. * API delete</a> operation on the given {@code object}.
  197. *
  198. * @param object The ID of the object to delete.
  199. * @return {@code true} if Facebook indicated that the object was successfully
  200. * deleted, {@code false} otherwise.
  201. * @throws com.restfb.exception.FacebookException
  202. * If an error occurred while attempting to delete the object.
  203. */
  204. boolean deleteObject(String object);
  205. /**
  206. * Converts an arbitrary number of {@code sessionKeys} to OAuth access tokens.
  207. * <p/>
  208. * See the <a
  209. * href="http://developers.facebook.com/docs/guides/upgrade">Facebook Platform
  210. * Upgrade Guide</a> for details on how this process works and why you should
  211. * convert your application's session keys if you haven't already.
  212. *
  213. * @param appId A Facebook application ID.
  214. * @param secretKey A Facebook application secret key.
  215. * @param sessionKeys The Old REST API session keys to be converted to OAuth access
  216. * tokens.
  217. * @return A list of access tokens ordered to correspond to the
  218. * {@code sessionKeys} argument list.
  219. * @throws com.restfb.exception.FacebookException
  220. * If an error occurs while attempting to convert the session keys
  221. * to API keys.
  222. * @since 1.6
  223. */
  224. List<AccessToken> convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys);
  225. /**
  226. * Represents an access token/expiration date pair.
  227. * <p/>
  228. * Facebook returns these types when converting from legacy session keys to
  229. * OAuth access tokens - see
  230. * {@link FacebookClient#convertSessionKeysToAccessTokens(String, String, String...)}
  231. * for details.
  232. *
  233. * @author <a href="http://restfb.com">Mark Allen</a>
  234. */
  235. public static class AccessToken {
  236. @Facebook("access_token")
  237. private String accessToken;
  238. @Facebook
  239. private Long expires;
  240. /**
  241. * @see Object#hashCode()
  242. */
  243. @Override
  244. public int hashCode() {
  245. return ReflectionUtils.hashCode(this);
  246. }
  247. /**
  248. * @see Object#equals(Object)
  249. */
  250. @Override
  251. public boolean equals(Object that) {
  252. return ReflectionUtils.equals(this, that);
  253. }
  254. /**
  255. * @see Object#toString()
  256. */
  257. @Override
  258. public String toString() {
  259. return ReflectionUtils.toString(this);
  260. }
  261. /**
  262. * The access token's value.
  263. *
  264. * @return The access token's value.
  265. */
  266. public String getAccessToken() {
  267. return accessToken;
  268. }
  269. /**
  270. * The date on which the access token expires.
  271. *
  272. * @return The date on which the access token expires.
  273. */
  274. public Date getExpires() {
  275. return expires == null ? null : new Date(1000L * expires);
  276. }
  277. }
  278. }