request method
ServiceResponseHandler callbacks will be used to handle the different possible responses from the request
Implementation
@override
Future<Either<ServiceFailure, S>> request({R? requestModel}) async {
if (await Locator().connectivity.getConnectivityStatus() ==
ConnectivityStatus.offline) {
Locator().logger.debug('JsonService response no connectivity error');
return Left(NoConnectivityServiceFailure());
}
Map<String, dynamic> requestJson = const {};
if (requestModel != null) {
requestJson = requestModel.toJson();
if (!isRequestModelJsonValid(requestJson)) {
Locator().logger.debug('JsonService response invalid request error');
return Left(GeneralServiceFailure());
}
}
final response = await _restApi.request(
method: _method, path: _path, requestBody: requestJson);
if (response.type == RestResponseType.timeOut) {
Locator().logger.debug('JsonService response no connectivity error');
return Left(NoConnectivityServiceFailure());
}
S model;
try {
final content = response.content.toString();
final Map<String, dynamic> jsonResponse =
(content.isEmpty) ? {} : json.decode(content) ?? <String, dynamic>{};
if (response.type != RestResponseType.success) {
return Left(onError(response.type, jsonResponse));
}
model = parseResponse(jsonResponse);
return Right(model);
} on Exception catch (e) {
// Errors should not be handled. The app should fail since it a developer
// mistake and should be fixed ASAP. Exceptions are not the fault of
// any developer, so we should log them to help us find quickly on
// production logs the problem.
Locator()
.logger
.error('JsonService response parse exception', e.toString());
}
return Left(GeneralServiceFailure());
}