D1APP-22 request service bug fixed

This commit is contained in:
MohammadTaha Basiri 2022-01-04 16:14:57 +03:30
parent 0ec05f1764
commit 9fcd2e0a16
1 changed files with 22 additions and 7 deletions

View File

@ -4,11 +4,16 @@ import 'dart:io';
import 'package:http/http.dart' as http;
class RequestService {
static late String token;
static late String _token;
static set token(value) => _token = value;
Map get result => _body['result'] ?? const {};
Map get errors => _body['errors'] ?? const {};
String? errorMessage;
dynamic body;
dynamic _body;
final Map<String, String> _headers = {
"accept": "*/*",
"Content-Type": "application/json; charset=UTF-8",
@ -26,7 +31,7 @@ class RequestService {
}) {
if (body != null) _requestBody = body;
if (requestHeaders != null) _headers.addAll(requestHeaders);
if (useAutherization) _headers.addAll({'Authorization': 'Bearer $token'});
if (useAutherization) _headers.addAll({'Authorization': 'Bearer $_token'});
if (body != null) _requestBody = body;
}
@ -61,7 +66,9 @@ class RequestService {
const Duration(seconds: 10),
)
.catchError(
(e) => throw e,
(e) {
throw e;
},
);
_handleResponse(response);
} catch (e) {
@ -140,7 +147,7 @@ class RequestService {
void _handleResponse(http.Response? response) {
if (_handleError(response)) {
if (response!.body.isNotEmpty) {
body = json.decode(response.body);
_body = json.decode(response.body);
}
}
}
@ -156,8 +163,16 @@ class RequestService {
return false;
}
if (response.statusCode != 200 && response.statusCode != 204) {
String data;
if (response.body.isEmpty) {
data = 'No results!';
} else if (response.body.contains('<!DOCTYPE html>')) {
data = response.body;
} else {
data = jsonDecode(response.body);
}
log(
'Request to [$url] failed with status code ${response.statusCode} & result : \n ${response.body.isNotEmpty ? json.decode(response.body) : 'No result.'}',
'Request to [$url] failed with status code ${response.statusCode} & result : \n $data',
name: 'Request fail error',
);
isSuccess = false;