Without a timeout, a hanging backend request left the schedule page showing an infinite spinner forever. Timeout ensures any hung request fails fast and the spinner resolves. The catchError fallback in schedule_view handles the edge case where getProfessional rejects unexpectedly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
76 lines
2.5 KiB
Dart
76 lines
2.5 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:prosapp_web_app/services/local_storage.dart';
|
|
|
|
class ApiService {
|
|
ApiService._();
|
|
static final instance = ApiService._();
|
|
|
|
static String get baseUrl => dotenv.env['API_BASE_URL'] ?? 'https://backend.prosapp.co/api/v1';
|
|
|
|
Future<String?> getToken() => Future.value(LocalStorage.prefs.getString('jwt_token'));
|
|
|
|
Future<void> saveToken(String token) => LocalStorage.prefs.setString('jwt_token', token);
|
|
|
|
Future<void> deleteToken() => LocalStorage.prefs.remove('jwt_token');
|
|
|
|
Future<Map<String, String>> _headers() async {
|
|
final token = await getToken();
|
|
return {
|
|
'Content-Type': 'application/json',
|
|
if (token != null) 'Authorization': 'Bearer $token',
|
|
};
|
|
}
|
|
|
|
dynamic _parse(http.Response res) {
|
|
final body = jsonDecode(res.body);
|
|
if (res.statusCode >= 200 && res.statusCode < 300) return body;
|
|
throw Exception(body['message'] ?? 'Error ${res.statusCode}');
|
|
}
|
|
|
|
static const _timeout = Duration(seconds: 30);
|
|
|
|
Future<dynamic> get(String path) async {
|
|
final res = await http.get(Uri.parse('$baseUrl$path'), headers: await _headers()).timeout(_timeout);
|
|
return _parse(res);
|
|
}
|
|
|
|
Future<dynamic> post(String path, Map<String, dynamic> body) async {
|
|
final res = await http.post(
|
|
Uri.parse('$baseUrl$path'),
|
|
headers: await _headers(),
|
|
body: jsonEncode(body),
|
|
).timeout(_timeout);
|
|
return _parse(res);
|
|
}
|
|
|
|
Future<dynamic> patch(String path, Map<String, dynamic> body) async {
|
|
final res = await http.patch(
|
|
Uri.parse('$baseUrl$path'),
|
|
headers: await _headers(),
|
|
body: jsonEncode(body),
|
|
).timeout(_timeout);
|
|
return _parse(res);
|
|
}
|
|
|
|
Future<dynamic> delete(String path) async {
|
|
final res = await http.delete(Uri.parse('$baseUrl$path'), headers: await _headers()).timeout(_timeout);
|
|
return _parse(res);
|
|
}
|
|
|
|
Future<String?> upload(Uint8List bytes, String filename) async {
|
|
final token = await getToken();
|
|
final req = http.MultipartRequest('POST', Uri.parse('$baseUrl/storage/upload'));
|
|
if (token != null) req.headers['Authorization'] = 'Bearer $token';
|
|
req.files.add(http.MultipartFile.fromBytes('file', bytes, filename: filename));
|
|
final streamed = await req.send();
|
|
final res = await http.Response.fromStream(streamed);
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
return (jsonDecode(res.body) as Map<String, dynamic>)['url'] as String?;
|
|
}
|
|
return null;
|
|
}
|
|
}
|