MKNetworkEngine.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //
  2. // MKNetworkEngine.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. #import "MKNetworkKit.h"
  25. /*!
  26. @header MKNetworkEngine.h
  27. @abstract Represents a subclassable Network Engine for your app
  28. */
  29. /*!
  30. * @class MKNetworkEngine
  31. * @abstract Represents a subclassable Network Engine for your app
  32. *
  33. * @discussion
  34. * This class is the heart of MKNetworkEngine
  35. * You create network operations and enqueue them here
  36. * MKNetworkEngine encapsulates a Reachability object that relieves you of managing network connectivity losses
  37. * MKNetworkEngine also allows you to provide custom header fields that gets appended automatically to every request
  38. */
  39. @interface MKNetworkEngine : NSObject
  40. /*!
  41. * @abstract Initializes your network engine with a hostname
  42. *
  43. * @discussion
  44. * Creates an engine for a given host name
  45. * The hostname parameter is optional
  46. * The hostname, if not null, initializes a Reachability notifier.
  47. * Network reachability notifications are automatically taken care of by MKNetworkEngine
  48. *
  49. */
  50. - (id) initWithHostName:(NSString*) hostName;
  51. /*!
  52. * @abstract Initializes your network engine with a hostname and custom header fields
  53. *
  54. * @discussion
  55. * Creates an engine for a given host name
  56. * The default headers you specify here will be appened to every operation created in this engine
  57. * The hostname, if not null, initializes a Reachability notifier.
  58. * Network reachability notifications are automatically taken care of by MKNetworkEngine
  59. * Both parameters are optional
  60. *
  61. */
  62. - (id) initWithHostName:(NSString*) hostName customHeaderFields:(NSDictionary*) headers;
  63. /*!
  64. * @abstract Initializes your network engine with a hostname
  65. *
  66. * @discussion
  67. * Creates an engine for a given host name
  68. * The hostname parameter is optional
  69. * The apiPath paramter is optional
  70. * The apiPath is prefixed to every call to operationWithPath: You can use this method if your server's API location is not at the root (/)
  71. * The hostname, if not null, initializes a Reachability notifier.
  72. * Network reachability notifications are automatically taken care of by MKNetworkEngine
  73. *
  74. */
  75. - (id) initWithHostName:(NSString*) hostName apiPath:(NSString*) apiPath customHeaderFields:(NSDictionary*) headers;
  76. /*!
  77. * @abstract Creates a simple GET Operation with a request URL
  78. *
  79. * @discussion
  80. * Creates an operation with the given URL path.
  81. * The default headers you specified in your MKNetworkEngine subclass gets added to the headers
  82. * The HTTP Method is implicitly assumed to be GET
  83. *
  84. */
  85. -(MKNetworkOperation*) operationWithPath:(NSString*) path;
  86. /*!
  87. * @abstract Creates a simple GET Operation with a request URL and parameters
  88. *
  89. * @discussion
  90. * Creates an operation with the given URL path.
  91. * The default headers you specified in your MKNetworkEngine subclass gets added to the headers
  92. * The body dictionary in this method gets attached to the URL as query parameters
  93. * The HTTP Method is implicitly assumed to be GET
  94. *
  95. */
  96. -(MKNetworkOperation*) operationWithPath:(NSString*) path
  97. params:(NSDictionary*) body;
  98. /*!
  99. * @abstract Creates a simple GET Operation with a request URL, parameters and HTTP Method
  100. *
  101. * @discussion
  102. * Creates an operation with the given URL path.
  103. * The default headers you specified in your MKNetworkEngine subclass gets added to the headers
  104. * The params dictionary in this method gets attached to the URL as query parameters if the HTTP Method is GET/DELETE
  105. * The params dictionary is attached to the body if the HTTP Method is POST/PUT
  106. * The HTTP Method is implicitly assumed to be GET
  107. */
  108. -(MKNetworkOperation*) operationWithPath:(NSString*) path
  109. params:(NSDictionary*) body
  110. httpMethod:(NSString*)method;
  111. /*!
  112. * @abstract Creates a simple GET Operation with a request URL, parameters, HTTP Method and the SSL switch
  113. *
  114. * @discussion
  115. * Creates an operation with the given URL path.
  116. * The ssl option when true changes the URL to https.
  117. * The ssl option when false changes the URL to http.
  118. * The default headers you specified in your MKNetworkEngine subclass gets added to the headers
  119. * The params dictionary in this method gets attached to the URL as query parameters if the HTTP Method is GET/DELETE
  120. * The params dictionary is attached to the body if the HTTP Method is POST/PUT
  121. * The previously mentioned methods operationWithPath: and operationWithPath:params: call this internally
  122. */
  123. -(MKNetworkOperation*) operationWithPath:(NSString*) path
  124. params:(NSDictionary*) body
  125. httpMethod:(NSString*)method
  126. ssl:(BOOL) useSSL;
  127. /*!
  128. * @abstract Creates a simple GET Operation with a request URL
  129. *
  130. * @discussion
  131. * Creates an operation with the given absolute URL.
  132. * The hostname of the engine is *NOT* prefixed
  133. * The default headers you specified in your MKNetworkEngine subclass gets added to the headers
  134. * The HTTP method is implicitly assumed to be GET.
  135. */
  136. -(MKNetworkOperation*) operationWithURLString:(NSString*) urlString;
  137. /*!
  138. * @abstract Creates a simple GET Operation with a request URL and parameters
  139. *
  140. * @discussion
  141. * Creates an operation with the given absolute URL.
  142. * The hostname of the engine is *NOT* prefixed
  143. * The default headers you specified in your MKNetworkEngine subclass gets added to the headers
  144. * The body dictionary in this method gets attached to the URL as query parameters
  145. * The HTTP method is implicitly assumed to be GET.
  146. */
  147. -(MKNetworkOperation*) operationWithURLString:(NSString*) urlString
  148. params:(NSDictionary*) body;
  149. /*!
  150. * @abstract Creates a simple Operation with a request URL, parameters and HTTP Method
  151. *
  152. * @discussion
  153. * Creates an operation with the given absolute URL.
  154. * The hostname of the engine is *NOT* prefixed
  155. * The default headers you specified in your MKNetworkEngine subclass gets added to the headers
  156. * The params dictionary in this method gets attached to the URL as query parameters if the HTTP Method is GET/DELETE
  157. * The params dictionary is attached to the body if the HTTP Method is POST/PUT
  158. * This method can be over-ridden by subclasses to tweak the operation creation mechanism.
  159. * You would typically over-ride this method to create a subclass of MKNetworkOperation (if you have one). After you create it, you should call [super prepareHeaders:operation] to attach any custom headers from super class.
  160. * @seealso
  161. * prepareHeaders:
  162. */
  163. -(MKNetworkOperation*) operationWithURLString:(NSString*) urlString
  164. params:(NSDictionary*) body
  165. httpMethod:(NSString*) method;
  166. /*!
  167. * @abstract adds the custom default headers
  168. *
  169. * @discussion
  170. * This method adds custom default headers to the factory created MKNetworkOperation.
  171. * This method can be over-ridden by subclasses to add more default headers if necessary.
  172. * You would typically over-ride this method if you have over-ridden operationWithURLString:params:httpMethod:.
  173. * @seealso
  174. * operationWithURLString:params:httpMethod:
  175. */
  176. -(void) prepareHeaders:(MKNetworkOperation*) operation;
  177. #if TARGET_OS_IPHONE
  178. /*!
  179. * @abstract Handy helper method for fetching images asynchronously in the background
  180. *
  181. * @discussion
  182. * Creates an operation with the given image URL.
  183. * The hostname of the engine is *NOT* prefixed.
  184. * The image is returned to the caller via MKNKImageBlock callback block. This image is resized as per the size and decompressed in background.
  185. * @seealso
  186. * imageAtUrl:onCompletion:
  187. */
  188. - (MKNetworkOperation*)imageAtURL:(NSURL *)url size:(CGSize) size onCompletion:(MKNKImageBlock) imageFetchedBlock DEPRECATED_ATTRIBUTE;
  189. /*!
  190. * @abstract Handy helper method for fetching images
  191. *
  192. * @discussion
  193. * Creates an operation with the given image URL.
  194. * The hostname of the engine is *NOT* prefixed.
  195. * The image is returned to the caller via MKNKImageBlock callback block.
  196. */
  197. - (MKNetworkOperation*)imageAtURL:(NSURL *)url onCompletion:(MKNKImageBlock) imageFetchedBlock DEPRECATED_ATTRIBUTE;
  198. /*!
  199. * @abstract Handy helper method for fetching images in the background
  200. *
  201. * @discussion
  202. * Creates an operation with the given image URL.
  203. * The hostname of the engine is *NOT* prefixed.
  204. * The image is returned to the caller via MKNKImageBlock callback block. This image is resized as per the size and decompressed in background.
  205. * @seealso
  206. * imageAtUrl:onCompletion:
  207. */
  208. - (MKNetworkOperation*)imageAtURL:(NSURL *)url completionHandler:(MKNKImageBlock) imageFetchedBlock errorHandler:(MKNKResponseErrorBlock) errorBlock;
  209. /*!
  210. * @abstract Handy helper method for fetching images asynchronously in the background
  211. *
  212. * @discussion
  213. * Creates an operation with the given image URL.
  214. * The hostname of the engine is *NOT* prefixed.
  215. * The image is returned to the caller via MKNKImageBlock callback block. This image is resized as per the size and decompressed in background.
  216. * @seealso
  217. * imageAtUrl:onCompletion:
  218. */
  219. - (MKNetworkOperation*)imageAtURL:(NSURL *)url size:(CGSize) size completionHandler:(MKNKImageBlock) imageFetchedBlock errorHandler:(MKNKResponseErrorBlock) errorBlock;
  220. #endif
  221. /*!
  222. * @abstract Enqueues your operation into the shared queue
  223. *
  224. * @discussion
  225. * The operation you created is enqueued to the shared queue. If the response for this operation was previously cached, the cached data will be returned.
  226. * @seealso
  227. * enqueueOperation:forceReload:
  228. */
  229. -(void) enqueueOperation:(MKNetworkOperation*) request;
  230. /*!
  231. * @abstract Enqueues your operation into the shared queue.
  232. *
  233. * @discussion
  234. * The operation you created is enqueued to the shared queue.
  235. * When forceReload is NO, this method behaves like enqueueOperation:
  236. * When forceReload is YES, No cached data will be returned even if cached data is available.
  237. * @seealso
  238. * enqueueOperation:
  239. */
  240. -(void) enqueueOperation:(MKNetworkOperation*) operation forceReload:(BOOL) forceReload;
  241. /*!
  242. * @abstract HostName of the engine
  243. * @property readonlyHostName
  244. *
  245. * @discussion
  246. * Returns the host name of the engine
  247. * This property is readonly cannot be updated.
  248. * You normally initialize an engine with its hostname using the initWithHostName:customHeaders: method
  249. */
  250. @property (readonly, copy, nonatomic) NSString *readonlyHostName;
  251. /*!
  252. * @abstract Port Number that should be used by URL creating factory methods
  253. * @property portNumber
  254. *
  255. * @discussion
  256. * Set a port number for your engine if your remote URL mandates it.
  257. * This property is optional and you DON'T have to specify the default HTTP port 80
  258. */
  259. @property (assign, nonatomic) int portNumber;
  260. /*!
  261. * @abstract WiFi only mode
  262. * @property wifiOnlyMode
  263. *
  264. * @discussion
  265. * When you set this property to YES, MKNetworkEngine will not run operations on mobile data network.
  266. */
  267. @property (assign, nonatomic) BOOL wifiOnlyMode;
  268. /*!
  269. * @abstract Sets an api path if it is different from root URL
  270. * @property apiPath
  271. *
  272. * @discussion
  273. * You can use this method to set a custom path to the API location if your server's API path is different from root (/)
  274. * This property is optional
  275. */
  276. @property (copy, nonatomic) NSString* apiPath;
  277. /*!
  278. * @abstract Handler that you implement to monitor reachability changes
  279. * @property reachabilityChangedHandler
  280. *
  281. * @discussion
  282. * The framework calls this handler whenever the reachability of the host changes.
  283. * The default implementation freezes the queued operations and stops network activity
  284. * You normally don't have to implement this unless you need to show a HUD notifying the user of connectivity loss
  285. */
  286. @property (copy, nonatomic) void (^reachabilityChangedHandler)(NetworkStatus ns);
  287. /*!
  288. * @abstract Registers an associated operation subclass
  289. *
  290. * @discussion
  291. * When you override both MKNetworkEngine and MKNetworkOperation, you might want the engine's factory method
  292. * to prepare operations of your MKNetworkOperation subclass. To create your own MKNetworkOperation subclasses from the factory method, you can register your MKNetworkOperation subclass using this method.
  293. * This method is optional. If you don't use, factory methods in MKNetworkEngine creates MKNetworkOperation objects.
  294. */
  295. -(void) registerOperationSubclass:(Class) aClass;
  296. /*!
  297. * @abstract Cache Directory Name
  298. *
  299. * @discussion
  300. * This method can be over-ridden by subclasses to provide an alternative cache directory
  301. * The default directory (MKNetworkKitCache) within the NSCaches directory will be used otherwise
  302. * Overriding this method is optional
  303. */
  304. -(NSString*) cacheDirectoryName;
  305. /*!
  306. * @abstract Cache Directory In Memory Cost
  307. *
  308. * @discussion
  309. * This method can be over-ridden by subclasses to provide an alternative in memory cache size.
  310. * By default, MKNetworkKit caches 10 recent requests in memory
  311. * The default size is 10
  312. * Overriding this method is optional
  313. */
  314. -(int) cacheMemoryCost;
  315. /*!
  316. * @abstract Enable Caching
  317. *
  318. * @discussion
  319. * This method should be called explicitly to enable caching for this engine.
  320. * By default, MKNetworkKit doens't cache your requests.
  321. * The cacheMemoryCost and cacheDirectoryName will be used when you turn caching on using this method.
  322. */
  323. -(void) useCache;
  324. /*!
  325. * @abstract Empties previously cached data
  326. *
  327. * @discussion
  328. * This method is a handy helper that you can use to clear cached data.
  329. * By default, MKNetworkKit doens't cache your requests. Use this only when you enabled caching
  330. * @seealso
  331. * useCache
  332. */
  333. -(void) emptyCache;
  334. /*!
  335. * @abstract Checks current reachable status
  336. *
  337. * @discussion
  338. * This method is a handy helper that you can use to check for network reachability.
  339. */
  340. -(BOOL) isReachable;
  341. @end