AFHTTPRequestOperationManager.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // AFHTTPRequestOperationManager.m
  2. //
  3. // Copyright (c) 2013-2014 AFNetworking (http://afnetworking.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import <Foundation/Foundation.h>
  23. #import "AFHTTPRequestOperationManager.h"
  24. #import "AFHTTPRequestOperation.h"
  25. #import <Availability.h>
  26. #import <Security/Security.h>
  27. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  28. #import <UIKit/UIKit.h>
  29. #endif
  30. @interface AFHTTPRequestOperationManager ()
  31. @property (readwrite, nonatomic, strong) NSURL *baseURL;
  32. @end
  33. @implementation AFHTTPRequestOperationManager
  34. + (instancetype)manager {
  35. return [[self alloc] initWithBaseURL:nil];
  36. }
  37. - (instancetype)init {
  38. return [self initWithBaseURL:nil];
  39. }
  40. - (instancetype)initWithBaseURL:(NSURL *)url {
  41. self = [super init];
  42. if (!self) {
  43. return nil;
  44. }
  45. // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
  46. if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
  47. url = [url URLByAppendingPathComponent:@""];
  48. }
  49. self.baseURL = url;
  50. self.requestSerializer = [AFHTTPRequestSerializer serializer];
  51. self.responseSerializer = [AFJSONResponseSerializer serializer];
  52. self.securityPolicy = [AFSecurityPolicy defaultPolicy];
  53. self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
  54. self.operationQueue = [[NSOperationQueue alloc] init];
  55. self.shouldUseCredentialStorage = YES;
  56. return self;
  57. }
  58. #pragma mark -
  59. #ifdef _SYSTEMCONFIGURATION_H
  60. #endif
  61. - (void)setRequestSerializer:(AFHTTPRequestSerializer <AFURLRequestSerialization> *)requestSerializer {
  62. NSParameterAssert(requestSerializer);
  63. _requestSerializer = requestSerializer;
  64. }
  65. - (void)setResponseSerializer:(AFHTTPResponseSerializer <AFURLResponseSerialization> *)responseSerializer {
  66. NSParameterAssert(responseSerializer);
  67. _responseSerializer = responseSerializer;
  68. }
  69. #pragma mark -
  70. - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
  71. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  72. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  73. {
  74. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  75. operation.responseSerializer = self.responseSerializer;
  76. operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage;
  77. operation.credential = self.credential;
  78. operation.securityPolicy = self.securityPolicy;
  79. [operation setCompletionBlockWithSuccess:success failure:failure];
  80. operation.completionQueue = self.completionQueue;
  81. operation.completionGroup = self.completionGroup;
  82. return operation;
  83. }
  84. #pragma mark -
  85. - (AFHTTPRequestOperation *)GET:(NSString *)URLString
  86. parameters:(id)parameters
  87. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  88. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  89. {
  90. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  91. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  92. [self.operationQueue addOperation:operation];
  93. return operation;
  94. }
  95. - (AFHTTPRequestOperation *)HEAD:(NSString *)URLString
  96. parameters:(id)parameters
  97. success:(void (^)(AFHTTPRequestOperation *operation))success
  98. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  99. {
  100. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"HEAD" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  101. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *requestOperation, __unused id responseObject) {
  102. if (success) {
  103. success(requestOperation);
  104. }
  105. } failure:failure];
  106. [self.operationQueue addOperation:operation];
  107. return operation;
  108. }
  109. - (AFHTTPRequestOperation *)POST:(NSString *)URLString
  110. parameters:(id)parameters
  111. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  112. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  113. {
  114. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  115. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  116. [self.operationQueue addOperation:operation];
  117. return operation;
  118. }
  119. - (AFHTTPRequestOperation *)POST:(NSString *)URLString
  120. parameters:(id)parameters
  121. constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
  122. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  123. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  124. {
  125. NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:nil];
  126. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  127. [self.operationQueue addOperation:operation];
  128. return operation;
  129. }
  130. - (AFHTTPRequestOperation *)PUT:(NSString *)URLString
  131. parameters:(id)parameters
  132. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  133. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  134. {
  135. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"PUT" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  136. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  137. [self.operationQueue addOperation:operation];
  138. return operation;
  139. }
  140. - (AFHTTPRequestOperation *)PATCH:(NSString *)URLString
  141. parameters:(id)parameters
  142. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  143. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  144. {
  145. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"PATCH" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  146. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  147. [self.operationQueue addOperation:operation];
  148. return operation;
  149. }
  150. - (AFHTTPRequestOperation *)DELETE:(NSString *)URLString
  151. parameters:(id)parameters
  152. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  153. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  154. {
  155. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"DELETE" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  156. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  157. [self.operationQueue addOperation:operation];
  158. return operation;
  159. }
  160. #pragma mark - NSObject
  161. - (NSString *)description {
  162. return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.operationQueue];
  163. }
  164. #pragma mark - NSSecureCoding
  165. + (BOOL)supportsSecureCoding {
  166. return YES;
  167. }
  168. - (id)initWithCoder:(NSCoder *)decoder {
  169. NSURL *baseURL = [decoder decodeObjectForKey:NSStringFromSelector(@selector(baseURL))];
  170. self = [self initWithBaseURL:baseURL];
  171. if (!self) {
  172. return nil;
  173. }
  174. self.requestSerializer = [decoder decodeObjectOfClass:[AFHTTPRequestSerializer class] forKey:NSStringFromSelector(@selector(requestSerializer))];
  175. self.responseSerializer = [decoder decodeObjectOfClass:[AFHTTPResponseSerializer class] forKey:NSStringFromSelector(@selector(responseSerializer))];
  176. return self;
  177. }
  178. - (void)encodeWithCoder:(NSCoder *)coder {
  179. [coder encodeObject:self.baseURL forKey:NSStringFromSelector(@selector(baseURL))];
  180. [coder encodeObject:self.requestSerializer forKey:NSStringFromSelector(@selector(requestSerializer))];
  181. [coder encodeObject:self.responseSerializer forKey:NSStringFromSelector(@selector(responseSerializer))];
  182. }
  183. #pragma mark - NSCopying
  184. - (id)copyWithZone:(NSZone *)zone {
  185. AFHTTPRequestOperationManager *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL];
  186. HTTPClient.requestSerializer = [self.requestSerializer copyWithZone:zone];
  187. HTTPClient.responseSerializer = [self.responseSerializer copyWithZone:zone];
  188. return HTTPClient;
  189. }
  190. @end