MKNetworkOperation.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. //
  2. // MKNetwork.h
  3. // MKNetworkKit
  4. //
  5. // Created by Mugunth Kumar (@mugunthkumar) on 11/11/11.
  6. // Copyright (C) 2011-2020 by Steinlogic Consulting and Training Pte Ltd
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. @class MKNetworkOperation;
  25. typedef enum {
  26. MKNetworkOperationStateReady = 1,
  27. MKNetworkOperationStateExecuting = 2,
  28. MKNetworkOperationStateFinished = 3
  29. } MKNetworkOperationState;
  30. typedef void (^MKNKVoidBlock)(void);
  31. typedef void (^MKNKIDBlock)(void);
  32. typedef void (^MKNKProgressBlock)(double progress);
  33. typedef void (^MKNKResponseBlock)(MKNetworkOperation* completedOperation);
  34. #if TARGET_OS_IPHONE
  35. typedef void (^MKNKImageBlock) (UIImage* fetchedImage, NSURL* url, BOOL isInCache);
  36. #elif TARGET_OS_MAC
  37. typedef void (^MKNKImageBlock) (NSImage* fetchedImage, NSURL* url, BOOL isInCache);
  38. #endif
  39. typedef void (^MKNKResponseErrorBlock)(MKNetworkOperation* completedOperation, NSError* error);
  40. typedef void (^MKNKErrorBlock)(NSError* error);
  41. typedef void (^MKNKAuthBlock)(NSURLAuthenticationChallenge* challenge);
  42. typedef NSString* (^MKNKEncodingBlock) (NSDictionary* postDataDict);
  43. typedef enum {
  44. MKNKPostDataEncodingTypeURL = 0, // default
  45. MKNKPostDataEncodingTypeJSON,
  46. MKNKPostDataEncodingTypePlist,
  47. MKNKPostDataEncodingTypeCustom
  48. } MKNKPostDataEncodingType;
  49. /*!
  50. @header MKNetworkOperation.h
  51. @abstract Represents a single unique network operation.
  52. */
  53. /*!
  54. * @class MKNetworkOperation
  55. * @abstract Represents a single unique network operation.
  56. *
  57. * @discussion
  58. * You normally create an instance of this class using the methods exposed by MKNetworkEngine
  59. * Created operations are enqueued into the shared queue on MKNetworkEngine
  60. * MKNetworkOperation encapsulates both request and response
  61. * Printing a MKNetworkOperation prints out a cURL command that can be copied and pasted directly on terminal
  62. * Freezable operations are serialized when network connectivity is lost and performed when connection is restored
  63. */
  64. @interface MKNetworkOperation : NSOperation {
  65. @private
  66. int _state;
  67. BOOL _freezable;
  68. MKNKPostDataEncodingType _postDataEncoding;
  69. }
  70. @property (nonatomic,assign) NSTimeInterval imageCacheDuration,imageHeadCacheDuration;
  71. /*!
  72. * @abstract Request URL Property
  73. * @property url
  74. *
  75. * @discussion
  76. * Returns the operation's URL
  77. * This property is readonly cannot be updated.
  78. * To create an operation with a specific URL, use the operationWithURLString:params:httpMethod:
  79. */
  80. @property (nonatomic, copy, readonly) NSString *url;
  81. /*!
  82. * @abstract The internal request object
  83. * @property readonlyRequest
  84. *
  85. * @discussion
  86. * Returns the operation's actual request object
  87. * This property is readonly cannot be modified.
  88. * To create an operation with a new request, use the operationWithURLString:params:httpMethod:
  89. */
  90. @property (nonatomic, strong, readonly) NSURLRequest *readonlyRequest;
  91. /*!
  92. * @abstract The internal HTTP Response Object
  93. * @property readonlyResponse
  94. *
  95. * @discussion
  96. * Returns the operation's actual response object
  97. * This property is readonly cannot be updated.
  98. */
  99. @property (nonatomic, strong, readonly) NSHTTPURLResponse *readonlyResponse;
  100. /*!
  101. * @abstract The internal HTTP Post data values
  102. * @property readonlyPostDictionary
  103. *
  104. * @discussion
  105. * Returns the operation's post data dictionary
  106. * This property is readonly cannot be updated.
  107. * Rather, updating this post dictionary doesn't have any effect on the MKNetworkOperation.
  108. * Use the addHeaders method to add post data parameters to the operation.
  109. *
  110. * @seealso
  111. * addHeaders:
  112. */
  113. @property (nonatomic, copy, readonly) NSDictionary *readonlyPostDictionary;
  114. /*!
  115. * @abstract The internal request object's method type
  116. * @property HTTPMethod
  117. *
  118. * @discussion
  119. * Returns the operation's method type
  120. * This property is readonly cannot be modified.
  121. * To create an operation with a new method type, use the operationWithURLString:params:httpMethod:
  122. */
  123. @property (nonatomic, copy, readonly) NSString *HTTPMethod;
  124. /*!
  125. * @abstract The internal response object's status code
  126. * @property HTTPStatusCode
  127. *
  128. * @discussion
  129. * Returns the operation's response's status code.
  130. * Returns 0 when the operation has not yet started and the response is not available.
  131. * This property is readonly cannot be modified.
  132. */
  133. @property (nonatomic, assign, readonly) NSInteger HTTPStatusCode;
  134. /*!
  135. * @abstract Post Data Encoding Type Property
  136. * @property postDataEncoding
  137. *
  138. * @discussion
  139. * Specifies which type of encoding should be used to encode post data.
  140. * MKNKPostDataEncodingTypeURL is the default which defaults to application/x-www-form-urlencoded
  141. * MKNKPostDataEncodingTypeJSON uses JSON encoding.
  142. * JSON Encoding is supported only in iOS 5 or Mac OS X 10.7 and above.
  143. * On older operating systems, JSON Encoding reverts back to URL Encoding
  144. * You can use the postDataEncodingHandler to provide a custom postDataEncoding
  145. * For example, JSON encoding using a third party library.
  146. *
  147. * @seealso
  148. * setCustomPostDataEncodingHandler:forType:
  149. *
  150. */
  151. @property (nonatomic, assign) MKNKPostDataEncodingType postDataEncoding;
  152. /*!
  153. * @abstract Set a customized Post Data Encoding Handler for a given HTTP Content Type
  154. *
  155. * @discussion
  156. * If you need customized post data encoding support, provide a block method here.
  157. * This block method will be invoked only when your HTTP Method is POST or PUT
  158. * For default URL encoding or JSON encoding, use the property postDataEncoding
  159. * If you change the postData format, it's your responsiblity to provide a correct Content-Type.
  160. *
  161. * @seealso
  162. * postDataEncoding
  163. */
  164. -(void) setCustomPostDataEncodingHandler:(MKNKEncodingBlock) postDataEncodingHandler forType:(NSString*) contentType;
  165. /*!
  166. * @abstract String Encoding Property
  167. * @property stringEncoding
  168. *
  169. * @discussion
  170. * Specifies which type of encoding should be used to encode URL strings
  171. */
  172. @property (nonatomic, assign) NSStringEncoding stringEncoding;
  173. /*!
  174. * @abstract Freezable request
  175. * @property freezable
  176. *
  177. * @discussion
  178. * Freezable operations are serialized when the network goes down and restored when the connectivity is up again.
  179. * Only POST, PUT and DELETE operations are freezable.
  180. * In short, any operation that changes the state of the server are freezable, creating a tweet, checking into a new location etc., Operations like fetching a list of tweets (think readonly GET operations) are not freezable.
  181. * MKNetworkKit doesn't freeze (readonly) GET operations even if they are marked as freezable
  182. */
  183. @property (nonatomic, assign) BOOL freezable;
  184. /*!
  185. * @abstract Error object
  186. * @property error
  187. *
  188. * @discussion
  189. * If the network operation results in an error, this will hold the response error, otherwise it will be nil
  190. */
  191. @property (nonatomic, readonly, strong) NSError *error;
  192. /*!
  193. * @abstract Boolean variable that states whether the operation should continue if the certificate is invalid.
  194. * @property shouldContinueWithInvalidCertificate
  195. *
  196. * @discussion
  197. * If you set this property to YES, the operation will continue as if the certificate was valid (if you use Server Trust Auth)
  198. * The default value is NO. MKNetworkKit will not run an operation with a server that is not trusted.
  199. */
  200. @property (nonatomic, assign) BOOL shouldContinueWithInvalidCertificate;
  201. /*!
  202. * @abstract Cache headers of the response
  203. * @property cacheHeaders
  204. *
  205. * @discussion
  206. * If the network operation is a GET, this dictionary will be populated with relevant cache related headers
  207. * MKNetworkKit assumes a 7 day cache for images and 1 minute cache for all requests with no-cache set
  208. * This property is internal to MKNetworkKit. Modifying this is not recommended and will result in unexpected behaviour
  209. */
  210. @property (strong, nonatomic) NSMutableDictionary *cacheHeaders;
  211. /*!
  212. * @abstract Authentication methods
  213. *
  214. * @discussion
  215. * If your request needs to be authenticated, set your username and password using this method.
  216. */
  217. -(void) setUsername:(NSString*) name password:(NSString*) password;
  218. /*!
  219. * @abstract Authentication methods
  220. *
  221. * @discussion
  222. * If your request needs to be authenticated using HTTP Basic, use this method to set your username and password.
  223. * Calling this method with basicAuth:NO is same as calling setUserName:password:
  224. * @seealso
  225. * setUserName:password:
  226. */
  227. -(void) setUsername:(NSString*) username password:(NSString*) password basicAuth:(BOOL) bYesOrNo;
  228. /*!
  229. * @abstract Authentication methods (Client Certificate)
  230. * @property clientCertificate
  231. *
  232. * @discussion
  233. * If your request needs to be authenticated using a client certificate, set the certificate path here
  234. */
  235. @property (copy, nonatomic) NSString *clientCertificate;
  236. /*!
  237. * @abstract Authentication methods (Password for the Client Certificate)
  238. * @property clientCertificatePassword
  239. *
  240. * @discussion
  241. * If your client certificate is encrypted with a password, specify it here
  242. */
  243. @property (copy, nonatomic) NSString *clientCertificatePassword;
  244. /*!
  245. * @abstract Custom authentication handler
  246. * @property authHandler
  247. *
  248. * @discussion
  249. * If your request needs to be authenticated using a custom method (like a Web page/HTML Form), add a block method here
  250. * and process the NSURLAuthenticationChallenge
  251. */
  252. @property (nonatomic, copy) MKNKAuthBlock authHandler;
  253. /*!
  254. * @abstract Handler that you implement to monitor reachability changes
  255. * @property operationStateChangedHandler
  256. *
  257. * @discussion
  258. * The framework calls this handler whenever the operation state changes
  259. */
  260. @property (copy, nonatomic) void (^operationStateChangedHandler)(MKNetworkOperationState newState);
  261. /*!
  262. * @abstract controls persistence of authentication credentials
  263. * @property credentialPersistence
  264. *
  265. * @discussion
  266. * The default value is set to NSURLCredentialPersistenceForSession, change it to NSURLCredentialPersistenceNone to avoid caching issues (isse #35)
  267. */
  268. @property (nonatomic, assign) NSURLCredentialPersistence credentialPersistence;
  269. #if TARGET_OS_IPHONE
  270. /*!
  271. * @abstract notification that has to be shown when an error occurs and the app is in background
  272. * @property localNotification
  273. *
  274. * @discussion
  275. * The default value nil. No notification is shown when an error occurs.
  276. * To show a notification when the app is in background and the network operation running in background fails,
  277. * set this parameter to a UILocalNotification object
  278. */
  279. @property (nonatomic, strong) UILocalNotification *localNotification;
  280. /*!
  281. * @abstract Shows a local notification when an error occurs
  282. * @property shouldShowLocalNotificationOnError
  283. *
  284. * @discussion
  285. * The default value NO. No notification is shown when an error occurs.
  286. * When set to YES, MKNetworkKit shows the NSError localizedDescription text as a notification when the app is in background and the network operation ended in error.
  287. * To customize the local notification text, use the property localNotification
  288. * @seealso
  289. * localNotification
  290. */
  291. @property (nonatomic, assign) BOOL shouldShowLocalNotificationOnError;
  292. #endif
  293. /*!
  294. * @abstract Add additional header parameters
  295. *
  296. * @discussion
  297. * If you ever need to set additional headers after creating your operation, you this method.
  298. * You normally set default headers to the engine and they get added to every request you create.
  299. * On specific cases where you need to set a new header parameter for just a single API call, you can use this
  300. */
  301. -(void) addHeaders:(NSDictionary*) headersDictionary;
  302. /*!
  303. * @abstract Sets the authorization header after prefixing it with a given auth type
  304. *
  305. * @discussion
  306. * If you need to set the HTTP Authorization header, you can use this convinience method.
  307. * This method internally calls addHeaders:
  308. * The authType parameter is a string that you can prefix to your auth token to tell your server what kind of authentication scheme you want to use. HTTP Basic Authentication uses the string "Basic" for authType
  309. * To use HTTP Basic Authentication, consider using the method setUsername:password:basicAuth: instead.
  310. *
  311. * Example
  312. * [op setToken:@"abracadabra" forAuthType:@"Token"] will set the header value to
  313. * "Authorization: Token abracadabra"
  314. *
  315. * @seealso
  316. * setUsername:password:basicAuth:
  317. * addHeaders:
  318. */
  319. -(void) setAuthorizationHeaderValue:(NSString*) token forAuthType:(NSString*) authType;
  320. /*!
  321. * @abstract Attaches a file to the request
  322. *
  323. * @discussion
  324. * This method lets you attach a file to the request
  325. * The method has a side effect. It changes the HTTPMethod to "POST" regardless of what it was before.
  326. * It also changes the post format to multipart/form-data
  327. * The mime-type is assumed to be application/octet-stream
  328. */
  329. -(void) addFile:(NSString*) filePath forKey:(NSString*) key;
  330. /*!
  331. * @abstract Attaches a file to the request and allows you to specify a mime-type
  332. *
  333. * @discussion
  334. * This method lets you attach a file to the request
  335. * The method has a side effect. It changes the HTTPMethod to "POST" regardless of what it was before.
  336. * It also changes the post format to multipart/form-data
  337. */
  338. -(void) addFile:(NSString*) filePath forKey:(NSString*) key mimeType:(NSString*) mimeType;
  339. /*!
  340. * @abstract Attaches a resource to the request from a NSData pointer
  341. *
  342. * @discussion
  343. * This method lets you attach a NSData object to the request. The behaviour is exactly similar to addFile:forKey:
  344. * The method has a side effect. It changes the HTTPMethod to "POST" regardless of what it was before.
  345. * It also changes the post format to multipart/form-data
  346. * The mime-type is assumed to be application/octet-stream
  347. */
  348. -(void) addData:(NSData*) data forKey:(NSString*) key;
  349. /*!
  350. * @abstract Attaches a resource to the request from a NSData pointer and allows you to specify a mime-type
  351. *
  352. * @discussion
  353. * This method lets you attach a NSData object to the request. The behaviour is exactly similar to addFile:forKey:mimeType:
  354. * The method has a side effect. It changes the HTTPMethod to "POST" regardless of what it was before.
  355. * It also changes the post format to multipart/form-data
  356. */
  357. -(void) addData:(NSData*) data forKey:(NSString*) key mimeType:(NSString*) mimeType fileName:(NSString*) fileName;
  358. /*!
  359. * @abstract Block Handler for completion and error
  360. *
  361. * @discussion
  362. * This method sets your completion and error blocks. If your operation's response data was previously called,
  363. * the completion block will be called almost immediately with the cached response. You can check if the completion
  364. * handler was invoked with a cached data or with real data by calling the isCachedResponse method.
  365. * This method is deprecated in favour of addCompletionHandler:errorHandler: that returns the completedOperation in the error block as well.
  366. * While I will still continue to support this method, I'll remove it completely in a future release.
  367. *
  368. * @seealso
  369. * isCachedResponse
  370. * addCompletionHandler:errorHandler:
  371. */
  372. -(void) onCompletion:(MKNKResponseBlock) response onError:(MKNKErrorBlock) error DEPRECATED_ATTRIBUTE;
  373. /*!
  374. * @abstract adds a block Handler for completion and error
  375. *
  376. * @discussion
  377. * This method sets your completion and error blocks. If your operation's response data was previously called,
  378. * the completion block will be called almost immediately with the cached response. You can check if the completion
  379. * handler was invoked with a cached data or with real data by calling the isCachedResponse method.
  380. *
  381. * @seealso
  382. * onCompletion:onError:
  383. */
  384. -(void) addCompletionHandler:(MKNKResponseBlock) response errorHandler:(MKNKResponseErrorBlock) error;
  385. /*!
  386. * @abstract Block Handler for tracking 304 not modified state
  387. *
  388. * @discussion
  389. * This method will be called if the server sends a 304 HTTP status for your request.
  390. *
  391. */
  392. -(void) onNotModified:(MKNKVoidBlock) notModifiedBlock;
  393. /*!
  394. * @abstract Block Handler for tracking upload progress
  395. *
  396. * @discussion
  397. * This method can be used to update your progress bars when an upload is in progress.
  398. * The value range of the progress is 0 to 1.
  399. *
  400. */
  401. -(void) onUploadProgressChanged:(MKNKProgressBlock) uploadProgressBlock;
  402. /*!
  403. * @abstract Block Handler for tracking download progress
  404. *
  405. * @discussion
  406. * This method can be used to update your progress bars when a download is in progress.
  407. * The value range of the progress is 0 to 1.
  408. *
  409. */
  410. -(void) onDownloadProgressChanged:(MKNKProgressBlock) downloadProgressBlock;
  411. /*!
  412. * @abstract Uploads a resource from a stream
  413. *
  414. * @discussion
  415. * This method can be used to upload a resource for a post body directly from a stream.
  416. *
  417. */
  418. -(void) setUploadStream:(NSInputStream*) inputStream;
  419. /*!
  420. * @abstract Downloads a resource directly to a file or any output stream
  421. *
  422. * @discussion
  423. * This method can be used to download a resource directly to a stream (It's normally a file in most cases).
  424. * Calling this method multiple times adds new streams to the same operation.
  425. * A stream cannot be removed after it is added.
  426. *
  427. */
  428. -(void) addDownloadStream:(NSOutputStream*) outputStream;
  429. /*!
  430. * @abstract Helper method to check if the response is from cache
  431. *
  432. * @discussion
  433. * This method should be used to check if your response is cached.
  434. * When you enable caching on MKNetworkEngine, your completionHandler will be called with cached data first and then
  435. * with real data, later after fetching. In your handler, you can call this method to check if it is from cache or not
  436. *
  437. */
  438. -(BOOL) isCachedResponse;
  439. /*!
  440. * @abstract Helper method to retrieve the contents
  441. *
  442. * @discussion
  443. * This method is used for accessing the downloaded data. If the operation is still in progress, the method returns nil instead of partial data. To access partial data, use a downloadStream.
  444. *
  445. * @seealso
  446. * addDownloadStream:
  447. */
  448. -(NSData*) responseData;
  449. /*!
  450. * @abstract Helper method to retrieve the contents as a NSString
  451. *
  452. * @discussion
  453. * This method is used for accessing the downloaded data. If the operation is still in progress, the method returns nil instead of partial data. To access partial data, use a downloadStream. The method also converts the responseData to a NSString using the stringEncoding specified in the operation
  454. *
  455. * @seealso
  456. * addDownloadStream:
  457. * stringEncoding
  458. */
  459. -(NSString*)responseString;
  460. /*!
  461. * @abstract Helper method to print the request as a cURL command
  462. *
  463. * @discussion
  464. * This method is used for displaying the request you created as a cURL command
  465. *
  466. */
  467. -(NSString*) curlCommandLineString;
  468. /*!
  469. * @abstract Helper method to retrieve the contents as a NSString encoded using a specific string encoding
  470. *
  471. * @discussion
  472. * This method is used for accessing the downloaded data. If the operation is still in progress, the method returns nil instead of partial data. To access partial data, use a downloadStream. The method also converts the responseData to a NSString using the stringEncoding specified in the parameter
  473. *
  474. * @seealso
  475. * addDownloadStream:
  476. * stringEncoding
  477. */
  478. -(NSString*) responseStringWithEncoding:(NSStringEncoding) encoding;
  479. /*!
  480. * @abstract Helper method to retrieve the contents as a UIImage
  481. *
  482. * @discussion
  483. * This method is used for accessing the downloaded data as a UIImage. If the operation is still in progress, the method returns nil instead of a partial image. To access partial data, use a downloadStream. If the response is not a valid image, this method returns nil. This method doesn't obey the response mime type property. If the server response with a proper image data but set the mime type incorrectly, this method will still be able access the response as an image.
  484. *
  485. * @seealso
  486. * addDownloadStream:
  487. */
  488. #if TARGET_OS_IPHONE
  489. -(UIImage*) responseImage;
  490. -(void) decompressedResponseImageOfSize:(CGSize) size completionHandler:(void (^)(UIImage *decompressedImage)) imageDecompressionHandler;
  491. #elif TARGET_OS_MAC
  492. -(NSImage*) responseImage;
  493. -(NSXMLDocument*) responseXML;
  494. #endif
  495. /*!
  496. * @abstract Helper method to retrieve the contents as a NSDictionary or NSArray depending on the JSON contents
  497. *
  498. * @discussion
  499. * This method is used for accessing the downloaded data as a NSDictionary or an NSArray. If the operation is still in progress, the method returns nil. If the response is not a valid JSON, this method returns nil.
  500. *
  501. * @seealso
  502. * responseJSONWithCompletionHandler:
  503. * @availability
  504. * iOS 5 and above or Mac OS 10.7 and above
  505. */
  506. -(id) responseJSON;
  507. /*!
  508. * @abstract Helper method to retrieve the contents as a NSDictionary or NSArray depending on the JSON contents in the background
  509. *
  510. * @discussion
  511. * This method is used for accessing the downloaded data as a NSDictionary or an NSArray. If the operation is still in progress, the method returns nil. If the response is not a valid JSON, this method returns nil. The difference between this and responseJSON is that, this method decodes JSON in the background.
  512. *
  513. * @availability
  514. * iOS 5 and above or Mac OS 10.7 and above
  515. */
  516. -(void) responseJSONWithCompletionHandler:(void (^)(id jsonObject)) jsonDecompressionHandler;
  517. /*!
  518. * @abstract Overridable custom method where you can add your custom business logic error handling
  519. *
  520. * @discussion
  521. * This optional method can be overridden to do custom error handling. Be sure to call [super operationSucceeded] at the last.
  522. * For example, a valid HTTP response (200) like "Item not found in database" might have a custom business error code
  523. * You can override this method and called [super failWithError:customError]; to notify that HTTP call was successful but the method
  524. * ended as a failed call
  525. *
  526. */
  527. -(void) operationSucceeded;
  528. /*!
  529. * @abstract Overridable custom method where you can add your custom business logic error handling
  530. *
  531. * @discussion
  532. * This optional method can be overridden to do custom error handling. Be sure to call [super operationSucceeded] at the last.
  533. * For example, a invalid HTTP response (401) like "Unauthorized" might be a valid case in your app.
  534. * You can override this method and called [super operationSucceeded]; to notify that HTTP call failed but the method
  535. * ended as a success call. For example, Facebook login failed, but to your business implementation, it's not a problem as you
  536. * are going to try alternative login mechanisms.
  537. *
  538. */
  539. -(void) operationFailedWithError:(NSError*) error;
  540. // internal methods called by MKNetworkEngine only.
  541. // Don't touch
  542. -(BOOL) isCacheable;
  543. -(void) setCachedData:(NSData*) cachedData;
  544. -(void) setCacheHandler:(MKNKResponseBlock) cacheHandler;
  545. -(void) updateHandlersFromOperation:(MKNetworkOperation*) operation;
  546. -(void) updateOperationBasedOnPreviousHeaders:(NSMutableDictionary*) headers;
  547. -(NSString*) uniqueIdentifier;
  548. - (id)initWithURLString:(NSString *)aURLString
  549. params:(NSDictionary *)params
  550. httpMethod:(NSString *)method;
  551. @end