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

ADD: custom request validator #316

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions YTKNetwork/YTKBaseRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions YTKNetwork/YTKBaseRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ - (BOOL)statusCodeValidator {
return (statusCode >= 200 && statusCode <= 299);
}

- (BOOL)customValidatorWithError:(NSError * _Nullable __autoreleasing *)error {
return YES;
}

#pragma mark - NSObject

- (NSString *)description {
Expand Down
6 changes: 6 additions & 0 deletions YTKNetwork/YTKNetworkAgent.m
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ - (BOOL)validateResult:(YTKBaseRequest *)request error:(NSError * _Nullable __au
}
return result;
}

result = [request customValidatorWithError:error];
if (!result) {
return result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要将错误传递出去

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

custom error 和 statusCode error、json error 一样会传递给 requestDidFailWithRequest:error:,只是由使用者自己初始化 error。

}

id json = [request responseJSONObject];
id validator = [request jsonValidator];
if (json && validator) {
Expand Down