AFHTTPSessionManager.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // AFHTTPSessionManager.h
  2. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  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. #import <Foundation/Foundation.h>
  22. #if !TARGET_OS_WATCH
  23. #import <SystemConfiguration/SystemConfiguration.h>
  24. #endif
  25. #import <TargetConditionals.h>
  26. #if TARGET_OS_IOS || TARGET_OS_WATCH || TARGET_OS_TV
  27. #import <MobileCoreServices/MobileCoreServices.h>
  28. #else
  29. #import <CoreServices/CoreServices.h>
  30. #endif
  31. #import "AFURLSessionManager.h"
  32. /**
  33. `AFHTTPSessionManager` is a subclass of `AFURLSessionManager` with convenience methods for making HTTP requests. When a `baseURL` is provided, requests made with the `GET` / `POST` / et al. convenience methods can be made with relative paths.
  34. ## Subclassing Notes
  35. Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass `AFHTTPSessionManager`, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application.
  36. For developers targeting iOS 6 or Mac OS X 10.8 or earlier, `AFHTTPRequestOperationManager` may be used to similar effect.
  37. ## Methods to Override
  38. To change the behavior of all data task operation construction, which is also used in the `GET` / `POST` / et al. convenience methods, override `dataTaskWithRequest:completionHandler:`.
  39. ## Serialization
  40. Requests created by an HTTP client will contain default headers and encode parameters according to the `requestSerializer` property, which is an object conforming to `<AFURLRequestSerialization>`.
  41. Responses received from the server are automatically validated and serialized by the `responseSerializers` property, which is an object conforming to `<AFURLResponseSerialization>`
  42. ## URL Construction Using Relative Paths
  43. For HTTP convenience methods, the request serializer constructs URLs from the path relative to the `-baseURL`, using `NSURL +URLWithString:relativeToURL:`, when provided. If `baseURL` is `nil`, `path` needs to resolve to a valid `NSURL` object using `NSURL +URLWithString:`.
  44. Below are a few examples of how `baseURL` and relative paths interact:
  45. NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
  46. [NSURL URLWithString:@"foo" relativeToURL:baseURL]; // http://example.com/v1/foo
  47. [NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz
  48. [NSURL URLWithString:@"/foo" relativeToURL:baseURL]; // http://example.com/foo
  49. [NSURL URLWithString:@"foo/" relativeToURL:baseURL]; // http://example.com/v1/foo
  50. [NSURL URLWithString:@"/foo/" relativeToURL:baseURL]; // http://example.com/foo/
  51. [NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/
  52. Also important to note is that a trailing slash will be added to any `baseURL` without one. This would otherwise cause unexpected behavior when constructing URLs using paths without a leading slash.
  53. @warning Managers for background sessions must be owned for the duration of their use. This can be accomplished by creating an application-wide or shared singleton instance.
  54. */
  55. NS_ASSUME_NONNULL_BEGIN
  56. @interface AFHTTPSessionManager : AFURLSessionManager <NSSecureCoding, NSCopying>
  57. /**
  58. The URL used to construct requests from relative paths in methods like `requestWithMethod:URLString:parameters:`, and the `GET` / `POST` / et al. convenience methods.
  59. */
  60. @property (readonly, nonatomic, strong, nullable) NSURL *baseURL;
  61. /**
  62. Requests created with `requestWithMethod:URLString:parameters:` & `multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:` are constructed with a set of default headers using a parameter serialization specified by this property. By default, this is set to an instance of `AFHTTPRequestSerializer`, which serializes query string parameters for `GET`, `HEAD`, and `DELETE` requests, or otherwise URL-form-encodes HTTP message bodies.
  63. @warning `requestSerializer` must not be `nil`.
  64. */
  65. @property (nonatomic, strong) AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer;
  66. /**
  67. Responses sent from the server in data tasks created with `dataTaskWithRequest:success:failure:` and run using the `GET` / `POST` / et al. convenience methods are automatically validated and serialized by the response serializer. By default, this property is set to an instance of `AFJSONResponseSerializer`.
  68. @warning `responseSerializer` must not be `nil`.
  69. */
  70. @property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;
  71. ///---------------------
  72. /// @name Initialization
  73. ///---------------------
  74. /**
  75. Creates and returns an `AFHTTPSessionManager` object.
  76. */
  77. + (instancetype)manager;
  78. /**
  79. Initializes an `AFHTTPSessionManager` object with the specified base URL.
  80. @param url The base URL for the HTTP client.
  81. @return The newly-initialized HTTP client
  82. */
  83. - (instancetype)initWithBaseURL:(nullable NSURL *)url;
  84. /**
  85. Initializes an `AFHTTPSessionManager` object with the specified base URL.
  86. This is the designated initializer.
  87. @param url The base URL for the HTTP client.
  88. @param configuration The configuration used to create the managed session.
  89. @return The newly-initialized HTTP client
  90. */
  91. - (instancetype)initWithBaseURL:(nullable NSURL *)url
  92. sessionConfiguration:(nullable NSURLSessionConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
  93. ///---------------------------
  94. /// @name Making HTTP Requests
  95. ///---------------------------
  96. /**
  97. Creates and runs an `NSURLSessionDataTask` with a `GET` request.
  98. @param URLString The URL string used to create the request URL.
  99. @param parameters The parameters to be encoded according to the client request serializer.
  100. @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
  101. @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
  102. @see -dataTaskWithRequest:completionHandler:
  103. */
  104. - (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
  105. parameters:(nullable id)parameters
  106. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  107. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE;
  108. /**
  109. Creates and runs an `NSURLSessionDataTask` with a `GET` request.
  110. @param URLString The URL string used to create the request URL.
  111. @param parameters The parameters to be encoded according to the client request serializer.
  112. @param downloadProgress A block object to be executed when the download progress is updated. Note this block is called on the session queue, not the main queue.
  113. @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
  114. @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
  115. @see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
  116. */
  117. - (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
  118. parameters:(nullable id)parameters
  119. progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
  120. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  121. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  122. /**
  123. Creates and runs an `NSURLSessionDataTask` with a `HEAD` request.
  124. @param URLString The URL string used to create the request URL.
  125. @param parameters The parameters to be encoded according to the client request serializer.
  126. @param success A block object to be executed when the task finishes successfully. This block has no return value and takes a single arguments: the data task.
  127. @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
  128. @see -dataTaskWithRequest:completionHandler:
  129. */
  130. - (nullable NSURLSessionDataTask *)HEAD:(NSString *)URLString
  131. parameters:(nullable id)parameters
  132. success:(nullable void (^)(NSURLSessionDataTask *task))success
  133. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  134. /**
  135. Creates and runs an `NSURLSessionDataTask` with a `POST` request.
  136. @param URLString The URL string used to create the request URL.
  137. @param parameters The parameters to be encoded according to the client request serializer.
  138. @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
  139. @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
  140. @see -dataTaskWithRequest:completionHandler:
  141. */
  142. - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
  143. parameters:(nullable id)parameters
  144. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  145. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE;
  146. /**
  147. Creates and runs an `NSURLSessionDataTask` with a `POST` request.
  148. @param URLString The URL string used to create the request URL.
  149. @param parameters The parameters to be encoded according to the client request serializer.
  150. @param uploadProgress A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue.
  151. @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
  152. @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
  153. @see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
  154. */
  155. - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
  156. parameters:(nullable id)parameters
  157. progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress
  158. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  159. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  160. /**
  161. Creates and runs an `NSURLSessionDataTask` with a multipart `POST` request.
  162. @param URLString The URL string used to create the request URL.
  163. @param parameters The parameters to be encoded according to the client request serializer.
  164. @param block A block that takes a single argument and appends data to the HTTP body. The block argument is an object adopting the `AFMultipartFormData` protocol.
  165. @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
  166. @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
  167. @see -dataTaskWithRequest:completionHandler:
  168. */
  169. - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
  170. parameters:(nullable id)parameters
  171. constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block
  172. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  173. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE;
  174. /**
  175. Creates and runs an `NSURLSessionDataTask` with a multipart `POST` request.
  176. @param URLString The URL string used to create the request URL.
  177. @param parameters The parameters to be encoded according to the client request serializer.
  178. @param block A block that takes a single argument and appends data to the HTTP body. The block argument is an object adopting the `AFMultipartFormData` protocol.
  179. @param uploadProgress A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue.
  180. @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
  181. @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
  182. @see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
  183. */
  184. - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
  185. parameters:(nullable id)parameters
  186. constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block
  187. progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress
  188. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  189. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  190. /**
  191. Creates and runs an `NSURLSessionDataTask` with a `PUT` request.
  192. @param URLString The URL string used to create the request URL.
  193. @param parameters The parameters to be encoded according to the client request serializer.
  194. @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
  195. @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
  196. @see -dataTaskWithRequest:completionHandler:
  197. */
  198. - (nullable NSURLSessionDataTask *)PUT:(NSString *)URLString
  199. parameters:(nullable id)parameters
  200. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  201. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  202. /**
  203. Creates and runs an `NSURLSessionDataTask` with a `PATCH` request.
  204. @param URLString The URL string used to create the request URL.
  205. @param parameters The parameters to be encoded according to the client request serializer.
  206. @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
  207. @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
  208. @see -dataTaskWithRequest:completionHandler:
  209. */
  210. - (nullable NSURLSessionDataTask *)PATCH:(NSString *)URLString
  211. parameters:(nullable id)parameters
  212. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  213. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  214. /**
  215. Creates and runs an `NSURLSessionDataTask` with a `DELETE` request.
  216. @param URLString The URL string used to create the request URL.
  217. @param parameters The parameters to be encoded according to the client request serializer.
  218. @param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
  219. @param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
  220. @see -dataTaskWithRequest:completionHandler:
  221. */
  222. - (nullable NSURLSessionDataTask *)DELETE:(NSString *)URLString
  223. parameters:(nullable id)parameters
  224. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  225. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  226. @end
  227. NS_ASSUME_NONNULL_END