import 'package:bibilili/http/core/hi_adapter.dart';
import 'package:bibilili/http/core/hi_error.dart';
import 'package:bibilili/http/core/mock_adapter.dart';
import 'package:bibilili/http/request/base_request.dart';
class HiNet{
HiNet._();
static HiNet _instance;
static HiNet getInstance(){
_instance= HiNet._();
return _instance;
}
Future fire(BaseRequest request) async {
HiNetResponse response;
var error;
try{
response = await send(request);
} on HiNetError catch(e){
error = e;
response=e.data;
printLog(e.message);
} catch(e){
//其他异常
error = e;
printLog(e);
}
if(response==null){
printLog(error);
}
var result = response.data;
printLog(result);
var status = response.statusCode;
switch(status){
case 200:
return result;
case 401:
throw NeedLogin();
case 403:
throw NeedAuth(request.toString(),data: result);
default:
throw HiNetError(status, result.toString(),data:result);
}
}
Future<dynamic> send<T>(BaseRequest request) async {
printLog("url:${request.url()}");
///使用Mock发送请求
HiNetAdapter adapter=MockAdapter();
return adapter.send(request);
}
void printLog(log){
print("hi_net:${log.toString()}");
}
}
一次性掌握Flutter高阶技能+商业级复杂项目架构设计与开发方案
了解课程