AFHTTPSessionManager.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #import "AFURLSessionManager.h"
  27. /**
  28. `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.
  29. ## Subclassing Notes
  30. 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.
  31. ## Methods to Override
  32. To change the behavior of all data task operation construction, which is also used in the `GET` / `POST` / et al. convenience methods, override `dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:`.
  33. ## Serialization
  34. 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>`.
  35. Responses received from the server are automatically validated and serialized by the `responseSerializers` property, which is an object conforming to `<AFURLResponseSerialization>`
  36. ## URL Construction Using Relative Paths
  37. 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:`.
  38. Below are a few examples of how `baseURL` and relative paths interact:
  39. NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
  40. [NSURL URLWithString:@"foo" relativeToURL:baseURL]; // http://example.com/v1/foo
  41. [NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz
  42. [NSURL URLWithString:@"/foo" relativeToURL:baseURL]; // http://example.com/foo
  43. [NSURL URLWithString:@"foo/" relativeToURL:baseURL]; // http://example.com/v1/foo
  44. [NSURL URLWithString:@"/foo/" relativeToURL:baseURL]; // http://example.com/foo/
  45. [NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/
  46. 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.
  47. @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.
  48. */
  49. NS_ASSUME_NONNULL_BEGIN
  50. @interface AFHTTPSessionManager : AFURLSessionManager <NSSecureCoding, NSCopying>
  51. /**
  52. The URL used to construct requests from relative paths in methods like `requestWithMethod:URLString:parameters:`, and the `GET` / `POST` / et al. convenience methods.
  53. */
  54. @property (readonly, nonatomic, strong, nullable) NSURL *baseURL;
  55. /**
  56. 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.
  57. @warning `requestSerializer` must not be `nil`.
  58. */
  59. @property (nonatomic, strong) AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer;
  60. /**
  61. 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`.
  62. @warning `responseSerializer` must not be `nil`.
  63. */
  64. @property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;
  65. ///-------------------------------
  66. /// @name Managing Security Policy
  67. ///-------------------------------
  68. /**
  69. The security policy used by created session to evaluate server trust for secure connections. `AFURLSessionManager` uses the `defaultPolicy` unless otherwise specified. A security policy configured with `AFSSLPinningModePublicKey` or `AFSSLPinningModeCertificate` can only be applied on a session manager initialized with a secure base URL (i.e. https). Applying a security policy with pinning enabled on an insecure session manager throws an `Invalid Security Policy` exception.
  70. */
  71. @property (nonatomic, strong) AFSecurityPolicy *securityPolicy;
  72. ///---------------------
  73. /// @name Initialization
  74. ///---------------------
  75. /**
  76. Creates and returns an `AFHTTPSessionManager` object.
  77. */
  78. + (instancetype)manager;
  79. /**
  80. Initializes an `AFHTTPSessionManager` object with the specified base URL.
  81. @param url The base URL for the HTTP client.
  82. @return The newly-initialized HTTP client
  83. */
  84. - (instancetype)initWithBaseURL:(nullable NSURL *)url;
  85. /**
  86. Initializes an `AFHTTPSessionManager` object with the specified base URL.
  87. This is the designated initializer.
  88. @param url The base URL for the HTTP client.
  89. @param configuration The configuration used to create the managed session.
  90. @return The newly-initialized HTTP client
  91. */
  92. - (instancetype)initWithBaseURL:(nullable NSURL *)url
  93. sessionConfiguration:(nullable NSURLSessionConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
  94. ///---------------------------
  95. /// @name Making HTTP Requests
  96. ///---------------------------
  97. /**
  98. Creates and runs an `NSURLSessionDataTask` with a `GET` request.
  99. @param URLString The URL string used to create the request URL.
  100. @param parameters The parameters to be encoded according to the client request serializer.
  101. @param headers The headers appended to the default headers for this request.
  102. @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.
  103. @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.
  104. @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.
  105. @see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
  106. */
  107. - (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
  108. parameters:(nullable id)parameters
  109. headers:(nullable NSDictionary <NSString *, NSString *> *)headers
  110. progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
  111. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  112. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  113. /**
  114. Creates and runs an `NSURLSessionDataTask` with a `HEAD` request.
  115. @param URLString The URL string used to create the request URL.
  116. @param parameters The parameters to be encoded according to the client request serializer.
  117. @param headers The headers appended to the default headers for this request.
  118. @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.
  119. @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.
  120. @see -dataTaskWithRequest:completionHandler:
  121. */
  122. - (nullable NSURLSessionDataTask *)HEAD:(NSString *)URLString
  123. parameters:(nullable id)parameters
  124. headers:(nullable NSDictionary <NSString *, NSString *> *)headers
  125. success:(nullable void (^)(NSURLSessionDataTask *task))success
  126. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  127. /**
  128. Creates and runs an `NSURLSessionDataTask` with a `POST` request.
  129. @param URLString The URL string used to create the request URL.
  130. @param parameters The parameters to be encoded according to the client request serializer.
  131. @param headers The headers appended to the default headers for this request.
  132. @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.
  133. @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.
  134. @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.
  135. @see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
  136. */
  137. - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
  138. parameters:(nullable id)parameters
  139. headers:(nullable NSDictionary <NSString *, NSString *> *)headers
  140. progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress
  141. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  142. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  143. /**
  144. Creates and runs an `NSURLSessionDataTask` with a multipart `POST` request.
  145. @param URLString The URL string used to create the request URL.
  146. @param parameters The parameters to be encoded according to the client request serializer.
  147. @param headers The headers appended to the default headers for this request.
  148. @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.
  149. @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.
  150. @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.
  151. @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.
  152. @see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
  153. */
  154. - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
  155. parameters:(nullable id)parameters
  156. headers:(nullable NSDictionary <NSString *, NSString *> *)headers
  157. constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block
  158. progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress
  159. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  160. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  161. /**
  162. Creates and runs an `NSURLSessionDataTask` with a `PUT` request.
  163. @param URLString The URL string used to create the request URL.
  164. @param parameters The parameters to be encoded according to the client request serializer.
  165. @param headers The headers appended to the default headers for this request.
  166. @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.
  167. @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.
  168. @see -dataTaskWithRequest:completionHandler:
  169. */
  170. - (nullable NSURLSessionDataTask *)PUT:(NSString *)URLString
  171. parameters:(nullable id)parameters
  172. headers:(nullable NSDictionary <NSString *, NSString *> *)headers
  173. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  174. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  175. /**
  176. Creates and runs an `NSURLSessionDataTask` with a `PATCH` request.
  177. @param URLString The URL string used to create the request URL.
  178. @param parameters The parameters to be encoded according to the client request serializer.
  179. @param headers The headers appended to the default headers for this request.
  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:completionHandler:
  183. */
  184. - (nullable NSURLSessionDataTask *)PATCH:(NSString *)URLString
  185. parameters:(nullable id)parameters
  186. headers:(nullable NSDictionary <NSString *, NSString *> *)headers
  187. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  188. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  189. /**
  190. Creates and runs an `NSURLSessionDataTask` with a `DELETE` request.
  191. @param URLString The URL string used to create the request URL.
  192. @param parameters The parameters to be encoded according to the client request serializer.
  193. @param headers The headers appended to the default headers for this request.
  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 *)DELETE:(NSString *)URLString
  199. parameters:(nullable id)parameters
  200. headers:(nullable NSDictionary <NSString *, NSString *> *)headers
  201. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  202. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  203. /**
  204. Creates an `NSURLSessionDataTask` with a custom `HTTPMethod` request.
  205. @param method The HTTPMethod string used to create the request.
  206. @param URLString The URL string used to create the request URL.
  207. @param parameters The parameters to be encoded according to the client request serializer.
  208. @param headers The headers appended to the default headers for this request.
  209. @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.
  210. @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.
  211. @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.
  212. @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.
  213. @see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
  214. */
  215. - (nullable NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
  216. URLString:(NSString *)URLString
  217. parameters:(nullable id)parameters
  218. headers:(nullable NSDictionary <NSString *, NSString *> *)headers
  219. uploadProgress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress
  220. downloadProgress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
  221. success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
  222. failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
  223. @end
  224. NS_ASSUME_NONNULL_END