Reliability/PoC hardening: dedupe uploads by file_hash, surface editor sync failures instead of a false success SnackBar with a retry-without-re-OCR path, bound the OCR pipeline fetches with timeouts, share a single ApiClient/Dio instance app-wide, tune capture JPEG quality, and add an opt-in docker-compose.demo.yml for a production-mode run ahead of client demos. Translate all Flutter-side user-facing text (screens, validators, SnackBars, the printed delivery receipt, and shared API error messages) to Bahasa Indonesia. Also includes in-progress OCR parser/accuracy-tuning work from the same session: table column/unit normalization fixes, store/customer master data, accuracy history log, and test-image renaming/cleanup. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017eRAsLqN9Sg1b9YPz22Lxz
90 lines
3.0 KiB
Dart
90 lines
3.0 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../../config/app_config.dart';
|
|
|
|
class ApiClient {
|
|
late final Dio _dio;
|
|
|
|
ApiClient() {
|
|
_dio = Dio(
|
|
BaseOptions(
|
|
baseUrl: AppConfig.apiBaseUrl,
|
|
connectTimeout: const Duration(seconds: 10),
|
|
receiveTimeout: const Duration(minutes: 2),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
},
|
|
),
|
|
);
|
|
|
|
_setupInterceptors();
|
|
}
|
|
|
|
void _setupInterceptors() {
|
|
// Request interceptor to append JWT token
|
|
_dio.interceptors.add(InterceptorsWrapper(
|
|
onRequest: (options, handler) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final token = prefs.getString('auth_token');
|
|
if (token != null) {
|
|
options.headers['Authorization'] = 'Bearer $token';
|
|
}
|
|
return handler.next(options);
|
|
},
|
|
));
|
|
|
|
// Logging interceptor for debugging
|
|
_dio.interceptors.add(LogInterceptor(
|
|
requestBody: true,
|
|
responseBody: true,
|
|
));
|
|
|
|
// Standardized Error Interceptor
|
|
_dio.interceptors.add(InterceptorsWrapper(
|
|
onError: (DioException e, handler) {
|
|
String errorMessage = 'Terjadi kesalahan yang tidak terduga';
|
|
if (e.type == DioExceptionType.connectionTimeout ||
|
|
e.type == DioExceptionType.receiveTimeout) {
|
|
errorMessage = 'Koneksi timeout. Silakan coba lagi.';
|
|
} else if (e.response != null) {
|
|
// Standardized HTTP error formatting
|
|
final statusCode = e.response?.statusCode;
|
|
String serverMsg = 'Tidak diketahui';
|
|
final responseData = e.response?.data;
|
|
if (responseData is Map) {
|
|
serverMsg = responseData['message'] ?? 'Tidak diketahui';
|
|
} else if (responseData is String) {
|
|
// Truncate raw string if it's a long HTML page
|
|
serverMsg = responseData.length > 100
|
|
? '${responseData.substring(0, 100)}...'
|
|
: responseData;
|
|
}
|
|
errorMessage = 'Kesalahan server ($statusCode): $serverMsg';
|
|
} else if (e.type == DioExceptionType.connectionError) {
|
|
errorMessage = 'Tidak ada koneksi internet.';
|
|
}
|
|
|
|
final customError = DioException(
|
|
requestOptions: e.requestOptions,
|
|
response: e.response,
|
|
type: e.type,
|
|
error: errorMessage,
|
|
);
|
|
return handler.next(customError);
|
|
},
|
|
));
|
|
|
|
// Dummy Response Interceptor for Development
|
|
// TODO: Remove this interceptor before production release
|
|
// _dio.interceptors.add(DummyResponseInterceptor());
|
|
}
|
|
|
|
Dio get client => _dio;
|
|
}
|
|
|
|
// Single shared instance so every screen/notifier reuses the same Dio HTTP client
|
|
// (connection keep-alive) instead of paying a fresh TCP/TLS handshake per call site.
|
|
final apiClientProvider = Provider<ApiClient>((ref) => ApiClient());
|