This is Objective-C and about Networking Request. Just set the AFNetWorking Manager and get method name from the method lists below (it is all Post request).
The way we take now is quite weird. If there is another method to add, just add the list. Shall I combine the list to one method? I will use Enum and Dictionary. Is there any better solution?
I know one method providing a request to handle every network request. It will inherit it and override some methods to define custom requests in your project. The main idea is use the Command Pattern.
@implementation SSOHttpRequestManager
/**
@param msg_type , method name , X-METHOD
@param version
@param dataDict request data
@param encrypt whether or not
@param success callback
@param failure callback
*/
+ (void)baseNetWorkType:(NSString *)msg_type
version:(NSString *)version
dataDict:(NSDictionary *)dataDict
isEncrypt:(BOOL)encrypt
success:(SuccessBlock)success
failure:(FailureBlock)failure
{
AFHTTPSessionManager *manager = [NetManager shareAFManager];
[manager.requestSerializer setValue:msg_type forHTTPHeaderField:@"X-METHOD"];
//设置请求体数据
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSString *dataStr ;//...
if (encrypt) { //... }
[dict setValue:dataStr forKey:@"data"];// @"version" @"digest"
[manager POST: K_SSO_URL parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *responseDict = (NSDictionary *)responseObject;
if([[responseDict valueForKey:@"code"] isEqual: @600]){
//.... [[AppManager sharedInstance] tokenInvalidQuit];
return;
}
NSString *responseMessage = [responseDict valueForKey:@"msg"];
NSString *statusCode = [responseDict valueForKey:@"code"];
NSString *responseData = [responseDict valueForKey:@"data"];
if ([statusCode isEqualToString: @"S200"]) {
success(responseDict,responseData,responseMessage);
}else{
failure(statusCode,responseMessage);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(@"-1",_NET_ERROR);
}];
}
#pragma mark judge network
+(BOOL)isConnectionAvailable{
BOOL isExistenceNetwork = YES;
//....
return isExistenceNetwork;
}
#pragma mark -- sendVerifyCode
+ (void)sendVerifyCodeParams:(NSDictionary *)params success:(SuccessBlock)success
failure:(FailureBlock)failure{
[SSOHttpRequestManager baseNetWorkType:@"sendVerifyCode" version:DEFALUT_VERSION dataDict:params isEncrypt:NO success:^(id response, id data, NSString *Message) {
success(response,data,Message);
} failure:^(NSString *statusCode, NSString *Message) {
failure(statusCode,Message);
}];
}
#pragma mark -- register
+ (void)registerParams:(NSDictionary *)params success:(SuccessBlock)success
failure:(FailureBlock)failure{
[SSOHttpRequestManager baseNetWorkType:@"register" version:DEFALUT_VERSION dataDict:params isEncrypt:YES success:^(id response, id data, NSString *Message) {
success(response,data,Message);
} failure:^(NSString *statusCode, NSString *Message) {
failure(statusCode,Message);
}];
}
#pragma mark -- login
+ (void)loginParams:(NSDictionary *)params success:(SuccessBlock)success
failure:(FailureBlock)failure{
[SSOHttpRequestManager baseNetWorkType:@"login" version:DEFALUT_VERSION dataDict:params isEncrypt:YES success:^(id response, id data, NSString *Message) {
success(response,data,Message);
} failure:^(NSString *statusCode, NSString *Message) {
failure(statusCode,Message);
}];
}
#pragma mark -- resetPassword
+ (void)findPasswordParams:(NSDictionary *)params success:(SuccessBlock)success
failure:(FailureBlock)failure{
[SSOHttpRequestManager baseNetWorkType:@"resetPassword" version:DEFALUT_VERSION dataDict:params isEncrypt:YES success:^(id response, id data, NSString *Message) {
success(response,data,Message);
} failure:^(NSString *statusCode, NSString *Message) {
failure(statusCode,Message);
}];
}
@end
1 Answer 1
- Don't pass 'params' in via a public method. What are these params? Make the method signature explicit.
So instead of
+ (void)loginParams:(NSDictionary *)params success:(SuccessBlock)success failure:(FailureBlock)failure
you have
+ (void)loginWithUsername:(NSString *)username password:(NSString *)password success:(SuccessBlock)success failure:(FailureBlock)failure
{
NSDictionary *params = @{ @"username" : username, @"password" : password };
Don't use success & failure blocks this just leads to unnecessary duplication to handle the fail states. Just use one completion block like
completion:^(BOOL success, NSError *error)completion
you can also add other return objects that make sense for the method.baseNetWorkType:version:dataDict:isEncrypt:success:failure
seems mostly unnecessary I would move most of the setup back into the individual methods or just get rid of it entirely.Don't put the class name in when you call class methods from inside the same class so
[SSOHttpRequestManager methodName
becomes just[self methodName
Explore related questions
See similar questions with these tags.