0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-07 00:50:23 -05:00

Update api.service.dart

Resolves the server endpoint and sets the base path for the API client.

If DNS resolution fails, it falls back to the original server URL.
Logs the resolution process for debugging.
This commit is contained in:
Jackie264 2024-12-27 21:53:13 +08:00 committed by GitHub
parent 90f9b08cb3
commit b3a9e4db23
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -10,6 +10,8 @@ import 'package:logging/logging.dart';
import 'package:openapi/api.dart'; import 'package:openapi/api.dart';
import 'package:http/http.dart'; import 'package:http/http.dart';
import 'dns_query_service.dart';
class ApiService implements Authentication { class ApiService implements Authentication {
late ApiClient _apiClient; late ApiClient _apiClient;
@ -83,6 +85,9 @@ class ApiService implements Authentication {
/// port - optional (default: based on schema) /// port - optional (default: based on schema)
/// path - optional /// path - optional
Future<String> resolveEndpoint(String serverUrl) async { Future<String> resolveEndpoint(String serverUrl) async {
if (!Uri.tryParse(serverUrl)?.hasAbsolutePath ?? false) {
throw ApiException(400, "Invalid server URL: $serverUrl");
}
final url = sanitizeUrl(serverUrl); final url = sanitizeUrl(serverUrl);
if (!await _isEndpointAvailable(serverUrl)) { if (!await _isEndpointAvailable(serverUrl)) {
@ -97,7 +102,23 @@ class ApiService implements Authentication {
return url; return url;
} }
final DnsQueryService _dnsQueryService = DnsQueryService();
/// Resolves the server endpoint and sets the base path for the API client.
///
/// If DNS resolution fails, it falls back to the original server URL.
/// Logs the resolution process for debugging.
Future<bool> _isEndpointAvailable(String serverUrl) async { Future<bool> _isEndpointAvailable(String serverUrl) async {
final parsedUri = Uri.parse(serverUrl);
final domain = parsedUri.host;
final resolvedUrl = await _dnsQueryService.resolveDns(domain);
if (resolvedUrl != null) {
serverUrl = resolvedUrl;
} else {
_log.info("DNS resolution failed for domain: $domain, original serverUrl: $serverUrl");
}
if (!serverUrl.endsWith('/api')) { if (!serverUrl.endsWith('/api')) {
serverUrl += '/api'; serverUrl += '/api';
} }
@ -117,6 +138,7 @@ class ApiService implements Authentication {
); );
return false; return false;
} }
_log.info('Resolved URL: $resolvedUrl, Final server URL: $serverUrl');
return true; return true;
} }
@ -143,7 +165,7 @@ class ApiService implements Authentication {
return endpoint; return endpoint;
} }
} catch (e) { } catch (e) {
debugPrint("Could not locate /.well-known/immich at $baseUrl"); _log.warning("Could not locate /.well-known/immich at $baseUrl");
} }
return ""; return "";
@ -157,6 +179,7 @@ class ApiService implements Authentication {
Future<void> setDeviceInfoHeader() async { Future<void> setDeviceInfoHeader() async {
DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin(); DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
try {
if (Platform.isIOS) { if (Platform.isIOS) {
final iosInfo = await deviceInfoPlugin.iosInfo; final iosInfo = await deviceInfoPlugin.iosInfo;
authenticationApi.apiClient authenticationApi.apiClient
@ -168,6 +191,9 @@ class ApiService implements Authentication {
.addDefaultHeader('deviceModel', androidInfo.model); .addDefaultHeader('deviceModel', androidInfo.model);
authenticationApi.apiClient.addDefaultHeader('deviceType', 'Android'); authenticationApi.apiClient.addDefaultHeader('deviceType', 'Android');
} }
} catch (e) {
_log.warning('Failed to set device info headers: $e');
}
} }
static Map<String, String> getRequestHeaders() { static Map<String, String> getRequestHeaders() {