AFHTTPSessionManager.h 15 KB

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