refactor: update connectivity service to use base URI for server health checks
- Refactored the ConnectivityService to replace health URI references with base URI for improved clarity and consistency. - Updated methods to probe server health and resolve URIs, ensuring a more streamlined approach to connectivity checks. - Enhanced the provider setup to create a Dio instance with custom headers from the active server configuration, improving flexibility in server communication.
This commit is contained in:
@@ -117,19 +117,19 @@ class ConnectivityService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<bool?> _probeActiveServer() async {
|
Future<bool?> _probeActiveServer() async {
|
||||||
final healthUri = _resolveHealthUri();
|
final baseUri = _resolveBaseUri();
|
||||||
if (healthUri == null) return null;
|
if (baseUri == null) return null;
|
||||||
|
|
||||||
return _probeHealthEndpoint(healthUri, updateLatency: true);
|
return _probeBaseEndpoint(baseUri, updateLatency: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool?> _probeAnyKnownServer() async {
|
Future<bool?> _probeAnyKnownServer() async {
|
||||||
try {
|
try {
|
||||||
final configs = await _ref.read(serverConfigsProvider.future);
|
final configs = await _ref.read(serverConfigsProvider.future);
|
||||||
for (final config in configs) {
|
for (final config in configs) {
|
||||||
final uri = _buildHealthUri(config.url);
|
final uri = _buildBaseUri(config.url);
|
||||||
if (uri == null) continue;
|
if (uri == null) continue;
|
||||||
final result = await _probeHealthEndpoint(uri);
|
final result = await _probeBaseEndpoint(uri);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -138,7 +138,7 @@ class ConnectivityService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool?> _probeHealthEndpoint(
|
Future<bool?> _probeBaseEndpoint(
|
||||||
Uri uri, {
|
Uri uri, {
|
||||||
bool updateLatency = false,
|
bool updateLatency = false,
|
||||||
}) async {
|
}) async {
|
||||||
@@ -157,8 +157,7 @@ class ConnectivityService {
|
|||||||
)
|
)
|
||||||
.timeout(const Duration(seconds: 4));
|
.timeout(const Duration(seconds: 4));
|
||||||
|
|
||||||
final isHealthy =
|
final isHealthy = response.statusCode == 200;
|
||||||
response.statusCode == 200 && _responseIndicatesHealth(response.data);
|
|
||||||
if (isHealthy && updateLatency) {
|
if (isHealthy && updateLatency) {
|
||||||
_lastLatencyMs = DateTime.now().difference(start).inMilliseconds;
|
_lastLatencyMs = DateTime.now().difference(start).inMilliseconds;
|
||||||
}
|
}
|
||||||
@@ -169,20 +168,20 @@ class ConnectivityService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Uri? _resolveHealthUri() {
|
Uri? _resolveBaseUri() {
|
||||||
final api = _ref.read(apiServiceProvider);
|
final api = _ref.read(apiServiceProvider);
|
||||||
if (api != null) {
|
if (api != null) {
|
||||||
return _buildHealthUri(api.baseUrl);
|
return _buildBaseUri(api.baseUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
final activeServer = _ref.read(activeServerProvider);
|
final activeServer = _ref.read(activeServerProvider);
|
||||||
return activeServer.maybeWhen(
|
return activeServer.maybeWhen(
|
||||||
data: (server) => server != null ? _buildHealthUri(server.url) : null,
|
data: (server) => server != null ? _buildBaseUri(server.url) : null,
|
||||||
orElse: () => null,
|
orElse: () => null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Uri? _buildHealthUri(String baseUrl) {
|
Uri? _buildBaseUri(String baseUrl) {
|
||||||
if (baseUrl.isEmpty) return null;
|
if (baseUrl.isEmpty) return null;
|
||||||
|
|
||||||
Uri? parsed = Uri.tryParse(baseUrl.trim());
|
Uri? parsed = Uri.tryParse(baseUrl.trim());
|
||||||
@@ -194,26 +193,54 @@ class ConnectivityService {
|
|||||||
}
|
}
|
||||||
if (parsed == null) return null;
|
if (parsed == null) return null;
|
||||||
|
|
||||||
return parsed.resolve('health');
|
// Return the base URL directly instead of resolving to /health
|
||||||
}
|
return parsed;
|
||||||
|
|
||||||
bool _responseIndicatesHealth(dynamic data) {
|
|
||||||
if (data is Map) {
|
|
||||||
final dynamic status = data['status'];
|
|
||||||
if (status is bool) return status;
|
|
||||||
if (status is num) return status != 0;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Providers
|
// Providers
|
||||||
final connectivityServiceProvider = Provider<ConnectivityService>((ref) {
|
final connectivityServiceProvider = Provider<ConnectivityService>((ref) {
|
||||||
// Use a lightweight Dio instance only for connectivity checks
|
// Create a Dio instance with custom headers from the active server config
|
||||||
|
final activeServer = ref.watch(activeServerProvider);
|
||||||
|
|
||||||
|
return activeServer.maybeWhen(
|
||||||
|
data: (server) {
|
||||||
|
if (server == null) {
|
||||||
|
// No server configured, use lightweight Dio
|
||||||
final dio = Dio();
|
final dio = Dio();
|
||||||
final service = ConnectivityService(dio, ref);
|
final service = ConnectivityService(dio, ref);
|
||||||
ref.onDispose(() => service.dispose());
|
ref.onDispose(() => service.dispose());
|
||||||
return service;
|
return service;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Dio with custom headers from server config
|
||||||
|
final dio = Dio(
|
||||||
|
BaseOptions(
|
||||||
|
baseUrl: server.url,
|
||||||
|
connectTimeout: const Duration(seconds: 30),
|
||||||
|
receiveTimeout: const Duration(seconds: 30),
|
||||||
|
followRedirects: true,
|
||||||
|
maxRedirects: 5,
|
||||||
|
validateStatus: (status) => status != null && status < 400,
|
||||||
|
// Add custom headers from server config
|
||||||
|
headers: server.customHeaders.isNotEmpty
|
||||||
|
? Map<String, String>.from(server.customHeaders)
|
||||||
|
: null,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final service = ConnectivityService(dio, ref);
|
||||||
|
ref.onDispose(() => service.dispose());
|
||||||
|
return service;
|
||||||
|
},
|
||||||
|
orElse: () {
|
||||||
|
// No server data available, use lightweight Dio
|
||||||
|
final dio = Dio();
|
||||||
|
final service = ConnectivityService(dio, ref);
|
||||||
|
ref.onDispose(() => service.dispose());
|
||||||
|
return service;
|
||||||
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@Riverpod(keepAlive: true)
|
@Riverpod(keepAlive: true)
|
||||||
|
|||||||
Reference in New Issue
Block a user