- API 클라이언트 및 인증 인터셉터 에러 처리 강화 - 의존성 주입 실패 시에도 앱 실행 가능하도록 개선 - 사용하지 않는 레거시 UI 컴포넌트 및 화면 제거 - pubspec.yaml 의존성 업데이트 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
257 lines
6.4 KiB
Dart
257 lines
6.4 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import '../../../core/config/environment.dart';
|
|
import 'interceptors/auth_interceptor.dart';
|
|
import 'interceptors/error_interceptor.dart';
|
|
import 'interceptors/logging_interceptor.dart';
|
|
|
|
/// API 클라이언트 클래스
|
|
class ApiClient {
|
|
late final Dio _dio;
|
|
|
|
static ApiClient? _instance;
|
|
|
|
factory ApiClient() {
|
|
_instance ??= ApiClient._internal();
|
|
return _instance!;
|
|
}
|
|
|
|
ApiClient._internal() {
|
|
try {
|
|
_dio = Dio(_baseOptions);
|
|
_setupInterceptors();
|
|
} catch (e) {
|
|
print('Error while creating ApiClient');
|
|
print('Stack trace:');
|
|
print(StackTrace.current);
|
|
// 기본값으로 초기화
|
|
_dio = Dio(BaseOptions(
|
|
baseUrl: 'http://localhost:8080/api/v1',
|
|
connectTimeout: const Duration(seconds: 30),
|
|
receiveTimeout: const Duration(seconds: 30),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
},
|
|
));
|
|
_setupInterceptors();
|
|
}
|
|
}
|
|
|
|
/// Dio 인스턴스 getter
|
|
Dio get dio => _dio;
|
|
|
|
/// 기본 옵션 설정
|
|
BaseOptions get _baseOptions {
|
|
try {
|
|
return BaseOptions(
|
|
baseUrl: Environment.apiBaseUrl,
|
|
connectTimeout: Duration(milliseconds: Environment.apiTimeout),
|
|
receiveTimeout: Duration(milliseconds: Environment.apiTimeout),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
},
|
|
validateStatus: (status) {
|
|
return status != null && status < 500;
|
|
},
|
|
);
|
|
} catch (e) {
|
|
// Environment가 초기화되지 않은 경우 기본값 사용
|
|
return BaseOptions(
|
|
baseUrl: 'http://localhost:8080/api/v1',
|
|
connectTimeout: const Duration(seconds: 30),
|
|
receiveTimeout: const Duration(seconds: 30),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
},
|
|
validateStatus: (status) {
|
|
return status != null && status < 500;
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 인터셉터 설정
|
|
void _setupInterceptors() {
|
|
_dio.interceptors.clear();
|
|
|
|
// 인증 인터셉터
|
|
_dio.interceptors.add(AuthInterceptor());
|
|
|
|
// 에러 처리 인터셉터
|
|
_dio.interceptors.add(ErrorInterceptor());
|
|
|
|
// 로깅 인터셉터 (개발 환경에서만)
|
|
try {
|
|
if (Environment.enableLogging && kDebugMode) {
|
|
_dio.interceptors.add(LoggingInterceptor());
|
|
}
|
|
} catch (e) {
|
|
// Environment 접근 실패 시 디버그 모드에서만 로깅 활성화
|
|
if (kDebugMode) {
|
|
_dio.interceptors.add(LoggingInterceptor());
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 토큰 업데이트
|
|
void updateAuthToken(String token) {
|
|
_dio.options.headers['Authorization'] = 'Bearer $token';
|
|
}
|
|
|
|
/// 토큰 제거
|
|
void removeAuthToken() {
|
|
_dio.options.headers.remove('Authorization');
|
|
}
|
|
|
|
/// GET 요청
|
|
Future<Response<T>> get<T>(
|
|
String path, {
|
|
Map<String, dynamic>? queryParameters,
|
|
Options? options,
|
|
CancelToken? cancelToken,
|
|
ProgressCallback? onReceiveProgress,
|
|
}) {
|
|
return _dio.get<T>(
|
|
path,
|
|
queryParameters: queryParameters,
|
|
options: options,
|
|
cancelToken: cancelToken,
|
|
onReceiveProgress: onReceiveProgress,
|
|
);
|
|
}
|
|
|
|
/// POST 요청
|
|
Future<Response<T>> post<T>(
|
|
String path, {
|
|
dynamic data,
|
|
Map<String, dynamic>? queryParameters,
|
|
Options? options,
|
|
CancelToken? cancelToken,
|
|
ProgressCallback? onSendProgress,
|
|
ProgressCallback? onReceiveProgress,
|
|
}) {
|
|
return _dio.post<T>(
|
|
path,
|
|
data: data,
|
|
queryParameters: queryParameters,
|
|
options: options,
|
|
cancelToken: cancelToken,
|
|
onSendProgress: onSendProgress,
|
|
onReceiveProgress: onReceiveProgress,
|
|
);
|
|
}
|
|
|
|
/// PUT 요청
|
|
Future<Response<T>> put<T>(
|
|
String path, {
|
|
dynamic data,
|
|
Map<String, dynamic>? queryParameters,
|
|
Options? options,
|
|
CancelToken? cancelToken,
|
|
ProgressCallback? onSendProgress,
|
|
ProgressCallback? onReceiveProgress,
|
|
}) {
|
|
return _dio.put<T>(
|
|
path,
|
|
data: data,
|
|
queryParameters: queryParameters,
|
|
options: options,
|
|
cancelToken: cancelToken,
|
|
onSendProgress: onSendProgress,
|
|
onReceiveProgress: onReceiveProgress,
|
|
);
|
|
}
|
|
|
|
/// PATCH 요청
|
|
Future<Response<T>> patch<T>(
|
|
String path, {
|
|
dynamic data,
|
|
Map<String, dynamic>? queryParameters,
|
|
Options? options,
|
|
CancelToken? cancelToken,
|
|
ProgressCallback? onSendProgress,
|
|
ProgressCallback? onReceiveProgress,
|
|
}) {
|
|
return _dio.patch<T>(
|
|
path,
|
|
data: data,
|
|
queryParameters: queryParameters,
|
|
options: options,
|
|
cancelToken: cancelToken,
|
|
onSendProgress: onSendProgress,
|
|
onReceiveProgress: onReceiveProgress,
|
|
);
|
|
}
|
|
|
|
/// DELETE 요청
|
|
Future<Response<T>> delete<T>(
|
|
String path, {
|
|
dynamic data,
|
|
Map<String, dynamic>? queryParameters,
|
|
Options? options,
|
|
CancelToken? cancelToken,
|
|
}) {
|
|
return _dio.delete<T>(
|
|
path,
|
|
data: data,
|
|
queryParameters: queryParameters,
|
|
options: options,
|
|
cancelToken: cancelToken,
|
|
);
|
|
}
|
|
|
|
/// 파일 업로드
|
|
Future<Response<T>> uploadFile<T>(
|
|
String path, {
|
|
required String filePath,
|
|
required String fileFieldName,
|
|
Map<String, dynamic>? additionalData,
|
|
ProgressCallback? onSendProgress,
|
|
CancelToken? cancelToken,
|
|
}) async {
|
|
final fileName = filePath.split('/').last;
|
|
final formData = FormData.fromMap({
|
|
fileFieldName: await MultipartFile.fromFile(
|
|
filePath,
|
|
filename: fileName,
|
|
),
|
|
...?additionalData,
|
|
});
|
|
|
|
return _dio.post<T>(
|
|
path,
|
|
data: formData,
|
|
options: Options(
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data',
|
|
},
|
|
),
|
|
onSendProgress: onSendProgress,
|
|
cancelToken: cancelToken,
|
|
);
|
|
}
|
|
|
|
/// 파일 다운로드
|
|
Future<Response> downloadFile(
|
|
String path, {
|
|
required String savePath,
|
|
ProgressCallback? onReceiveProgress,
|
|
CancelToken? cancelToken,
|
|
Map<String, dynamic>? queryParameters,
|
|
}) {
|
|
return _dio.download(
|
|
path,
|
|
savePath,
|
|
queryParameters: queryParameters,
|
|
onReceiveProgress: onReceiveProgress,
|
|
cancelToken: cancelToken,
|
|
options: Options(
|
|
responseType: ResponseType.bytes,
|
|
followRedirects: false,
|
|
),
|
|
);
|
|
}
|
|
} |