feat: consolidate backend and docker-compose setup
This commit is contained in:
commit
ff3753a745
306 files changed
+35450
No files matched your search
@@ -0,0 +1,74 @@
|
||||
import 'package:dio/dio.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 = 'An unexpected error occurred';
|
||||
if (e.type == DioExceptionType.connectionTimeout ||
|
||||
e.type == DioExceptionType.receiveTimeout) {
|
||||
errorMessage = 'Connection timed out. Please try again.';
|
||||
} else if (e.response != null) {
|
||||
// Standardized HTTP error formatting
|
||||
final statusCode = e.response?.statusCode;
|
||||
errorMessage = 'Server error ($statusCode): ${e.response?.data['message'] ?? 'Unknown'}';
|
||||
} else if (e.type == DioExceptionType.connectionError) {
|
||||
errorMessage = 'No internet connection.';
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// Interceptor to simulate a 5-second delayed OCR processing response.
|
||||
/// This will only intercept POST requests to the upload document endpoint.
|
||||
class DummyResponseInterceptor extends Interceptor {
|
||||
@override
|
||||
Future<void> onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
|
||||
// Check if it's the upload document endpoint
|
||||
if (options.path.contains('/documents/upload')) {
|
||||
debugPrint('Simulating 5-second processing delay for OCR...');
|
||||
|
||||
// Simulate 5 seconds delay
|
||||
await Future.delayed(const Duration(seconds: 5));
|
||||
|
||||
// Create a dummy successful response based on user schema requirements
|
||||
final dummyData = {
|
||||
'status': 'success',
|
||||
'message': 'Document processed successfully',
|
||||
'data': {
|
||||
'header': {
|
||||
'tanggal': '16 April 2026',
|
||||
'no_po': 'PO/26/0000140731',
|
||||
'no_so': '1691869400',
|
||||
'no_do': '1659896184'
|
||||
},
|
||||
'shipment': {
|
||||
'kepada_yth': 'PT.PRIMAFOOD INTERNATIONAL DKI AREA',
|
||||
'order_untuk': '',
|
||||
'alamat': 'JL. ANCOL BARAT VIII/1, ANCOL, PADEMANGAN, JAKARTA UTARA, 14430',
|
||||
'plat_truk': 'B 9310 BXT'
|
||||
},
|
||||
'items': [
|
||||
{
|
||||
'nomor_sku': '12080101',
|
||||
'nama_barang': '920 FIESTA SPICY CHICK 400 G',
|
||||
'banyak': '1 BOX',
|
||||
'jumlah': '12 PAC'
|
||||
},
|
||||
{
|
||||
'nomor_sku': '13010513',
|
||||
'nama_barang': '304 Champ Chicken Sausage 375 Gr 15 Pack',
|
||||
'banyak': '1 BOX',
|
||||
'jumlah': '15 PAC'
|
||||
},
|
||||
{
|
||||
'nomor_sku': '13030502',
|
||||
'nama_barang': '298 Champ Chicken Ball 500 GR Fr',
|
||||
'banyak': '1 BOX',
|
||||
'jumlah': '10 PAC'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
debugPrint('Dummy response generated');
|
||||
|
||||
return handler.resolve(
|
||||
Response(
|
||||
requestOptions: options,
|
||||
data: dummyData,
|
||||
statusCode: 200,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// For other requests, continue normally
|
||||
super.onRequest(options, handler);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user