mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Split oott_api.dart into per-domain modules
Break the 372-line frontend API client into focused files under lib/utils/api/: error mapping (api_error.dart), Dio setup behind a buildDio factory (dio_config.dart), and device/scanner/notification endpoints as extensions in part files. Collapse the five near-identical scanner-status methods via a generic _getModel helper and de-duplicate the pagination logic via a shared _paginate helper. The BackendAPI singleton and dioErrorToUserMessage remain importable from oott_api.dart unchanged, so no call sites are affected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
5f7a1287a1
commit
c9e79e028f
@@ -0,0 +1,40 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
String dioErrorToUserMessage(Object error) {
|
||||
if (error is TypeError || error is FormatException) {
|
||||
debugPrint('Backend response shape error: $error');
|
||||
return 'Unexpected response shape from backend.';
|
||||
}
|
||||
if (error is! DioException) {
|
||||
debugPrint('Non-Dio error from backend call: $error');
|
||||
return 'Unexpected error.';
|
||||
}
|
||||
switch (error.type) {
|
||||
case DioExceptionType.connectionTimeout:
|
||||
case DioExceptionType.sendTimeout:
|
||||
case DioExceptionType.receiveTimeout:
|
||||
return 'Backend did not respond in time.';
|
||||
case DioExceptionType.connectionError:
|
||||
return 'Cannot reach backend. Check the base URL and your network.';
|
||||
case DioExceptionType.badCertificate:
|
||||
return 'Backend TLS certificate could not be verified.';
|
||||
case DioExceptionType.cancel:
|
||||
return 'Request canceled.';
|
||||
case DioExceptionType.badResponse:
|
||||
final status = error.response?.statusCode;
|
||||
if (status == 401 || status == 403) {
|
||||
return 'Authentication failed. Check your API key in Settings.';
|
||||
}
|
||||
if (status == 404) {
|
||||
return 'Not found.';
|
||||
}
|
||||
if (status != null && status >= 500 && status < 600) {
|
||||
return 'Backend error (status $status). Please try again.';
|
||||
}
|
||||
return 'Unexpected response from backend (status ${status ?? 'unknown'}).';
|
||||
case DioExceptionType.unknown:
|
||||
debugPrint('Unknown Dio error: ${error.message}');
|
||||
return 'Unexpected error contacting backend.';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user