FBSDKGraphRequestConnection.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #import <Foundation/Foundation.h>
  19. @class FBSDKGraphRequest;
  20. @class FBSDKGraphRequestConnection;
  21. /**
  22. FBSDKGraphRequestHandler
  23. A block that is passed to addRequest to register for a callback with the results of that
  24. request once the connection completes.
  25. Pass a block of this type when calling addRequest. This will be called once
  26. the request completes. The call occurs on the UI thread.
  27. @param connection The `FBSDKGraphRequestConnection` that sent the request.
  28. @param result The result of the request. This is a translation of
  29. JSON data to `NSDictionary` and `NSArray` objects. This
  30. is nil if there was an error.
  31. @param error The `NSError` representing any error that occurred.
  32. */
  33. typedef void (^FBSDKGraphRequestHandler)(FBSDKGraphRequestConnection *connection,
  34. id result,
  35. NSError *error);
  36. /**
  37. @protocol
  38. The `FBSDKGraphRequestConnectionDelegate` protocol defines the methods used to receive network
  39. activity progress information from a <FBSDKGraphRequestConnection>.
  40. */
  41. @protocol FBSDKGraphRequestConnectionDelegate <NSObject>
  42. @optional
  43. /**
  44. @method
  45. Tells the delegate the request connection will begin loading
  46. If the <FBSDKGraphRequestConnection> is created using one of the convenience factory methods prefixed with
  47. start, the object returned from the convenience method has already begun loading and this method
  48. will not be called when the delegate is set.
  49. @param connection The request connection that is starting a network request
  50. */
  51. - (void)requestConnectionWillBeginLoading:(FBSDKGraphRequestConnection *)connection;
  52. /**
  53. @method
  54. Tells the delegate the request connection finished loading
  55. If the request connection completes without a network error occurring then this method is called.
  56. Invocation of this method does not indicate success of every <FBSDKGraphRequest> made, only that the
  57. request connection has no further activity. Use the error argument passed to the FBSDKGraphRequestHandler
  58. block to determine success or failure of each <FBSDKGraphRequest>.
  59. This method is invoked after the completion handler for each <FBSDKGraphRequest>.
  60. @param connection The request connection that successfully completed a network request
  61. */
  62. - (void)requestConnectionDidFinishLoading:(FBSDKGraphRequestConnection *)connection;
  63. /**
  64. @method
  65. Tells the delegate the request connection failed with an error
  66. If the request connection fails with a network error then this method is called. The `error`
  67. argument specifies why the network connection failed. The `NSError` object passed to the
  68. FBSDKGraphRequestHandler block may contain additional information.
  69. @param connection The request connection that successfully completed a network request
  70. @param error The `NSError` representing the network error that occurred, if any. May be nil
  71. in some circumstances. Consult the `NSError` for the <FBSDKGraphRequest> for reliable
  72. failure information.
  73. */
  74. - (void)requestConnection:(FBSDKGraphRequestConnection *)connection
  75. didFailWithError:(NSError *)error;
  76. /**
  77. @method
  78. Tells the delegate how much data has been sent and is planned to send to the remote host
  79. The byte count arguments refer to the aggregated <FBSDKGraphRequest> objects, not a particular <FBSDKGraphRequest>.
  80. Like `NSURLSession`, the values may change in unexpected ways if data needs to be resent.
  81. @param connection The request connection transmitting data to a remote host
  82. @param bytesWritten The number of bytes sent in the last transmission
  83. @param totalBytesWritten The total number of bytes sent to the remote host
  84. @param totalBytesExpectedToWrite The total number of bytes expected to send to the remote host
  85. */
  86. - (void)requestConnection:(FBSDKGraphRequestConnection *)connection
  87. didSendBodyData:(NSInteger)bytesWritten
  88. totalBytesWritten:(NSInteger)totalBytesWritten
  89. totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;
  90. @end
  91. /**
  92. The `FBSDKGraphRequestConnection` represents a single connection to Facebook to service a request.
  93. The request settings are encapsulated in a reusable <FBSDKGraphRequest> object. The
  94. `FBSDKGraphRequestConnection` object encapsulates the concerns of a single communication
  95. e.g. starting a connection, canceling a connection, or batching requests.
  96. */
  97. @interface FBSDKGraphRequestConnection : NSObject
  98. /**
  99. The delegate object that receives updates.
  100. */
  101. @property (nonatomic, weak) id<FBSDKGraphRequestConnectionDelegate> delegate;
  102. /**
  103. Gets or sets the timeout interval to wait for a response before giving up.
  104. */
  105. @property (nonatomic, assign) NSTimeInterval timeout;
  106. /**
  107. The raw response that was returned from the server. (readonly)
  108. This property can be used to inspect HTTP headers that were returned from
  109. the server.
  110. The property is nil until the request completes. If there was a response
  111. then this property will be non-nil during the FBSDKGraphRequestHandler callback.
  112. */
  113. @property (nonatomic, retain, readonly) NSHTTPURLResponse *URLResponse;
  114. /**
  115. @methodgroup Class methods
  116. */
  117. /**
  118. @method
  119. This method sets the default timeout on all FBSDKGraphRequestConnection instances. Defaults to 60 seconds.
  120. @param defaultConnectionTimeout The timeout interval.
  121. */
  122. + (void)setDefaultConnectionTimeout:(NSTimeInterval)defaultConnectionTimeout;
  123. /**
  124. @methodgroup Adding requests
  125. */
  126. /**
  127. @method
  128. This method adds an <FBSDKGraphRequest> object to this connection.
  129. @param request A request to be included in the round-trip when start is called.
  130. @param handler A handler to call back when the round-trip completes or times out.
  131. The completion handler is retained until the block is called upon the
  132. completion or cancellation of the connection.
  133. */
  134. - (void)addRequest:(FBSDKGraphRequest *)request
  135. completionHandler:(FBSDKGraphRequestHandler)handler;
  136. /**
  137. @method
  138. This method adds an <FBSDKGraphRequest> object to this connection.
  139. @param request A request to be included in the round-trip when start is called.
  140. @param handler A handler to call back when the round-trip completes or times out.
  141. The handler will be invoked on the main thread.
  142. @param name An optional name for this request. This can be used to feed
  143. the results of one request to the input of another <FBSDKGraphRequest> in the same
  144. `FBSDKGraphRequestConnection` as described in
  145. [Graph API Batch Requests]( https://developers.facebook.com/docs/reference/api/batch/ ).
  146. The completion handler is retained until the block is called upon the
  147. completion or cancellation of the connection. This request can be named
  148. to allow for using the request's response in a subsequent request.
  149. */
  150. - (void)addRequest:(FBSDKGraphRequest *)request
  151. batchEntryName:(NSString *)name
  152. completionHandler:(FBSDKGraphRequestHandler)handler;
  153. - (void)addRequest:(FBSDKGraphRequest *)request
  154. completionHandler:(FBSDKGraphRequestHandler)handler
  155. batchEntryName:(NSString *)name
  156. DEPRECATED_MSG_ATTRIBUTE("Renamed `addRequest:batchEntryName:completionHandler:`");
  157. /**
  158. @method
  159. This method adds an <FBSDKGraphRequest> object to this connection.
  160. @param request A request to be included in the round-trip when start is called.
  161. @param handler A handler to call back when the round-trip completes or times out.
  162. @param batchParameters The optional dictionary of parameters to include for this request
  163. as described in [Graph API Batch Requests]( https://developers.facebook.com/docs/reference/api/batch/ ).
  164. Examples include "depends_on", "name", or "omit_response_on_success".
  165. The completion handler is retained until the block is called upon the
  166. completion or cancellation of the connection. This request can be named
  167. to allow for using the request's response in a subsequent request.
  168. */
  169. - (void)addRequest:(FBSDKGraphRequest *)request
  170. batchParameters:(NSDictionary<NSString *, id> *)batchParameters
  171. completionHandler:(FBSDKGraphRequestHandler)handler;
  172. - (void)addRequest:(FBSDKGraphRequest *)request
  173. completionHandler:(FBSDKGraphRequestHandler)handler
  174. batchParameters:(NSDictionary *)batchParameters
  175. DEPRECATED_MSG_ATTRIBUTE("Renamed `addRequest:batchParameters:completionHandler:`");
  176. /**
  177. @methodgroup Instance methods
  178. */
  179. /**
  180. @method
  181. Signals that a connection should be logically terminated as the
  182. application is no longer interested in a response.
  183. Synchronously calls any handlers indicating the request was cancelled. Cancel
  184. does not guarantee that the request-related processing will cease. It
  185. does promise that all handlers will complete before the cancel returns. A call to
  186. cancel prior to a start implies a cancellation of all requests associated
  187. with the connection.
  188. */
  189. - (void)cancel;
  190. /**
  191. @method
  192. This method starts a connection with the server and is capable of handling all of the
  193. requests that were added to the connection.
  194. By default, a connection is scheduled on the current thread in the default mode when it is created.
  195. See `setDelegateQueue:` for other options.
  196. This method cannot be called twice for an `FBSDKGraphRequestConnection` instance.
  197. */
  198. - (void)start;
  199. /**
  200. Determines the operation queue that is used to call methods on the connection's delegate.
  201. @param queue The operation queue to use when calling delegate methods.
  202. By default, a connection is scheduled on the current thread in the default mode when it is created.
  203. You cannot reschedule a connection after it has started.
  204. */
  205. - (void)setDelegateQueue:(NSOperationQueue *)queue;
  206. /**
  207. @method
  208. Overrides the default version for a batch request
  209. The SDK automatically prepends a version part, such as "v2.0" to API paths in order to simplify API versioning
  210. for applications. If you want to override the version part while using batch requests on the connection, call
  211. this method to set the version for the batch request.
  212. @param version This is a string in the form @"v2.0" which will be used for the version part of an API path
  213. */
  214. - (void)overrideGraphAPIVersion:(NSString *)version;
  215. - (void)overrideVersionPartWith:(NSString *)version
  216. DEPRECATED_MSG_ATTRIBUTE("Renamed `overrideGraphAPIVersion`");
  217. @end
  218. /**
  219. The key in the result dictionary for requests to old versions of the Graph API
  220. whose response is not a JSON object.
  221. When a request returns a non-JSON response (such as a "true" literal), that response
  222. will be wrapped into a dictionary using this const as the key. This only applies for very few Graph API
  223. prior to v2.1.
  224. */
  225. FOUNDATION_EXPORT NSString *const FBSDKNonJSONResponseProperty;