AFHTTPSessionManager.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // AFHTTPSessionManager.h
  2. // Copyright (c) 2011–2015 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 <Availability.h>
  26. #if __IPHONE_OS_VERSION_MIN_REQUIRED
  27. #import <MobileCoreServices/MobileCoreServices.h>
  28. #else
  29. #import <CoreServices/CoreServices.h>
  30. #endif
  31. #import "AFURLSessionManager.h"
  32. #ifndef NS_DESIGNATED_INITIALIZER
  33. #if __has_attribute(objc_designated_initializer)
  34. #define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
  35. #else
  36. #define NS_DESIGNATED_INITIALIZER
  37. #endif
  38. #endif
  39. /**
  40. `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.
  41. ## Subclassing Notes
  42. 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.
  43. For developers targeting iOS 6 or Mac OS X 10.8 or earlier, `AFHTTPRequestOperationManager` may be used to similar effect.
  44. ## Methods to Override
  45. 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:`.
  46. ## Serialization
  47. 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>`.
  48. Responses received from the server are automatically validated and serialized by the `responseSerializers` property, which is an object conforming to `<AFURLResponseSerialization>`
  49. ## URL Construction Using Relative Paths
  50. 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:`.
  51. Below are a few examples of how `baseURL` and relative paths interact:
  52. NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
  53. [NSURL URLWithString:@"foo" relativeToURL:baseURL]; // http://example.com/v1/foo
  54. [NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz
  55. [NSURL URLWithString:@"/foo" relativeToURL:baseURL]; // http://example.com/foo
  56. [NSURL URLWithString:@"foo/" relativeToURL:baseURL]; // http://example.com/v1/foo
  57. [NSURL URLWithString:@"/foo/" relativeToURL:baseURL]; // http://example.com/foo/
  58. [NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/
  59. 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.
  60. @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.
  61. */
  62. #if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090) || TARGET_OS_WATCH
  63. NS_ASSUME_NONNULL_BEGIN
  64. @interface AFHTTPSessionManager : AFURLSessionManager <NSSecureCoding, NSCopying>
  65. /**
  66. The URL used to construct requests from relative paths in methods like `requestWithMethod:URLString:parameters:`, and the `GET` / `POST` / et al. convenience methods.
  67. */
  68. @property (readonly, nonatomic, strong, nullable) NSURL *baseURL;
  69. /**
  70. 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.
  71. @warning `requestSerializer` must not be `nil`.
  72. */
  73. @property (nonatomic, strong) AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer;
  74. /**
  75. 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`.
  76. @warning `responseSerializer` must not be `nil`.
  77. */
  78. @property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;
  79. ///---------------------
  80. /// @name Initialization
  81. ///---------------------
  82. /**
  83. Creates and returns an `AFHTTPSessionManager` object.
  84. */
  85. + (instancetype)manager;
  86. /**
  87. Initializes an `AFHTTPSessionManager` object with the specified base URL.
  88. @param url The base URL for the HTTP client.
  89. @return The newly-initialized HTTP client
  90. */
  91. - (instancetype)initWithBaseURL:(nullable NSURL *)url;
  92. /**
  93. Initializes an `AFHTTPSessionManager` object with the specified base URL.
  94. This is the designated initializer.
  95. @param url The base URL for the HTTP client.
  96. @param configuration The configuration used to create the managed session.
  97. @return The newly-initialized HTTP client
  98. */
  99. - (instancetype)initWithBaseURL:(nullable NSURL *)url
  100. sessionConfiguration:(nullable NSURLSessionConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
  101. ///---------------------------
  102. /// @name Making HTTP Requests
  103. ///---------------------------
  104. /**
  105. Creates and runs an `NSURLSessionDataTask` with a `GET` request.
  106. @param URLString The URL string used to create the request URL.
  107. @param parameters The parameters to be encoded according to the client request serializer.
  108. @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.
  109. @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.
  110. @see -dataTaskWithRequest:completionHandler:
  111. */
  112. - (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
  113. parameters:(nullable id)parameters
  114. success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success
  115. failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure;
  116. /**
  117. Creates and runs an `NSURLSessionDataTask` with a `HEAD` request.
  118. @param URLString The URL string used to create the request URL.
  119. @param parameters The parameters to be encoded according to the client request serializer.
  120. @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.
  121. @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.
  122. @see -dataTaskWithRequest:completionHandler:
  123. */
  124. - (nullable NSURLSessionDataTask *)HEAD:(NSString *)URLString
  125. parameters:(nullable id)parameters
  126. success:(nullable void (^)(NSURLSessionDataTask *task))success
  127. failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure;
  128. /**
  129. Creates and runs an `NSURLSessionDataTask` with a `POST` request.
  130. @param URLString The URL string used to create the request URL.
  131. @param parameters The parameters to be encoded according to the client request serializer.
  132. @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.
  133. @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.
  134. @see -dataTaskWithRequest:completionHandler:
  135. */
  136. - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
  137. parameters:(nullable id)parameters
  138. success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success
  139. failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure;
  140. /**
  141. Creates and runs an `NSURLSessionDataTask` with a multipart `POST` request.
  142. @param URLString The URL string used to create the request URL.
  143. @param parameters The parameters to be encoded according to the client request serializer.
  144. @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.
  145. @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.
  146. @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.
  147. @see -dataTaskWithRequest:completionHandler:
  148. */
  149. - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
  150. parameters:(nullable id)parameters
  151. constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block
  152. success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success
  153. failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure;
  154. /**
  155. Creates and runs an `NSURLSessionDataTask` with a `PUT` request.
  156. @param URLString The URL string used to create the request URL.
  157. @param parameters The parameters to be encoded according to the client request serializer.
  158. @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.
  159. @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.
  160. @see -dataTaskWithRequest:completionHandler:
  161. */
  162. - (nullable NSURLSessionDataTask *)PUT:(NSString *)URLString
  163. parameters:(nullable id)parameters
  164. success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success
  165. failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure;
  166. /**
  167. Creates and runs an `NSURLSessionDataTask` with a `PATCH` request.
  168. @param URLString The URL string used to create the request URL.
  169. @param parameters The parameters to be encoded according to the client request serializer.
  170. @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.
  171. @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.
  172. @see -dataTaskWithRequest:completionHandler:
  173. */
  174. - (nullable NSURLSessionDataTask *)PATCH:(NSString *)URLString
  175. parameters:(nullable id)parameters
  176. success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success
  177. failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure;
  178. /**
  179. Creates and runs an `NSURLSessionDataTask` with a `DELETE` request.
  180. @param URLString The URL string used to create the request URL.
  181. @param parameters The parameters to be encoded according to the client request serializer.
  182. @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.
  183. @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.
  184. @see -dataTaskWithRequest:completionHandler:
  185. */
  186. - (nullable NSURLSessionDataTask *)DELETE:(NSString *)URLString
  187. parameters:(nullable id)parameters
  188. success:(nullable void (^)(NSURLSessionDataTask *task, id responseObject))success
  189. failure:(nullable void (^)(NSURLSessionDataTask *task, NSError *error))failure;
  190. @end
  191. NS_ASSUME_NONNULL_END
  192. #endif