import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:http/http.dart'; import 'package:unit2/utils/text_container.dart'; import 'package:unit2/utils/urls.dart'; class Request { static final Request _instance = Request(); static Request get instance => _instance; int requestTimeout = 25; String host = Url.instance.host(); Future getRequest( {String? path, Map? headers, Map? param}) async { Response response; try { response = await get(Uri.http(host, path!, param), headers: headers) .timeout(Duration(seconds: requestTimeout)); } on TimeoutException catch (_) { Fluttertoast.showToast( msg: timeoutError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (timeoutError); } on SocketException catch (_) { Fluttertoast.showToast( msg: timeoutError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (timeoutError); } on FormatException catch (_) { throw const FormatException(formatError); } on HttpException catch (_) { throw const HttpException(httpError); } on Error catch (e) { debugPrint("get request error: $e"); Fluttertoast.showToast( msg: onError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (onError); } return response; } Future postRequest( {String? path, Map? headers, Map? body, Map? param}) async { Response response; try { response = await post(Uri.http(host, path!, param), headers: headers, body: jsonEncode(body)) .timeout(Duration(seconds: requestTimeout)); } on TimeoutException catch (_) { Fluttertoast.showToast( msg: timeoutError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (timeoutError); } on SocketException catch (_) { Fluttertoast.showToast( msg: timeoutError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (timeoutError); } on FormatException catch (_) { throw const FormatException(formatError); } on HttpException catch (_) { throw const HttpException(httpError); } on Error catch (e) { debugPrint("post request error: $e"); Fluttertoast.showToast( msg: onError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (e.toString()); } return response; } Future putRequest( {required String path, required Map? headers, required Map? body, required Map? param}) async { Response response; try { response =await put(Uri.http(host,path,param),headers: headers,body: jsonEncode(body)); } on TimeoutException catch (_) { Fluttertoast.showToast( msg: timeoutError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (timeoutError); } on SocketException catch (_) { Fluttertoast.showToast( msg: timeoutError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (timeoutError); } on FormatException catch (_) { throw const FormatException(formatError); } on HttpException catch (_) { throw const HttpException(httpError); } on Error catch (e) { debugPrint("post request error: $e"); Fluttertoast.showToast( msg: onError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (e.toString()); } return response; } Future patch( {required String path, required Map? headers, required Map? body, required Map? param}) async { Response response; try { response =await patch(path: host+path, headers: headers,body: body,param: param); } on TimeoutException catch (_) { Fluttertoast.showToast( msg: timeoutError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (timeoutError); } on SocketException catch (_) { Fluttertoast.showToast( msg: timeoutError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (timeoutError); } on FormatException catch (_) { throw const FormatException(formatError); } on HttpException catch (_) { throw const HttpException(httpError); } on Error catch (e) { debugPrint("post request error: $e"); Fluttertoast.showToast( msg: onError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (e.toString()); } return response; } Future deleteRequest( {required String path, required Map? headers, required Map? body, required Map? param}) async { Response response; try { response = await delete(Uri.http(host, path, param), headers: headers, body: jsonEncode(body)) .timeout(Duration(seconds: requestTimeout)); } on TimeoutException catch (_) { Fluttertoast.showToast( msg: timeoutError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (timeoutError); } on SocketException catch (_) { Fluttertoast.showToast( msg: timeoutError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (timeoutError); } on FormatException catch (_) { throw const FormatException(formatError); } on HttpException catch (_) { throw const HttpException(httpError); } on Error catch (e) { Fluttertoast.showToast( msg: onError, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, backgroundColor: Colors.black, ); throw (e.toString()); } return response; } }