handleResponse<T> method
Future<Result<T> >
handleResponse<T>(
- HubbleHTTPResponse res, {
- required SuccessConverter success,
Implementation
Future<Result<T>> handleResponse<T>(HubbleHTTPResponse res,
{required SuccessConverter success}) async {
final options = res.request.options;
if (buildConfig.isInspectorEnabled) {
appAnalytics()
.fireEvent(InspectorHTTPResponse(res: res, success: success));
}
if (res.statusCode >= 200 && res.statusCode <= 299) {
if (res.statusCode == 204 || res.body == '') {
return Success(await success(null));
}
final json = options.isProtobuf
? res.body
: options.parseJson
? (res.body != null ? jsonDecode(res.body) : {})
: res.body;
final response = await success(json);
return Success(response);
} else if (res.statusCode >= 400 && res.statusCode <= 499) {
if (res.statusCode == 401) {
appAnalytics().userLogout(
reason: 'api_401',
api: res.request.url,
);
logout();
}
late final Map<String, dynamic> jsonBody;
try {
jsonBody = json.decode(
options.isProtobuf ? String.fromCharCodes(res.body) : res.body);
} catch (_) {
if (kDebugMode) {
Logger.log(_);
}
jsonBody = {};
}
final data = APIErrors.fromJson(jsonBody);
return ValidationError(data);
} else {
final jsonBody = json.decode(
options.isProtobuf ? String.fromCharCodes(res.body) : res.body);
final data = APIErrors.fromJson(jsonBody);
return ServerError(data);
}
}