From 5f86589d2a8e40b2389b0c9c707e25acaaa3026d Mon Sep 17 00:00:00 2001 From: Lavare Date: Fri, 24 Feb 2017 16:38:30 +0800 Subject: [PATCH] ADD: custom request validator --- YTKNetwork/YTKBaseRequest.h | 3 +++ YTKNetwork/YTKBaseRequest.m | 4 ++++ YTKNetwork/YTKNetworkAgent.m | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/YTKNetwork/YTKBaseRequest.h b/YTKNetwork/YTKBaseRequest.h index c79fc59..0f405fc 100644 --- a/YTKNetwork/YTKBaseRequest.h +++ b/YTKNetwork/YTKBaseRequest.h @@ -332,6 +332,9 @@ typedef void(^YTKRequestCompletionBlock)(__kindof YTKBaseRequest *request); /// This validator will be used to test if `responseStatusCode` is valid. - (BOOL)statusCodeValidator; +/// Use this to custom validate with specific error. +- (BOOL)customValidatorWithError:(NSError * _Nullable __autoreleasing *)error; + @end NS_ASSUME_NONNULL_END diff --git a/YTKNetwork/YTKBaseRequest.m b/YTKNetwork/YTKBaseRequest.m index 828b01f..aaa8fc7 100644 --- a/YTKNetwork/YTKBaseRequest.m +++ b/YTKNetwork/YTKBaseRequest.m @@ -202,6 +202,10 @@ - (BOOL)statusCodeValidator { return (statusCode >= 200 && statusCode <= 299); } +- (BOOL)customValidatorWithError:(NSError * _Nullable __autoreleasing *)error { + return YES; +} + #pragma mark - NSObject - (NSString *)description { diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index dbe24ac..d9ed6cb 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -271,6 +271,12 @@ - (BOOL)validateResult:(YTKBaseRequest *)request error:(NSError * _Nullable __au } return result; } + + result = [request customValidatorWithError:error]; + if (!result) { + return result; + } + id json = [request responseJSONObject]; id validator = [request jsonValidator]; if (json && validator) {