Skip to content

Commit

Permalink
[feature] 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
indulgeIn committed Aug 19, 2019
1 parent 1739a5d commit 10f27eb
Show file tree
Hide file tree
Showing 23 changed files with 131 additions and 67 deletions.
29 changes: 29 additions & 0 deletions YBNetwork.podspec
Original file line number Diff line number Diff line change
@@ -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 = { "杨波" => "[email protected]" }

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
4 changes: 2 additions & 2 deletions YBNetwork/Cache/YBNetworkCache+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBNetworkCache+Internal.h
// YBNetwork<https://github.com/indulgeIn/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"
Expand Down
4 changes: 2 additions & 2 deletions YBNetwork/Cache/YBNetworkCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBNetworkCache.h
// YBNetwork<https://github.com/indulgeIn/YBNetwork>
//
// Created by 杨波 on 2019/4/5.
// Copyright © 2019 杨波. All rights reserved.
// Created by 波儿菜 on 2019/4/5.
// Copyright © 2019 波儿菜. All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions YBNetwork/Cache/YBNetworkCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBNetworkCache.m
// YBNetwork<https://github.com/indulgeIn/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"
Expand Down
4 changes: 2 additions & 2 deletions YBNetwork/Manger/YBBaseRequest+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBBaseRequest+Internal.h
// YBNetwork<https://github.com/indulgeIn/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"
Expand Down
4 changes: 2 additions & 2 deletions YBNetwork/Manger/YBNetworkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBNetworkManager.h
// YBNetwork<https://github.com/indulgeIn/YBNetwork>
//
// Created by 杨波 on 2019/4/2.
// Copyright © 2019 杨波. All rights reserved.
// Created by 波儿菜 on 2019/4/2.
// Copyright © 2019 波儿菜. All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions YBNetwork/Manger/YBNetworkManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBNetworkManager.m
// YBNetwork<https://github.com/indulgeIn/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"
Expand Down
4 changes: 2 additions & 2 deletions YBNetwork/Response/YBNetworkResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBNetworkResponse.h
// YBNetwork<https://github.com/indulgeIn/YBNetwork>
//
// Created by 杨波 on 2019/4/6.
// Copyright © 2019 杨波. All rights reserved.
// Created by 波儿菜 on 2019/4/6.
// Copyright © 2019 波儿菜. All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down
4 changes: 2 additions & 2 deletions YBNetwork/Response/YBNetworkResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBNetworkResponse.m
// YBNetwork<https://github.com/indulgeIn/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"
Expand Down
11 changes: 7 additions & 4 deletions YBNetwork/YBBaseRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBBaseRequest.h
// YBNetwork<https://github.com/indulgeIn/YBNetwork>
//
// Created by 杨波 on 2019/4/3.
// Copyright © 2019 杨波. All rights reserved.
// Created by 波儿菜 on 2019/4/3.
// Copyright © 2019 波儿菜. All rights reserved.
//

#import <Foundation/Foundation.h>
Expand Down Expand Up @@ -101,7 +101,7 @@ NS_ASSUME_NONNULL_BEGIN
@end


/// 预处理请求数据 (重载分类方法)
/// 预处理请求数据 (重写分类方法)
@interface YBBaseRequest (PreprocessRequest)

/** 预处理请求参数, 返回处理后的请求参数 */
Expand All @@ -113,9 +113,12 @@ NS_ASSUME_NONNULL_BEGIN
@end


/// 预处理响应数据 (重载分类方法)
/// 预处理响应数据 (重写分类方法)
@interface YBBaseRequest (PreprocessResponse)

/** 是否将响应成功转换为响应错误(在某些情况下服务器返回一些代表访问错误的状态码,就可以重写这个方法直接转换为错误响应) */
- (BOOL)yb_preprocessShouldFailedWithResponse:(YBNetworkResponse *)response;

/** 预处理请求成功数据 (子线程执行, 若数据来自缓存在主线程执行) */
- (void)yb_preprocessSuccessInChildThreadWithResponse:(YBNetworkResponse *)response;

Expand Down
11 changes: 8 additions & 3 deletions YBNetwork/YBBaseRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBBaseRequest.m
// YBNetwork<https://github.com/indulgeIn/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"
Expand Down Expand Up @@ -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];
Expand Down
54 changes: 36 additions & 18 deletions YBNetwork/YBNetworkDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// YBNetworkDefine.h
// YBNetwork<https://github.com/indulgeIn/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
Expand Down Expand Up @@ -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
};


Expand Down
4 changes: 2 additions & 2 deletions YBNetworkDemo/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <UIKit/UIKit.h>
Expand Down
4 changes: 2 additions & 2 deletions YBNetworkDemo/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 11 additions & 2 deletions YBNetworkDemo/TestCase/APITeams/DefaultServerRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"; //给每一个请求,添加额外的参数
Expand Down
4 changes: 2 additions & 2 deletions YBNetworkDemo/TestCase/APITeams/OtherServerRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions YBNetworkDemo/TestCase/APITeams/OtherServerRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions YBNetworkDemo/TestCase/TestViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <UIKit/UIKit.h>
Expand Down
12 changes: 6 additions & 6 deletions YBNetworkDemo/TestCase/TestViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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));
}];
}

Expand All @@ -76,11 +76,11 @@ - (void)searchB {
#pragma mark - <YBResponseDelegate>

- (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
Expand Down
Loading

0 comments on commit 10f27eb

Please sign in to comment.