diff --git a/YBNetwork.podspec b/YBNetwork.podspec new file mode 100644 index 0000000..cbe1945 --- /dev/null +++ b/YBNetwork.podspec @@ -0,0 +1,29 @@ + + +Pod::Spec.new do |s| + + + s.name = "YBNetwork" + s.version = "1.0" + s.summary = "基于 AFNetworking 网络中间层,注重性能,设计简洁,易于拓展" + s.description = <<-DESC + 基于 AFNetworking 网络中间层,注重性能,设计简洁,易于拓展。 + DESC + + s.homepage = "https://github.com/indulgeIn" + + s.license = "MIT" + + s.author = { "杨波" => "1106355439@qq.com" } + + s.platform = :ios, "8.0" + + s.source = { :git => "https://github.com/indulgeIn/YBNetwork.git", :tag => "#{s.version}" } + + s.source_files = "YBNetwork/**/*.{h,m}" + + s.dependency 'AFNetworking' + + s.requires_arc = true + +end diff --git a/YBNetwork/Cache/YBNetworkCache+Internal.h b/YBNetwork/Cache/YBNetworkCache+Internal.h index d7b6fa3..9b03a83 100644 --- a/YBNetwork/Cache/YBNetworkCache+Internal.h +++ b/YBNetwork/Cache/YBNetworkCache+Internal.h @@ -2,8 +2,8 @@ // YBNetworkCache+Internal.h // YBNetwork // -// Created by 杨波 on 2019/4/7. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/7. +// Copyright © 2019 波儿菜. All rights reserved. // #import "YBNetworkCache.h" diff --git a/YBNetwork/Cache/YBNetworkCache.h b/YBNetwork/Cache/YBNetworkCache.h index 63a9f5d..20fb646 100644 --- a/YBNetwork/Cache/YBNetworkCache.h +++ b/YBNetwork/Cache/YBNetworkCache.h @@ -2,8 +2,8 @@ // YBNetworkCache.h // YBNetwork // -// Created by 杨波 on 2019/4/5. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/5. +// Copyright © 2019 波儿菜. All rights reserved. // #import diff --git a/YBNetwork/Cache/YBNetworkCache.m b/YBNetwork/Cache/YBNetworkCache.m index fce6737..b2ff843 100644 --- a/YBNetwork/Cache/YBNetworkCache.m +++ b/YBNetwork/Cache/YBNetworkCache.m @@ -2,8 +2,8 @@ // YBNetworkCache.m // YBNetwork // -// Created by 杨波 on 2019/4/5. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/5. +// Copyright © 2019 波儿菜. All rights reserved. // #import "YBNetworkCache.h" diff --git a/YBNetwork/Manger/YBBaseRequest+Internal.h b/YBNetwork/Manger/YBBaseRequest+Internal.h index 8d77619..c2cba36 100644 --- a/YBNetwork/Manger/YBBaseRequest+Internal.h +++ b/YBNetwork/Manger/YBBaseRequest+Internal.h @@ -2,8 +2,8 @@ // YBBaseRequest+Internal.h // YBNetwork // -// Created by 杨波 on 2019/4/3. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/3. +// Copyright © 2019 波儿菜. All rights reserved. // #import "YBBaseRequest.h" diff --git a/YBNetwork/Manger/YBNetworkManager.h b/YBNetwork/Manger/YBNetworkManager.h index f8ab323..1e9a29b 100644 --- a/YBNetwork/Manger/YBNetworkManager.h +++ b/YBNetwork/Manger/YBNetworkManager.h @@ -2,8 +2,8 @@ // YBNetworkManager.h // YBNetwork // -// Created by 杨波 on 2019/4/2. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/2. +// Copyright © 2019 波儿菜. All rights reserved. // #import diff --git a/YBNetwork/Manger/YBNetworkManager.m b/YBNetwork/Manger/YBNetworkManager.m index 0fb32ce..94172b1 100644 --- a/YBNetwork/Manger/YBNetworkManager.m +++ b/YBNetwork/Manger/YBNetworkManager.m @@ -2,8 +2,8 @@ // YBNetworkManager.m // YBNetwork // -// Created by 杨波 on 2019/4/2. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/2. +// Copyright © 2019 波儿菜. All rights reserved. // #import "YBNetworkManager.h" diff --git a/YBNetwork/Response/YBNetworkResponse.h b/YBNetwork/Response/YBNetworkResponse.h index e6edca1..92f171c 100644 --- a/YBNetwork/Response/YBNetworkResponse.h +++ b/YBNetwork/Response/YBNetworkResponse.h @@ -2,8 +2,8 @@ // YBNetworkResponse.h // YBNetwork // -// Created by 杨波 on 2019/4/6. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/6. +// Copyright © 2019 波儿菜. All rights reserved. // #import diff --git a/YBNetwork/Response/YBNetworkResponse.m b/YBNetwork/Response/YBNetworkResponse.m index fb943bb..0baab4d 100644 --- a/YBNetwork/Response/YBNetworkResponse.m +++ b/YBNetwork/Response/YBNetworkResponse.m @@ -2,8 +2,8 @@ // YBNetworkResponse.m // YBNetwork // -// Created by 杨波 on 2019/4/6. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/6. +// Copyright © 2019 波儿菜. All rights reserved. // #import "YBNetworkResponse.h" diff --git a/YBNetwork/YBBaseRequest.h b/YBNetwork/YBBaseRequest.h index edc5de4..09b10a4 100644 --- a/YBNetwork/YBBaseRequest.h +++ b/YBNetwork/YBBaseRequest.h @@ -2,8 +2,8 @@ // YBBaseRequest.h // YBNetwork // -// Created by 杨波 on 2019/4/3. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/3. +// Copyright © 2019 波儿菜. All rights reserved. // #import @@ -101,7 +101,7 @@ NS_ASSUME_NONNULL_BEGIN @end -/// 预处理请求数据 (重载分类方法) +/// 预处理请求数据 (重写分类方法) @interface YBBaseRequest (PreprocessRequest) /** 预处理请求参数, 返回处理后的请求参数 */ @@ -113,9 +113,12 @@ NS_ASSUME_NONNULL_BEGIN @end -/// 预处理响应数据 (重载分类方法) +/// 预处理响应数据 (重写分类方法) @interface YBBaseRequest (PreprocessResponse) +/** 是否将响应成功转换为响应错误(在某些情况下服务器返回一些代表访问错误的状态码,就可以重写这个方法直接转换为错误响应) */ +- (BOOL)yb_preprocessShouldFailedWithResponse:(YBNetworkResponse *)response; + /** 预处理请求成功数据 (子线程执行, 若数据来自缓存在主线程执行) */ - (void)yb_preprocessSuccessInChildThreadWithResponse:(YBNetworkResponse *)response; diff --git a/YBNetwork/YBBaseRequest.m b/YBNetwork/YBBaseRequest.m index 388be39..316b31e 100644 --- a/YBNetwork/YBBaseRequest.m +++ b/YBNetwork/YBBaseRequest.m @@ -2,8 +2,8 @@ // YBBaseRequest.m // YBNetwork // -// Created by 杨波 on 2019/4/3. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/3. +// Copyright © 2019 波儿菜. All rights reserved. // #import "YBBaseRequest.h" @@ -194,7 +194,12 @@ - (void)requestDownloadProgress:(NSProgress *)progress { } - (void)requestCompletionWithResponse:(YBNetworkResponse *)response cacheKey:(NSString *)cacheKey fromCache:(BOOL)fromCache taskID:(NSNumber *)taskID { - if (response.error) { + BOOL shouldFailed = nil != response.error; + if (!shouldFailed && [self respondsToSelector:@selector(yb_preprocessShouldFailedWithResponse:)]) { + shouldFailed = [self yb_preprocessShouldFailedWithResponse:response]; + } + + if (shouldFailed) { [self failureWithResponse:response]; } else { [self successWithResponse:response cacheKey:cacheKey fromCache:NO]; diff --git a/YBNetwork/YBNetworkDefine.h b/YBNetwork/YBNetworkDefine.h index 9302db6..919c908 100644 --- a/YBNetwork/YBNetworkDefine.h +++ b/YBNetwork/YBNetworkDefine.h @@ -2,8 +2,8 @@ // YBNetworkDefine.h // YBNetwork // -// Created by 杨波 on 2019/4/3. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/3. +// Copyright © 2019 波儿菜. All rights reserved. // #ifndef YBNetworkDefine_h @@ -40,39 +40,57 @@ typedef NS_ENUM(NSInteger, YBRequestMethod) { /// 网络响应错误类型 typedef NS_ENUM(NSInteger, YBResponseErrorType) { - YBResponseErrorTypeNone, //无 - YBResponseErrorTypeTimedOut, //超时 - YBResponseErrorTypeCancelled, //取消 - YBResponseErrorTypeNoNetwork //无网络 + /// 未知 + YBResponseErrorTypeUnknown, + /// 超时 + YBResponseErrorTypeTimedOut, + /// 取消 + YBResponseErrorTypeCancelled, + /// 无网络 + YBResponseErrorTypeNoNetwork, + /// 服务器错误 + YBResponseErrorTypeServerError }; /// 缓存存储模式 typedef NS_OPTIONS(NSUInteger, YBNetworkCacheWriteMode) { - YBNetworkCacheWriteModeNone = 0, //无缓存 - YBNetworkCacheWriteModeMemory = 1 << 0, //内存缓存 - YBNetworkCacheWriteModeDisk = 1 << 1, //磁盘缓存 + /// 无缓存 + YBNetworkCacheWriteModeNone = 0, + /// 内存缓存 + YBNetworkCacheWriteModeMemory = 1 << 0, + /// 磁盘缓存 + YBNetworkCacheWriteModeDisk = 1 << 1, YBNetworkCacheWriteModeMemoryAndDisk = YBNetworkCacheWriteModeMemory | YBNetworkCacheWriteModeDisk }; /// 缓存读取模式 typedef NS_ENUM(NSInteger, YBNetworkCacheReadMode) { - YBNetworkCacheReadModeNone, //不读取缓存 - YBNetworkCacheReadModeAlsoNetwork, //缓存命中后仍然发起网络请求 - YBNetworkCacheReadModeCancelNetwork, //缓存命中后不发起网络请求 + /// 不读取缓存 + YBNetworkCacheReadModeNone, + /// 缓存命中后仍然发起网络请求 + YBNetworkCacheReadModeAlsoNetwork, + /// 缓存命中后不发起网络请求 + YBNetworkCacheReadModeCancelNetwork, }; /// 网络请求释放策略 typedef NS_ENUM(NSInteger, YBNetworkReleaseStrategy) { - YBNetworkReleaseStrategyHoldRequest, //网络任务会持有 YBBaseRequest 实例,网络任务完成 YBBaseRequest 实例才会释放 - YBNetworkReleaseStrategyWhenRequestDealloc, //网络请求将随着 YBBaseRequest 实例的释放而取消 - YBNetworkReleaseStrategyNotCareRequest //网络请求和 YBBaseRequest 实例无关联 + /// 网络任务会持有 YBBaseRequest 实例,网络任务完成 YBBaseRequest 实例才会释放 + YBNetworkReleaseStrategyHoldRequest, + /// 网络请求将随着 YBBaseRequest 实例的释放而取消 + YBNetworkReleaseStrategyWhenRequestDealloc, + /// 网络请求和 YBBaseRequest 实例无关联 + YBNetworkReleaseStrategyNotCareRequest }; /// 重复网络请求处理策略 typedef NS_ENUM(NSInteger, YBNetworkRepeatStrategy) { - YBNetworkRepeatStrategyAllAllowed, //允许重复网络请求 - YBNetworkRepeatStrategyCancelOldest, //取消最旧的网络请求 - YBNetworkRepeatStrategyCancelNewest //取消最新的网络请求 + /// 允许重复网络请求 + YBNetworkRepeatStrategyAllAllowed, + /// 取消最旧的网络请求 + YBNetworkRepeatStrategyCancelOldest, + /// 取消最新的网络请求 + YBNetworkRepeatStrategyCancelNewest }; diff --git a/YBNetworkDemo/AppDelegate.h b/YBNetworkDemo/AppDelegate.h index d6f8e72..378490a 100644 --- a/YBNetworkDemo/AppDelegate.h +++ b/YBNetworkDemo/AppDelegate.h @@ -2,8 +2,8 @@ // AppDelegate.h // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import diff --git a/YBNetworkDemo/AppDelegate.m b/YBNetworkDemo/AppDelegate.m index 0661494..3b4a707 100644 --- a/YBNetworkDemo/AppDelegate.m +++ b/YBNetworkDemo/AppDelegate.m @@ -2,8 +2,8 @@ // AppDelegate.m // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import "AppDelegate.h" diff --git a/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.h b/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.h index e06c8a1..e4f3efd 100644 --- a/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.h +++ b/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.h @@ -2,8 +2,8 @@ // DefaultRequest.h // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import "YBBaseRequest.h" diff --git a/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m b/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m index c879fde..b2bc665 100644 --- a/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m +++ b/YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m @@ -2,8 +2,8 @@ // DefaultRequest.m // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import "DefaultServerRequest.h" @@ -38,6 +38,15 @@ - (void)start { [super start]; } +- (BOOL)yb_preprocessShouldFailedWithResponse:(YBNetworkResponse *)response { + NSDictionary *responseDic = response.responseObject; + if ([responseDic isKindOfClass:NSDictionary.self] && [[NSString stringWithFormat:@"%@", responseDic[@"error_code"]] isEqualToString:@"2"]) { + response.errorType = YBResponseErrorTypeServerError; + return YES; + } + return NO; +} + - (NSDictionary *)yb_preprocessParameter:(NSDictionary *)parameter { NSMutableDictionary *tmp = [NSMutableDictionary dictionaryWithDictionary:parameter ?: @{}]; tmp[@"test_deviceID"] = @"test250"; //给每一个请求,添加额外的参数 diff --git a/YBNetworkDemo/TestCase/APITeams/OtherServerRequest.h b/YBNetworkDemo/TestCase/APITeams/OtherServerRequest.h index a18c930..dc5ff8f 100644 --- a/YBNetworkDemo/TestCase/APITeams/OtherServerRequest.h +++ b/YBNetworkDemo/TestCase/APITeams/OtherServerRequest.h @@ -2,8 +2,8 @@ // OtherRequest.h // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import "YBBaseRequest.h" diff --git a/YBNetworkDemo/TestCase/APITeams/OtherServerRequest.m b/YBNetworkDemo/TestCase/APITeams/OtherServerRequest.m index 594c9e9..3debe52 100644 --- a/YBNetworkDemo/TestCase/APITeams/OtherServerRequest.m +++ b/YBNetworkDemo/TestCase/APITeams/OtherServerRequest.m @@ -2,8 +2,8 @@ // OtherRequest.m // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import "OtherServerRequest.h" diff --git a/YBNetworkDemo/TestCase/TestViewController.h b/YBNetworkDemo/TestCase/TestViewController.h index b9a1277..6e90a26 100644 --- a/YBNetworkDemo/TestCase/TestViewController.h +++ b/YBNetworkDemo/TestCase/TestViewController.h @@ -2,8 +2,8 @@ // TestViewController.h // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import diff --git a/YBNetworkDemo/TestCase/TestViewController.m b/YBNetworkDemo/TestCase/TestViewController.m index d254fa8..cb8126b 100644 --- a/YBNetworkDemo/TestCase/TestViewController.m +++ b/YBNetworkDemo/TestCase/TestViewController.m @@ -2,8 +2,8 @@ // TestViewController.m // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import "TestViewController.h" @@ -61,11 +61,11 @@ - (void)searchA { [request startWithSuccess:^(YBNetworkResponse * _Nonnull response) { __strong typeof(weakSelf) self = weakSelf; if (!self) return; - NSLog(@"response success : %@", response.responseObject); + NSLog(@"\nresponse success : %@", response.responseObject); } failure:^(YBNetworkResponse * _Nonnull response) { __strong typeof(weakSelf) self = weakSelf; if (!self) return; - NSLog(@"response failure : 类型 : %@", @(response.errorType)); + NSLog(@"\nresponse failure : 类型 : %@", @(response.errorType)); }]; } @@ -76,11 +76,11 @@ - (void)searchB { #pragma mark - - (void)request:(__kindof YBBaseRequest *)request successWithResponse:(YBNetworkResponse *)response { - NSLog(@"response success : %@", response.responseObject); + NSLog(@"\nresponse success : %@", response.responseObject); } - (void)request:(__kindof YBBaseRequest *)request failureWithResponse:(YBNetworkResponse *)response { - NSLog(@"response failure : 类型 : %@", @(response.errorType)); + NSLog(@"\nresponse failure : 类型 : %@", @(response.errorType)); } #pragma mark - getter diff --git a/YBNetworkDemo/ViewController.h b/YBNetworkDemo/ViewController.h index d01e7f1..369e911 100644 --- a/YBNetworkDemo/ViewController.h +++ b/YBNetworkDemo/ViewController.h @@ -2,8 +2,8 @@ // ViewController.h // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import diff --git a/YBNetworkDemo/ViewController.m b/YBNetworkDemo/ViewController.m index 09381fb..1c9c44c 100644 --- a/YBNetworkDemo/ViewController.m +++ b/YBNetworkDemo/ViewController.m @@ -2,8 +2,8 @@ // ViewController.m // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import "ViewController.h" diff --git a/YBNetworkDemo/main.m b/YBNetworkDemo/main.m index b7d5c9a..6656913 100644 --- a/YBNetworkDemo/main.m +++ b/YBNetworkDemo/main.m @@ -2,8 +2,8 @@ // main.m // YBNetworkDemo // -// Created by 杨波 on 2019/4/9. -// Copyright © 2019 杨波. All rights reserved. +// Created by 波儿菜 on 2019/4/9. +// Copyright © 2019 波儿菜. All rights reserved. // #import