From a87d6e792057ff649945af3daca865e9f5804677 Mon Sep 17 00:00:00 2001 From: Ezreal <453742103@qq.com> Date: Thu, 1 Dec 2016 16:40:38 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20errorcode=20?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=20=EF=BC=8C=E6=B7=BB=E5=8A=A0=20model=20?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKBaseRequest.h | 4 ++++ YTKNetwork/YTKBaseRequest.m | 5 +++++ YTKNetwork/YTKNetworkAgent.m | 27 +++++++++++++++++++++++++++ YTKNetwork/YTKNetworkConfig.h | 12 ++++++++++++ YTKNetwork/YTKNetworkPrivate.h | 1 + 5 files changed, 49 insertions(+) diff --git a/YTKNetwork/YTKBaseRequest.h b/YTKNetwork/YTKBaseRequest.h index c79fc59..0b18da0 100644 --- a/YTKNetwork/YTKBaseRequest.h +++ b/YTKNetwork/YTKBaseRequest.h @@ -166,6 +166,8 @@ typedef void(^YTKRequestCompletionBlock)(__kindof YTKBaseRequest *request); /// for the response object. Otherwise this value is nil. @property (nonatomic, strong, readonly, nullable) id responseJSONObject; +@property (nonatomic, strong, readonly, nullable) id responseModel; + /// This error can be either serialization error or network error. If nothing wrong happens /// this value will be nil. @property (nonatomic, strong, readonly, nullable) NSError *error; @@ -332,6 +334,8 @@ typedef void(^YTKRequestCompletionBlock)(__kindof YTKBaseRequest *request); /// This validator will be used to test if `responseStatusCode` is valid. - (BOOL)statusCodeValidator; +- (id)jsonToModelWithData:(NSDictionary *)dataDict; + @end NS_ASSUME_NONNULL_END diff --git a/YTKNetwork/YTKBaseRequest.m b/YTKNetwork/YTKBaseRequest.m index 828b01f..322b441 100644 --- a/YTKNetwork/YTKBaseRequest.m +++ b/YTKNetwork/YTKBaseRequest.m @@ -202,6 +202,11 @@ - (BOOL)statusCodeValidator { return (statusCode >= 200 && statusCode <= 299); } +- (id)jsonToModelWithData:(NSDictionary *)dataDict +{ + return nil; +} + #pragma mark - NSObject - (NSString *)description { diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index dbe24ac..29c92ae 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -264,6 +264,15 @@ - (void)cancelAllRequests { } - (BOOL)validateResult:(YTKBaseRequest *)request error:(NSError * _Nullable __autoreleasing *)error { + + if (_config.responseFilterProtocol != nil && [_config.responseFilterProtocol respondsToSelector:@selector(checkErrorCodeWithRequest:)]) + { + if (![_config.responseFilterProtocol checkErrorCodeWithRequest:request]) + { + return NO; + } + } + BOOL result = [request statusCodeValidator]; if (!result) { if (error) { @@ -282,6 +291,24 @@ - (BOOL)validateResult:(YTKBaseRequest *)request error:(NSError * _Nullable __au return result; } } + + if (_config.responseFilterProtocol != nil && [_config.responseFilterProtocol respondsToSelector:@selector(extractDataWithRequest:)]) + { + NSDictionary *dataDict = [_config.responseFilterProtocol extractDataWithRequest:request]; + if (dataDict != nil && [dataDict isKindOfClass:[NSDictionary class]]) + { + request.responseModel = [request jsonToModelWithData:dataDict]; + } + else + { + request.responseModel = [request jsonToModelWithData:request.responseJSONObject]; + } + }else + { + request.responseModel = [request jsonToModelWithData:request.responseJSONObject]; + } + + return YES; } diff --git a/YTKNetwork/YTKNetworkConfig.h b/YTKNetwork/YTKNetworkConfig.h index 2eb385b..66e4959 100644 --- a/YTKNetwork/YTKNetworkConfig.h +++ b/YTKNetwork/YTKNetworkConfig.h @@ -37,8 +37,18 @@ NS_ASSUME_NONNULL_BEGIN /// /// @return A new url which will be used as a new `requestUrl` - (NSString *)filterUrl:(NSString *)originUrl withRequest:(YTKBaseRequest *)request; + +@end + +@protocol YTKResponseFilterProtocol + +- (BOOL)checkErrorCodeWithRequest:(YTKBaseRequest *)request; + +- (NSDictionary *)extractDataWithRequest:(YTKBaseRequest *)request; + @end + /// YTKCacheDirPathFilterProtocol can be used to append common path components when caching response results @protocol YTKCacheDirPathFilterProtocol /// Preprocess cache path before actually saving them. @@ -75,6 +85,8 @@ NS_ASSUME_NONNULL_BEGIN /// SessionConfiguration will be used to initialize AFHTTPSessionManager. Default is nil. @property (nonatomic, strong) NSURLSessionConfiguration* sessionConfiguration; +@property (nonatomic,strong) id responseFilterProtocol; + /// Add a new URL filter. - (void)addUrlFilter:(id)filter; /// Remove all URL filters. diff --git a/YTKNetwork/YTKNetworkPrivate.h b/YTKNetwork/YTKNetworkPrivate.h index f9361c6..2ed297f 100644 --- a/YTKNetwork/YTKNetworkPrivate.h +++ b/YTKNetwork/YTKNetworkPrivate.h @@ -63,6 +63,7 @@ FOUNDATION_EXPORT void YTKLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2); @property (nonatomic, strong, readwrite, nullable) NSData *responseData; @property (nonatomic, strong, readwrite, nullable) id responseJSONObject; @property (nonatomic, strong, readwrite, nullable) id responseObject; +@property (nonatomic, strong, readwrite, nullable) id responseModel; @property (nonatomic, strong, readwrite, nullable) NSString *responseString; @property (nonatomic, strong, readwrite, nullable) NSError *error; From c38add0b60d4b66f8658c33d0bcc742017365f6e Mon Sep 17 00:00:00 2001 From: Ezreal <453742103@qq.com> Date: Thu, 1 Dec 2016 17:16:14 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E6=A3=80=E6=9F=A5jsonToModelWithData=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0=20=20=E9=98=B2=E6=8A=A4=20crash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKNetworkAgent.m | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index 29c92ae..e14373a 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -297,15 +297,24 @@ - (BOOL)validateResult:(YTKBaseRequest *)request error:(NSError * _Nullable __au NSDictionary *dataDict = [_config.responseFilterProtocol extractDataWithRequest:request]; if (dataDict != nil && [dataDict isKindOfClass:[NSDictionary class]]) { - request.responseModel = [request jsonToModelWithData:dataDict]; + if ([request respondsToSelector:@selector(jsonToModelWithData:)]) + { + request.responseModel = [request jsonToModelWithData:dataDict]; + } } else { - request.responseModel = [request jsonToModelWithData:request.responseJSONObject]; + if ([request respondsToSelector:@selector(jsonToModelWithData:)]) + { + request.responseModel = [request jsonToModelWithData:request.responseJSONObject]; + } } }else { - request.responseModel = [request jsonToModelWithData:request.responseJSONObject]; + if ([request respondsToSelector:@selector(jsonToModelWithData:)]) + { + request.responseModel = [request jsonToModelWithData:request.responseJSONObject]; + } } From 85265be042b7533d01b3806db3336063f726ae7f Mon Sep 17 00:00:00 2001 From: Ezreal <453742103@qq.com> Date: Thu, 1 Dec 2016 17:28:46 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20responseModel=20?= =?UTF-8?q?=E4=BC=9A=20crash=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKBaseRequest.m | 1 + 1 file changed, 1 insertion(+) diff --git a/YTKNetwork/YTKBaseRequest.m b/YTKNetwork/YTKBaseRequest.m index 322b441..40dcf42 100644 --- a/YTKNetwork/YTKBaseRequest.m +++ b/YTKNetwork/YTKBaseRequest.m @@ -39,6 +39,7 @@ @interface YTKBaseRequest () @property (nonatomic, strong, readwrite) NSData *responseData; @property (nonatomic, strong, readwrite) id responseJSONObject; @property (nonatomic, strong, readwrite) id responseObject; +@property (nonatomic, strong, readwrite) id responseModel; @property (nonatomic, strong, readwrite) NSString *responseString; @property (nonatomic, strong, readwrite) NSError *error; From 69301ed1778d489b00dec2ab2f7a389c8856f52c Mon Sep 17 00:00:00 2001 From: Ezreal <453742103@qq.com> Date: Fri, 10 Mar 2017 14:37:30 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20serializationError?= =?UTF-8?q?=20=E7=B1=BB=E5=9E=8B=E4=B8=8D=E5=8C=B9=E9=85=8D=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=8E=A5=E5=8F=A3=E8=BF=94=E5=9B=9E=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKNetworkAgent.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index e14373a..11607c8 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -364,10 +364,10 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response if (error) { succeed = NO; requestError = error; - } else if (serializationError) { + }/* else if (serializationError) { succeed = NO; requestError = serializationError; - } else { + }*/ else { succeed = [self validateResult:request error:&validationError]; requestError = validationError; } From c0412bf6910288a4e1f83515f202f4d12f3b1b43 Mon Sep 17 00:00:00 2001 From: Ezreal <453742103@qq.com> Date: Fri, 10 Mar 2017 14:55:35 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKNetworkAgent.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index 11607c8..94fe9cd 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -351,6 +351,8 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response switch (request.responseSerializerType) { case YTKResponseSerializerTypeHTTP: // Default serializer. Do nothing. + request.responseObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&serializationError]; + request.responseJSONObject = request.responseObject; break; case YTKResponseSerializerTypeJSON: request.responseObject = [self.jsonResponseSerializer responseObjectForResponse:task.response data:request.responseData error:&serializationError]; @@ -364,10 +366,10 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response if (error) { succeed = NO; requestError = error; - }/* else if (serializationError) { + }else if (serializationError) { succeed = NO; requestError = serializationError; - }*/ else { + }else { succeed = [self validateResult:request error:&validationError]; requestError = validationError; } From dc8b535854543d70902c0e6e784e5a4ded549f6d Mon Sep 17 00:00:00 2001 From: Ezreal <453742103@qq.com> Date: Mon, 20 Mar 2017 11:28:07 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E4=B8=8D=E5=8C=B9=E9=85=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKNetworkAgent.m | 2 -- 1 file changed, 2 deletions(-) diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index 94fe9cd..c62cca1 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -351,8 +351,6 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response switch (request.responseSerializerType) { case YTKResponseSerializerTypeHTTP: // Default serializer. Do nothing. - request.responseObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&serializationError]; - request.responseJSONObject = request.responseObject; break; case YTKResponseSerializerTypeJSON: request.responseObject = [self.jsonResponseSerializer responseObjectForResponse:task.response data:request.responseData error:&serializationError]; From e0049ec1ef97cd6bba44c7bcdeaf5e7e7bde47e5 Mon Sep 17 00:00:00 2001 From: Ezreal <453742103@qq.com> Date: Thu, 30 Mar 2017 16:07:23 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E7=BB=86=E8=8A=82=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKNetworkAgent.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index c62cca1..6e8cc5e 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -295,7 +295,7 @@ - (BOOL)validateResult:(YTKBaseRequest *)request error:(NSError * _Nullable __au if (_config.responseFilterProtocol != nil && [_config.responseFilterProtocol respondsToSelector:@selector(extractDataWithRequest:)]) { NSDictionary *dataDict = [_config.responseFilterProtocol extractDataWithRequest:request]; - if (dataDict != nil && [dataDict isKindOfClass:[NSDictionary class]]) + if (dataDict != nil) { if ([request respondsToSelector:@selector(jsonToModelWithData:)]) { From 2b3320ce0ca22796e52365e7a3f85e7f4726fc7c Mon Sep 17 00:00:00 2001 From: ezreal <453742103@qq.com> Date: Thu, 12 Oct 2017 15:02:32 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20request=20=20start/f?= =?UTF-8?q?inish=20=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKBaseRequest.h | 4 ++++ YTKNetwork/YTKBaseRequest.m | 7 +++++++ YTKNetwork/YTKNetworkAgent.m | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/YTKNetwork/YTKBaseRequest.h b/YTKNetwork/YTKBaseRequest.h index 87d6cb4..6fc5e9f 100644 --- a/YTKNetwork/YTKBaseRequest.h +++ b/YTKNetwork/YTKBaseRequest.h @@ -336,6 +336,10 @@ typedef void(^YTKRequestCompletionBlock)(__kindof YTKBaseRequest *request); - (id)jsonToModelWithData:(NSDictionary *)dataDict; +-(void)requestStart; + +-(void)requestFinish + @end NS_ASSUME_NONNULL_END diff --git a/YTKNetwork/YTKBaseRequest.m b/YTKNetwork/YTKBaseRequest.m index 40dcf42..9f7ccd4 100644 --- a/YTKNetwork/YTKBaseRequest.m +++ b/YTKNetwork/YTKBaseRequest.m @@ -208,6 +208,13 @@ - (id)jsonToModelWithData:(NSDictionary *)dataDict return nil; } +-(void)requestStart +{ +} + +-(void)requestFinish +{ +} #pragma mark - NSObject - (NSString *)description { diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index d9f076e..67ffbf4 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -235,6 +235,9 @@ - (void)addRequest:(YTKBaseRequest *)request { // Retain request YTKLog(@"Add request: %@", NSStringFromClass([request class])); [self addRequestToRecord:request]; + + [request requestStart]; + [request.requestTask resume]; } @@ -346,6 +349,8 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response YTKLog(@"Finished Request: %@", NSStringFromClass([request class])); + [request requestFinish]; + NSError * __autoreleasing serializationError = nil; NSError * __autoreleasing validationError = nil; From 3cd92722362abe3338a53d18e14443d5bec2370e Mon Sep 17 00:00:00 2001 From: ezreal <453742103@qq.com> Date: Thu, 12 Oct 2017 15:05:03 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=B0=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKBaseRequest.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/YTKNetwork/YTKBaseRequest.h b/YTKNetwork/YTKBaseRequest.h index 6fc5e9f..4ce1e41 100644 --- a/YTKNetwork/YTKBaseRequest.h +++ b/YTKNetwork/YTKBaseRequest.h @@ -338,7 +338,7 @@ typedef void(^YTKRequestCompletionBlock)(__kindof YTKBaseRequest *request); -(void)requestStart; --(void)requestFinish +-(void)requestFinish; @end From 7cb9e7ccce3df8287329dded33eafca69e71d9ae Mon Sep 17 00:00:00 2001 From: ezreal <453742103@qq.com> Date: Thu, 12 Oct 2017 15:15:27 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20requestfinish=20?= =?UTF-8?q?=E7=9A=84=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKNetworkAgent.m | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index 67ffbf4..1a8aee6 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -348,8 +348,6 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response } YTKLog(@"Finished Request: %@", NSStringFromClass([request class])); - - [request requestFinish]; NSError * __autoreleasing serializationError = nil; NSError * __autoreleasing validationError = nil; @@ -375,6 +373,9 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response break; } } + + [request requestFinish]; + if (error) { succeed = NO; requestError = error; From 82577efc2371e8aa1b7b0a497e159a63a9ee83df Mon Sep 17 00:00:00 2001 From: ezreal <453742103@qq.com> Date: Thu, 12 Oct 2017 15:31:08 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=20[request=20reques?= =?UTF-8?q?tFinish];=20=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKNetworkAgent.m | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index 1a8aee6..e9d05c0 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -374,8 +374,6 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response } } - [request requestFinish]; - if (error) { succeed = NO; requestError = error; @@ -388,6 +386,7 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response } if (succeed) { + [request requestFinish]; [self requestDidSucceedWithRequest:request]; } else { [self requestDidFailWithRequest:request error:requestError]; From babc3bbed4d24aab1f94f07a1a2e27ecf5a82310 Mon Sep 17 00:00:00 2001 From: ezreal <453742103@qq.com> Date: Tue, 17 Oct 2017 12:52:45 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E5=85=B3=E9=97=ADhttps=20=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YTKNetwork/YTKNetworkConfig.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/YTKNetwork/YTKNetworkConfig.m b/YTKNetwork/YTKNetworkConfig.m index ac5cdf7..bdaf073 100644 --- a/YTKNetwork/YTKNetworkConfig.m +++ b/YTKNetwork/YTKNetworkConfig.m @@ -52,6 +52,8 @@ - (instancetype)init { _urlFilters = [NSMutableArray array]; _cacheDirPathFilters = [NSMutableArray array]; _securityPolicy = [AFSecurityPolicy defaultPolicy]; + _securityPolicy.allowInvalidCertificates = YES; + _securityPolicy.validatesDomainName = NO; _debugLogEnabled = NO; } return self;