Files
b0esche_cloud/b0esche_cloud/lib/services/api_client.dart
Leon Bösche ed22c5eda4 Fix file upload timeout and UX issues
- Increase Dio receiveTimeout from 10s to 60s to allow file uploads and org creation
  to complete (Nextcloud user provisioning takes 5-7s)
- Hide 'Empty Folder' text and back button in root directory (main folder)
- Back button and empty message now only show in actual subdirectories
2026-01-11 01:19:00 +01:00

117 lines
3.3 KiB
Dart

import 'package:dio/dio.dart';
import '../models/api_error.dart';
import '../blocs/session/session_bloc.dart';
import '../blocs/session/session_event.dart';
import '../blocs/session/session_state.dart';
class ApiClient {
late final Dio _dio;
final SessionBloc _sessionBloc;
ApiClient(this._sessionBloc, {String baseUrl = 'https://go.b0esche.cloud'}) {
_dio = Dio(
BaseOptions(
baseUrl: baseUrl,
connectTimeout: const Duration(seconds: 10),
receiveTimeout: const Duration(seconds: 60), // Increased for file uploads and org operations
),
);
_dio.interceptors.add(
InterceptorsWrapper(
onRequest: (options, handler) {
// Add JWT if available
final token = _getCurrentToken();
if (token != null) {
options.headers['Authorization'] = 'Bearer $token';
}
return handler.next(options);
},
onError: (error, handler) async {
if (error.response?.statusCode == 401) {
final path = error.requestOptions.path;
// Do not expire session for auth endpoints; show inline error instead
final isAuthEndpoint = path.startsWith('/auth/');
if (!isAuthEndpoint) {
// Session expired, trigger logout
_sessionBloc.add(SessionExpired());
}
}
return handler.next(error);
},
),
);
}
String get baseUrl => _dio.options.baseUrl;
String? _getCurrentToken() {
// Get from SessionBloc state
final state = _sessionBloc.state;
if (state is SessionActive) {
return state.token;
}
return null;
}
Future<T> get<T>(
String path, {
Map<String, dynamic>? queryParameters,
required T Function(dynamic data) fromJson,
}) async {
try {
final response = await _dio.get(path, queryParameters: queryParameters);
return fromJson(response.data);
} on DioException catch (e) {
throw _handleError(e);
}
}
Future<T> post<T>(
String path, {
dynamic data,
required T Function(dynamic data) fromJson,
}) async {
try {
final response = await _dio.post(path, data: data);
return fromJson(response.data);
} on DioException catch (e) {
throw _handleError(e);
}
}
Future<List<T>> getList<T>(
String path, {
Map<String, dynamic>? queryParameters,
required T Function(dynamic data) fromJson,
}) async {
try {
final response = await _dio.get(path, queryParameters: queryParameters);
return (response.data as List).map(fromJson).toList();
} on DioException catch (e) {
throw _handleError(e);
}
}
ApiError _handleError(DioException e) {
final status = e.response?.statusCode;
final data = e.response?.data;
// Handle network errors
if (e.type == DioExceptionType.connectionError ||
e.type == DioExceptionType.connectionTimeout ||
e.type == DioExceptionType.receiveTimeout ||
e.type == DioExceptionType.sendTimeout) {
return ApiError(
code: 'NETWORK_ERROR',
message: 'Network error. Please check your connection and try again.',
status: status,
);
}
String code = data?['code'] ?? 'UNKNOWN';
String message = data?['message'] ?? 'Unknown error';
return ApiError(code: code, message: message, status: status);
}
}