176 lines
4.2 KiB
Dart
176 lines
4.2 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer';
|
|
import 'dart:io';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class RequestHandler {
|
|
static late String token;
|
|
|
|
String? errorMessage;
|
|
|
|
dynamic body;
|
|
final Map<String, String> _headers = {
|
|
"accept": "*/*",
|
|
"Content-Type": "application/json; charset=UTF-8",
|
|
};
|
|
final String url;
|
|
dynamic _requestBody;
|
|
|
|
bool isSuccess = false;
|
|
|
|
RequestHandler(
|
|
this.url, {
|
|
Map<String, String>? requestHeaders,
|
|
body,
|
|
bool useAutherization = true,
|
|
}) {
|
|
if (body != null) _requestBody = body;
|
|
if (requestHeaders != null) _headers.addAll(requestHeaders);
|
|
if (useAutherization) _headers.addAll({'Authorization': 'Bearer $token'});
|
|
if (body != null) _requestBody = body;
|
|
}
|
|
|
|
Future<void> httpGet() async {
|
|
try {
|
|
final response = await http
|
|
.get(
|
|
Uri.parse(url),
|
|
headers: _headers,
|
|
)
|
|
.timeout(
|
|
const Duration(seconds: 10),
|
|
)
|
|
.catchError(
|
|
(e) => throw e,
|
|
);
|
|
_handleResponse(response);
|
|
} catch (e) {
|
|
_handleError(null);
|
|
}
|
|
}
|
|
|
|
Future<void> post() async {
|
|
try {
|
|
final response = await http
|
|
.post(
|
|
Uri.parse(url),
|
|
body: json.encode(_requestBody),
|
|
headers: _headers,
|
|
)
|
|
.timeout(
|
|
const Duration(seconds: 10),
|
|
)
|
|
.catchError(
|
|
(e) => throw e,
|
|
);
|
|
_handleResponse(response);
|
|
} catch (e) {
|
|
_handleError(null);
|
|
}
|
|
}
|
|
|
|
Future<void> delete() async {
|
|
try {
|
|
final response = await http
|
|
.delete(
|
|
Uri.parse(url),
|
|
body: json.encode(_requestBody),
|
|
headers: _headers,
|
|
)
|
|
.timeout(
|
|
const Duration(seconds: 10),
|
|
)
|
|
.catchError(
|
|
(e) => throw e,
|
|
);
|
|
_handleResponse(response);
|
|
} catch (e) {
|
|
_handleError(null);
|
|
}
|
|
}
|
|
|
|
Future<void> multipart(File file) async {
|
|
try {
|
|
final request = http.MultipartRequest('POST', Uri.parse(url));
|
|
_headers.update('Content-Type', (_) => 'multipart/form-data');
|
|
request.headers.addAll(_headers);
|
|
request.files.add(
|
|
http.MultipartFile(
|
|
'file',
|
|
file.readAsBytes().asStream(),
|
|
file.lengthSync(),
|
|
filename: 'desc',
|
|
),
|
|
);
|
|
final streamedResponse = await request
|
|
.send()
|
|
.timeout(
|
|
const Duration(seconds: 10),
|
|
)
|
|
.catchError(
|
|
(e) => throw e,
|
|
);
|
|
final response = await http.Response.fromStream(streamedResponse);
|
|
_handleResponse(response);
|
|
} catch (e) {
|
|
_handleError(null);
|
|
}
|
|
}
|
|
|
|
Future<void> put() async {
|
|
try {
|
|
final response = await http
|
|
.put(
|
|
Uri.parse(url),
|
|
body: json.encode(_requestBody),
|
|
headers: _headers,
|
|
)
|
|
.timeout(
|
|
const Duration(seconds: 10),
|
|
)
|
|
.catchError(
|
|
(e) => throw e,
|
|
);
|
|
_handleResponse(response);
|
|
} catch (e) {
|
|
_handleError(null);
|
|
}
|
|
}
|
|
|
|
void _handleResponse(http.Response? response) {
|
|
if (_handleError(response)) {
|
|
if (response!.body.isNotEmpty) {
|
|
body = json.decode(response.body);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool _handleError(http.Response? response) {
|
|
if (response == null) {
|
|
log(
|
|
'Connecion failed or request timedout! ("$url")',
|
|
name: 'Request fail error',
|
|
);
|
|
isSuccess = false;
|
|
errorMessage = _errorMessageGenerator(response);
|
|
return false;
|
|
}
|
|
if (response.statusCode != 200 && response.statusCode != 204) {
|
|
log(
|
|
'Request to [$url] failed with status code ${response.statusCode} & result : \n ${response.body.isNotEmpty ? json.decode(response.body) : 'No result.'}',
|
|
name: 'Request fail error',
|
|
);
|
|
isSuccess = false;
|
|
errorMessage = _errorMessageGenerator(response);
|
|
return false;
|
|
}
|
|
isSuccess = true;
|
|
return true;
|
|
}
|
|
|
|
String _errorMessageGenerator(http.Response? response) {
|
|
//TODO: Needs implementation
|
|
return '';
|
|
}
|
|
}
|