typedef NS_ENUM(NSUInteger, AIFURLResponseStatus)
{
AIFURLResponseStatusSuccess, //作为底层,请求是否成功只考虑是否成功收到服务器反馈。至于签名是否正确,返回的数据是否完整,由上层的RTApiBaseManager来决定。
AIFURLResponseStatusErrorTimeout,
AIFURLResponseStatusErrorNoNetwork // 默认除了超时以外的错误都是无网络错误。
};
NSURLSessionDataTask *task = [self.sessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
NSURLSessionDataTask *storedTask = self.dispatchTable[requestId];
if (storedTask == nil) {
// 如果这个operation是被cancel的,那就不用处理回调了。
return;
}else{
[self.dispatchTable removeObjectForKey:requestId];
}
NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
if (!error) {
// success
[AIFLogger logDebugInfoWithResponse:(NSHTTPURLResponse*)response
resposeString:responseString
request:request
error:NULL];
AIFURLResponse *response = [[AIFURLResponse alloc] initWithResponseString:responseString
requestId:requestId
request:request
responseData:responseObject
status:AIFURLResponseStatusSuccess];
success?success(response):nil;
}else{
[AIFLogger logDebugInfoWithResponse:(NSHTTPURLResponse*)response
resposeString:responseString
request:request
error:error];
AIFURLResponse *response = [[AIFURLResponse alloc] initWithResponseString:responseString
requestId:requestId
request:request
responseData:responseObject
error:error];
fail?fail(response):nil;
}
}];
//作为底层,请求是否成功只考虑是否成功收到服务器反馈。至于签名是否正确,返回的数据是否完整,由上层的RTApiBaseManager来决定。