2023-01-17 07:52:31 +00:00
|
|
|
import 'dart:async';
|
2023-01-19 05:21:12 +00:00
|
|
|
import 'dart:convert';
|
2023-01-17 07:52:31 +00:00
|
|
|
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;
|
2023-06-06 06:54:51 +00:00
|
|
|
int requestTimeout = 25;
|
2023-01-17 07:52:31 +00:00
|
|
|
String host = Url.instance.host();
|
|
|
|
|
|
|
|
Future<Response> getRequest(
|
|
|
|
{String? path,
|
|
|
|
Map<String, String>? headers,
|
|
|
|
Map<String, String>? param}) async {
|
|
|
|
Response response;
|
|
|
|
try {
|
2023-08-22 06:22:08 +00:00
|
|
|
response = await get(Uri.http(host, path!, param), headers: headers)
|
2023-01-17 07:52:31 +00:00
|
|
|
.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<Response> postRequest(
|
|
|
|
{String? path,
|
|
|
|
Map<String, String>? headers,
|
|
|
|
Map? body,
|
|
|
|
Map<String, String>? param}) async {
|
|
|
|
Response response;
|
2023-01-25 01:18:30 +00:00
|
|
|
try {
|
2023-08-22 06:22:08 +00:00
|
|
|
response = await post(Uri.http(host, path!, param),
|
2023-02-01 08:03:05 +00:00
|
|
|
headers: headers, body: jsonEncode(body))
|
2023-01-17 07:52:31 +00:00
|
|
|
.timeout(Duration(seconds: requestTimeout));
|
2023-01-25 01:18:30 +00:00
|
|
|
} 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());
|
|
|
|
}
|
2023-01-17 07:52:31 +00:00
|
|
|
return response;
|
|
|
|
}
|
2023-02-23 00:53:14 +00:00
|
|
|
|
2023-03-02 00:40:47 +00:00
|
|
|
Future<Response> putRequest(
|
|
|
|
{required String path,
|
|
|
|
required Map<String, String>? headers,
|
|
|
|
required Map? body,
|
|
|
|
required Map<String, dynamic>? param}) async {
|
|
|
|
Response response;
|
|
|
|
try {
|
2023-08-22 06:22:08 +00:00
|
|
|
response =await put(Uri.http(host,path,param),headers: headers,body: jsonEncode(body));
|
2023-03-02 00:40:47 +00:00
|
|
|
} 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;
|
|
|
|
}
|
2023-05-02 08:42:15 +00:00
|
|
|
Future<Response> patch(
|
|
|
|
{required String path,
|
|
|
|
required Map<String, String>? headers,
|
|
|
|
required Map? body,
|
|
|
|
required Map<String, dynamic>? 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;
|
|
|
|
}
|
|
|
|
|
2023-03-02 00:40:47 +00:00
|
|
|
|
2023-02-23 00:53:14 +00:00
|
|
|
Future<Response> deleteRequest(
|
|
|
|
{required String path,
|
|
|
|
required Map<String, String>? headers,
|
|
|
|
required Map? body,
|
|
|
|
required Map<String, dynamic>? param}) async {
|
|
|
|
Response response;
|
2023-03-02 00:40:47 +00:00
|
|
|
try {
|
2023-08-22 06:22:08 +00:00
|
|
|
response = await delete(Uri.http(host, path, param),
|
2023-02-23 00:53:14 +00:00
|
|
|
headers: headers, body: jsonEncode(body))
|
|
|
|
.timeout(Duration(seconds: requestTimeout));
|
2023-03-02 00:40:47 +00:00
|
|
|
} 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());
|
|
|
|
}
|
2023-02-23 00:53:14 +00:00
|
|
|
return response;
|
|
|
|
}
|
2023-01-17 07:52:31 +00:00
|
|
|
}
|