Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a #414

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions YTKNetwork/YTKBaseRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -332,6 +334,12 @@ typedef void(^YTKRequestCompletionBlock)(__kindof YTKBaseRequest *request);
/// This validator will be used to test if `responseStatusCode` is valid.
- (BOOL)statusCodeValidator;

- (id)jsonToModelWithData:(NSDictionary *)dataDict;

-(void)requestStart;

-(void)requestFinish;

@end

NS_ASSUME_NONNULL_END
13 changes: 13 additions & 0 deletions YTKNetwork/YTKBaseRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -202,6 +203,18 @@ - (BOOL)statusCodeValidator {
return (statusCode >= 200 && statusCode <= 299);
}

- (id)jsonToModelWithData:(NSDictionary *)dataDict
{
return nil;
}

-(void)requestStart
{
}

-(void)requestFinish
{
}
#pragma mark - NSObject

- (NSString *)description {
Expand Down
47 changes: 44 additions & 3 deletions YTKNetwork/YTKNetworkAgent.m
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ - (void)addRequest:(YTKBaseRequest *)request {
// Retain request
YTKLog(@"Add request: %@", NSStringFromClass([request class]));
[self addRequestToRecord:request];

[request requestStart];

[request.requestTask resume];
}

Expand Down Expand Up @@ -273,6 +276,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) {
Expand All @@ -291,6 +303,33 @@ - (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)
{
if ([request respondsToSelector:@selector(jsonToModelWithData:)])
{
request.responseModel = [request jsonToModelWithData:dataDict];
}
}
else
{
if ([request respondsToSelector:@selector(jsonToModelWithData:)])
{
request.responseModel = [request jsonToModelWithData:request.responseJSONObject];
}
}
}else
{
if ([request respondsToSelector:@selector(jsonToModelWithData:)])
{
request.responseModel = [request jsonToModelWithData:request.responseJSONObject];
}
}


return YES;
}

Expand All @@ -309,7 +348,7 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response
}

YTKLog(@"Finished Request: %@", NSStringFromClass([request class]));

NSError * __autoreleasing serializationError = nil;
NSError * __autoreleasing validationError = nil;

Expand All @@ -334,18 +373,20 @@ - (void)handleRequestResult:(NSURLSessionTask *)task responseObject:(id)response
break;
}
}

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;
}

if (succeed) {
[request requestFinish];
[self requestDidSucceedWithRequest:request];
} else {
[self requestDidFailWithRequest:request error:requestError];
Expand Down
12 changes: 12 additions & 0 deletions YTKNetwork/YTKNetworkConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <NSObject>

- (BOOL)checkErrorCodeWithRequest:(YTKBaseRequest *)request;

- (NSDictionary *)extractDataWithRequest:(YTKBaseRequest *)request;

@end


/// YTKCacheDirPathFilterProtocol can be used to append common path components when caching response results
@protocol YTKCacheDirPathFilterProtocol <NSObject>
/// Preprocess cache path before actually saving them.
Expand Down Expand Up @@ -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<YTKResponseFilterProtocol> responseFilterProtocol;

/// Add a new URL filter.
- (void)addUrlFilter:(id<YTKUrlFilterProtocol>)filter;
/// Remove all URL filters.
Expand Down
2 changes: 2 additions & 0 deletions YTKNetwork/YTKNetworkConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions YTKNetwork/YTKNetworkPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down