Skip to content

Commit

Permalink
MOD: improve some naming
Browse files Browse the repository at this point in the history
Change-Id: Ie1c516379a955461b75daaef78b59a429a93b63a
  • Loading branch information
liujl committed Aug 22, 2016
1 parent 7d3f666 commit 9436033
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 60 deletions.
20 changes: 20 additions & 0 deletions MigrationGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,23 @@ YTKNetwork 2.0 中加入了用于控制是否进行异步写缓存的接口:
## 响应前向处理

`- (void)requestCompleteFilter``- (void)requestFailedFilter` 对应, YTKNetwork 2.0 中加入了用于在响应结束,但是切换回主线程之前执行操作的函数 `- (void)requestCompletePreprocessor``- (void)requestFailedPreprocessor`,在这里执行的操作,可以避免卡顿主线程。

## 命名变更

YTKNetwork 2.0 中部分函数命名发生了变更:

* `[YTKNetworkAgent sharedInstance]` -> `[YTKNetworkAgent sharedAgent]`
* `[YTKChainRequestAgent sharedInstance]` -> `[YTKChainRequestAgent sharedAgent]`
* `[YTKBatchRequestAgent sharedInstance]` -> `[YTKBatchRequestAgent sharedAgent]`
* `[YTKNetworkConfig sharedInstance]` -> `[YTKNetworkConfig sharedConfig]`

部分枚举的命名也发生了变化:

* `YTKRequestMethodGet` -> `YTKRequestMethodGET`
* `YTKRequestMethodPost` -> `YTKRequestMethodPOST`
* ...

同时 `ChainCallback` 类型被重命名为 `YTKChainCallback`

```typedef void (^YTKChainCallback)(YTKChainRequest *chainRequest, YTKBaseRequest *baseRequest);```

21 changes: 12 additions & 9 deletions YTKNetwork/YTKBaseRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ NS_ASSUME_NONNULL_BEGIN

/// HTTP Request method.
typedef NS_ENUM(NSInteger, YTKRequestMethod) {
YTKRequestMethodGet = 0,
YTKRequestMethodPost,
YTKRequestMethodHead,
YTKRequestMethodPut,
YTKRequestMethodDelete,
YTKRequestMethodPatch,
YTKRequestMethodGET = 0,
YTKRequestMethodPOST,
YTKRequestMethodHEAD,
YTKRequestMethodPUT,
YTKRequestMethodDELETE,
YTKRequestMethodPATCH,
};

/// Request serializer type.
Expand All @@ -44,9 +44,12 @@ typedef NS_ENUM(NSInteger, YTKRequestSerializerType) {
/// Response serializer type, which determines response serialization process and
/// the type of `responseObject`.
typedef NS_ENUM(NSInteger, YTKResponseSerializerType) {
YTKResponseSerializerTypeHTTP = 0, /// NSData
YTKResponseSerializerTypeJSON, /// JSON object
YTKResponseSerializerTypeXMLParser /// NSXMLParser
/// NSData type
YTKResponseSerializerTypeHTTP,
/// JSON object type
YTKResponseSerializerTypeJSON,
/// NSXMLParser type
YTKResponseSerializerTypeXMLParser,
};

/// Request priority
Expand Down
6 changes: 3 additions & 3 deletions YTKNetwork/YTKBaseRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ - (void)addAccessory:(id<YTKRequestAccessory>)accessory {

- (void)start {
[self toggleAccessoriesWillStartCallBack];
[[YTKNetworkAgent sharedInstance] addRequest:self];
[[YTKNetworkAgent sharedAgent] addRequest:self];
}

- (void)stop {
[self toggleAccessoriesWillStopCallBack];
self.delegate = nil;
[[YTKNetworkAgent sharedInstance] cancelRequest:self];
[[YTKNetworkAgent sharedAgent] cancelRequest:self];
[self toggleAccessoriesDidStopCallBack];
}

Expand Down Expand Up @@ -160,7 +160,7 @@ - (id)cacheFileNameFilterForRequestArgument:(id)argument {
}

- (YTKRequestMethod)requestMethod {
return YTKRequestMethodGet;
return YTKRequestMethodGET;
}

- (YTKRequestSerializerType)requestSerializerType {
Expand Down
8 changes: 4 additions & 4 deletions YTKNetwork/YTKBatchRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ - (void)start {
return;
}
_failedRequest = nil;
[[YTKBatchRequestAgent sharedInstance] addBatchRequest:self];
[[YTKBatchRequestAgent sharedAgent] addBatchRequest:self];
[self toggleAccessoriesWillStartCallBack];
for (YTKRequest * req in _requestArray) {
req.delegate = self;
Expand All @@ -68,7 +68,7 @@ - (void)stop {
_delegate = nil;
[self clearRequest];
[self toggleAccessoriesDidStopCallBack];
[[YTKBatchRequestAgent sharedInstance] removeBatchRequest:self];
[[YTKBatchRequestAgent sharedAgent] removeBatchRequest:self];
}

- (void)startWithCompletionBlockWithSuccess:(void (^)(YTKBatchRequest *batchRequest))success
Expand Down Expand Up @@ -118,7 +118,7 @@ - (void)requestFinished:(YTKRequest *)request {
}
[self clearCompletionBlock];
[self toggleAccessoriesDidStopCallBack];
[[YTKBatchRequestAgent sharedInstance] removeBatchRequest:self];
[[YTKBatchRequestAgent sharedAgent] removeBatchRequest:self];
}
}

Expand All @@ -140,7 +140,7 @@ - (void)requestFailed:(YTKRequest *)request {
[self clearCompletionBlock];

[self toggleAccessoriesDidStopCallBack];
[[YTKBatchRequestAgent sharedInstance] removeBatchRequest:self];
[[YTKBatchRequestAgent sharedAgent] removeBatchRequest:self];
}

- (void)clearRequest {
Expand Down
2 changes: 1 addition & 1 deletion YTKNetwork/YTKBatchRequestAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)new NS_UNAVAILABLE;

/// Get the shared batch request agent.
+ (YTKBatchRequestAgent *)sharedInstance;
+ (YTKBatchRequestAgent *)sharedAgent;

/// Add a batch request.
- (void)addBatchRequest:(YTKBatchRequest *)request;
Expand Down
2 changes: 1 addition & 1 deletion YTKNetwork/YTKBatchRequestAgent.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ @interface YTKBatchRequestAgent()

@implementation YTKBatchRequestAgent

+ (YTKBatchRequestAgent *)sharedInstance {
+ (YTKBatchRequestAgent *)sharedAgent {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Expand Down
8 changes: 4 additions & 4 deletions YTKNetwork/YTKChainRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ - (void)start {
if ([_requestArray count] > 0) {
[self toggleAccessoriesWillStartCallBack];
[self startNextRequest];
[[YTKChainRequestAgent sharedInstance] addChainRequest:self];
[[YTKChainRequestAgent sharedAgent] addChainRequest:self];
} else {
YTKLog(@"Error! Chain request array is empty.");
}
Expand All @@ -68,7 +68,7 @@ - (void)start {
- (void)stop {
[self toggleAccessoriesWillStopCallBack];
[self clearRequest];
[[YTKChainRequestAgent sharedInstance] removeChainRequest:self];
[[YTKChainRequestAgent sharedAgent] removeChainRequest:self];
[self toggleAccessoriesDidStopCallBack];
}

Expand Down Expand Up @@ -107,7 +107,7 @@ - (void)requestFinished:(YTKBaseRequest *)request {
[self toggleAccessoriesWillStopCallBack];
if ([_delegate respondsToSelector:@selector(chainRequestFinished:)]) {
[_delegate chainRequestFinished:self];
[[YTKChainRequestAgent sharedInstance] removeChainRequest:self];
[[YTKChainRequestAgent sharedAgent] removeChainRequest:self];
}
[self toggleAccessoriesDidStopCallBack];
}
Expand All @@ -117,7 +117,7 @@ - (void)requestFailed:(YTKBaseRequest *)request {
[self toggleAccessoriesWillStopCallBack];
if ([_delegate respondsToSelector:@selector(chainRequestFailed:failedBaseRequest:)]) {
[_delegate chainRequestFailed:self failedBaseRequest:request];
[[YTKChainRequestAgent sharedInstance] removeChainRequest:self];
[[YTKChainRequestAgent sharedAgent] removeChainRequest:self];
}
[self toggleAccessoriesDidStopCallBack];
}
Expand Down
2 changes: 1 addition & 1 deletion YTKNetwork/YTKChainRequestAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)new NS_UNAVAILABLE;

/// Get the shared chain request agent.
+ (YTKChainRequestAgent *)sharedInstance;
+ (YTKChainRequestAgent *)sharedAgent;

/// Add a chain request.
- (void)addChainRequest:(YTKChainRequest *)request;
Expand Down
2 changes: 1 addition & 1 deletion YTKNetwork/YTKChainRequestAgent.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ @interface YTKChainRequestAgent()

@implementation YTKChainRequestAgent

+ (YTKChainRequestAgent *)sharedInstance {
+ (YTKChainRequestAgent *)sharedAgent {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Expand Down
2 changes: 1 addition & 1 deletion YTKNetwork/YTKNetworkAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)new NS_UNAVAILABLE;

/// Get the shared agent.
+ (YTKNetworkAgent *)sharedInstance;
+ (YTKNetworkAgent *)sharedAgent;

/// Add request to session and start it.
- (void)addRequest:(YTKBaseRequest *)request;
Expand Down
16 changes: 8 additions & 8 deletions YTKNetwork/YTKNetworkAgent.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ @implementation YTKNetworkAgent {
NSIndexSet *_allStatusCodes;
}

+ (YTKNetworkAgent *)sharedInstance {
+ (YTKNetworkAgent *)sharedAgent {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Expand All @@ -61,7 +61,7 @@ + (YTKNetworkAgent *)sharedInstance {
- (instancetype)init {
self = [super init];
if (self) {
_config = [YTKNetworkConfig sharedInstance];
_config = [YTKNetworkConfig sharedConfig];
_manager = [AFHTTPSessionManager manager];
_requestsRecord = [NSMutableDictionary dictionary];
_processingQueue = dispatch_queue_create("com.yuantiku.networkagent.processing", DISPATCH_QUEUE_CONCURRENT);
Expand Down Expand Up @@ -176,7 +176,7 @@ - (void)addRequest:(YTKBaseRequest *)request {
}];
request.requestTask = dataTask;
} else {
if (method == YTKRequestMethodGet) {
if (method == YTKRequestMethodGET) {
if (request.resumableDownloadPath) {
// add parameters to URL;
NSString *filteredUrl = [YTKNetworkPrivate urlStringWithOriginUrlString:url appendParameters:param];
Expand Down Expand Up @@ -232,7 +232,7 @@ - (void)addRequest:(YTKBaseRequest *)request {
} else {
request.requestTask = [self dataTaskWithHTTPMethod:@"GET" requestSerializer:requestSerializer URLString:url parameters:param];
}
} else if (method == YTKRequestMethodPost) {
} else if (method == YTKRequestMethodPOST) {
if (constructingBlock != nil) {
NSError *serializationError = nil;
NSMutableURLRequest *urlRequest = [requestSerializer multipartFormRequestWithMethod:@"POST" URLString:url parameters:param constructingBodyWithBlock:constructingBlock error:&serializationError];
Expand All @@ -252,13 +252,13 @@ - (void)addRequest:(YTKBaseRequest *)request {
} else {
request.requestTask = [self dataTaskWithHTTPMethod:@"POST" requestSerializer:requestSerializer URLString:url parameters:param];
}
} else if (method == YTKRequestMethodHead) {
} else if (method == YTKRequestMethodHEAD) {
request.requestTask = [self dataTaskWithHTTPMethod:@"HEAD" requestSerializer:requestSerializer URLString:url parameters:param];
} else if (method == YTKRequestMethodPut) {
} else if (method == YTKRequestMethodPUT) {
request.requestTask = [self dataTaskWithHTTPMethod:@"PUT" requestSerializer:requestSerializer URLString:url parameters:param];
} else if (method == YTKRequestMethodDelete) {
} else if (method == YTKRequestMethodDELETE) {
request.requestTask = [self dataTaskWithHTTPMethod:@"DELETE" requestSerializer:requestSerializer URLString:url parameters:param];
} else if (method == YTKRequestMethodPatch) {
} else if (method == YTKRequestMethodPATCH) {
request.requestTask = [self dataTaskWithHTTPMethod:@"PATCH" requestSerializer:requestSerializer URLString:url parameters:param];
} else {
YTKLog(@"Error, unsupport method type");
Expand Down
2 changes: 1 addition & 1 deletion YTKNetwork/YTKNetworkConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)new NS_UNAVAILABLE;

/// Return a shared config object.
+ (YTKNetworkConfig *)sharedInstance;
+ (YTKNetworkConfig *)sharedConfig;

/// Request base URL, such as "http://www.yuantiku.com". Default is empty string.
@property (strong, nonatomic) NSString *baseUrl;
Expand Down
2 changes: 1 addition & 1 deletion YTKNetwork/YTKNetworkConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ @implementation YTKNetworkConfig {
NSMutableArray<id<YTKCacheDirPathFilterProtocol>> *_cacheDirPathFilters;
}

+ (YTKNetworkConfig *)sharedInstance {
+ (YTKNetworkConfig *)sharedConfig {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Expand Down
4 changes: 2 additions & 2 deletions YTKNetwork/YTKRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ - (NSString *)cacheBasePath {
NSString *path = [pathOfLibrary stringByAppendingPathComponent:@"LazyRequestCache"];

// Filter cache base path
NSArray<id<YTKCacheDirPathFilterProtocol>> *filters = [[YTKNetworkConfig sharedInstance] cacheDirPathFilters];
NSArray<id<YTKCacheDirPathFilterProtocol>> *filters = [[YTKNetworkConfig sharedConfig] cacheDirPathFilters];
if (filters.count > 0) {
for (id<YTKCacheDirPathFilterProtocol> f in filters) {
path = [f filterCacheDirPath:path withRequest:self];
Expand All @@ -423,7 +423,7 @@ - (NSString *)cacheBasePath {

- (NSString *)cacheFileName {
NSString *requestUrl = [self requestUrl];
NSString *baseUrl = [YTKNetworkConfig sharedInstance].baseUrl;
NSString *baseUrl = [YTKNetworkConfig sharedConfig].baseUrl;
id argument = [self cacheFileNameFilterForRequestArgument:[self requestArgument]];
NSString *requestInfo = [NSString stringWithFormat:@"Method:%ld Host:%@ Url:%@ Argument:%@",
(long)[self requestMethod], baseUrl, requestUrl, argument];
Expand Down
2 changes: 1 addition & 1 deletion YTKNetworkTests/YTKBasicHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ @interface YTKBasicHTTPRequest ()
@implementation YTKBasicHTTPRequest

- (instancetype)initWithRequestUrl:(NSString *)url {
return [self initWithRequestUrl:url method:YTKRequestMethodGet];
return [self initWithRequestUrl:url method:YTKRequestMethodGET];
}

- (instancetype)initWithRequestUrl:(NSString *)url method:(YTKRequestMethod)method {
Expand Down
2 changes: 1 addition & 1 deletion YTKNetworkTests/YTKCacheTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ - (void)testCacheDirFilter {
NSString *const pathSuffix = @"MyCachePath";
// Add filter.
YTKBasicCacheDirFilter *filter = [YTKBasicCacheDirFilter filterWithPathComponent:pathSuffix];
[[YTKNetworkConfig sharedInstance] addCacheDirPathFilter:filter];
[[YTKNetworkConfig sharedConfig] addCacheDirPathFilter:filter];

// Test caching logic.
YTKCustomCacheRequest *req = [[YTKCustomCacheRequest alloc] initWithRequestUrl:@"get" cacheTimeInSeconds:5 cacheVersion:0 cacheSensitiveData:nil];
Expand Down
12 changes: 6 additions & 6 deletions YTKNetworkTests/YTKNetworkRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ - (void)tearDown {
}

- (void)testBasicHTTPRequest {
YTKBasicHTTPRequest *get = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"get" method:YTKRequestMethodGet];
YTKBasicHTTPRequest *get = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"get" method:YTKRequestMethodGET];
[self expectSuccess:get];

YTKBasicHTTPRequest *post = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"post" method:YTKRequestMethodPost];
YTKBasicHTTPRequest *post = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"post" method:YTKRequestMethodPOST];
[self expectSuccess:post];

YTKBasicHTTPRequest *patch = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"patch" method:YTKRequestMethodPatch];
YTKBasicHTTPRequest *patch = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"patch" method:YTKRequestMethodPATCH];
[self expectSuccess:patch];

YTKBasicHTTPRequest *put = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"put" method:YTKRequestMethodPut];
YTKBasicHTTPRequest *put = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"put" method:YTKRequestMethodPUT];
[self expectSuccess:put];

YTKBasicHTTPRequest *delete = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"delete" method:YTKRequestMethodDelete];
YTKBasicHTTPRequest *delete = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"delete" method:YTKRequestMethodDELETE];
[self expectSuccess:delete];

YTKBasicHTTPRequest *fail404 = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"status/404" method:YTKRequestMethodGet];
YTKBasicHTTPRequest *fail404 = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"status/404" method:YTKRequestMethodGET];
[self expectFailure:fail404];
}

Expand Down
2 changes: 1 addition & 1 deletion YTKNetworkTests/YTKRequestFilterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ - (void)tearDown {

- (void)testBasicFilter {
YTKBasicUrlFilter *filter = [YTKBasicUrlFilter filterWithArguments:@{@"key": @"value"}];
[[YTKNetworkConfig sharedInstance] addUrlFilter:filter];
[[YTKNetworkConfig sharedConfig] addUrlFilter:filter];

YTKBasicHTTPRequest *req = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"get"];
[self expectSuccess:req withAssertion:^(YTKBaseRequest *request) {
Expand Down
16 changes: 8 additions & 8 deletions YTKNetworkTests/YTKResumableDownloadTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ @implementation YTKResumableDownloadTests
- (void)setUp {
[super setUp];
[self createDirectory:[self saveBasePath]];
[self clearDirectory:[[YTKNetworkAgent sharedInstance] incompleteDownloadTempCacheFolder]];
[self clearDirectory:[[YTKNetworkAgent sharedAgent] incompleteDownloadTempCacheFolder]];

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
// Ignore cache
config.requestCachePolicy = NSURLRequestReloadIgnoringCacheData;
config.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever;
// Force download failed because of timeout.
config.timeoutIntervalForResource = 1;
[[YTKNetworkAgent sharedInstance] resetURLSessionManagerWithConfiguration:config];
[[YTKNetworkAgent sharedAgent] resetURLSessionManagerWithConfiguration:config];
// Allow all content type
[[YTKNetworkAgent sharedInstance] manager].responseSerializer.acceptableContentTypes = nil;
[[YTKNetworkAgent sharedAgent] manager].responseSerializer.acceptableContentTypes = nil;
}

- (void)tearDown {
[super tearDown];
[self clearDirectory:[self saveBasePath]];
[self clearDirectory:[[YTKNetworkAgent sharedInstance] incompleteDownloadTempCacheFolder]];
[self clearDirectory:[[YTKNetworkAgent sharedAgent] incompleteDownloadTempCacheFolder]];
}

- (NSString *)saveBasePath {
Expand All @@ -61,9 +61,9 @@ - (void)testResumableDownloadWithFullPath {
[self expectFailure:req];

// Start the request again
[[YTKNetworkAgent sharedInstance] resetURLSessionManager];
[[YTKNetworkAgent sharedAgent] resetURLSessionManager];
// Allow all content type
[[YTKNetworkAgent sharedInstance] manager].responseSerializer.acceptableContentTypes = nil;
[[YTKNetworkAgent sharedAgent] manager].responseSerializer.acceptableContentTypes = nil;

YTKDownloadRequest *req2 = [[YTKDownloadRequest alloc] initWithTimeout:self.networkTimeout requestUrl:kTestDownloadURL];
req2.resumableDownloadPath = [[self saveBasePath] stringByAppendingPathComponent:@"downloaded.bin"];
Expand All @@ -84,8 +84,8 @@ - (void)testResumableDownloadWithDirectoryPath {

[self expectFailure:req];

[[YTKNetworkAgent sharedInstance] resetURLSessionManager];
[[YTKNetworkAgent sharedInstance] manager].responseSerializer.acceptableContentTypes = nil;
[[YTKNetworkAgent sharedAgent] resetURLSessionManager];
[[YTKNetworkAgent sharedAgent] manager].responseSerializer.acceptableContentTypes = nil;

YTKDownloadRequest *req2 = [[YTKDownloadRequest alloc] initWithTimeout:self.networkTimeout requestUrl:kTestDownloadURL];
req2.resumableDownloadPath = [self saveBasePath];
Expand Down
Loading

0 comments on commit 9436033

Please sign in to comment.