1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-07 20:36:48 +01:00

consolidate common process into resolveAndSetEndpoint

This commit is contained in:
Connery Noble 2023-01-14 16:41:10 -08:00
parent a485bb2010
commit 7a23c58be2
6 changed files with 34 additions and 21 deletions

View file

@ -357,7 +357,6 @@ class BackgroundService {
Hive.openBox<HiveBackupAlbums>(hiveBackupInfoBox), Hive.openBox<HiveBackupAlbums>(hiveBackupInfoBox),
]); ]);
ApiService apiService = ApiService(); ApiService apiService = ApiService();
apiService.setEndpoint(Hive.box(userInfoBox).get(serverEndpointKey));
apiService.setAccessToken(Hive.box(userInfoBox).get(accessTokenKey)); apiService.setAccessToken(Hive.box(userInfoBox).get(accessTokenKey));
BackupService backupService = BackupService(apiService); BackupService backupService = BackupService(apiService);
AppSettingsService settingsService = AppSettingsService(); AppSettingsService settingsService = AppSettingsService();

View file

@ -59,10 +59,8 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
) async { ) async {
try { try {
// Resolve API server endpoint from user provided serverUrl // Resolve API server endpoint from user provided serverUrl
final serverEndpoint = await _apiService.resolveEndpoint(serverUrl); await _apiService.resolveAndSetEndpoint(serverUrl);
_apiService.setEndpoint(serverEndpoint);
await _apiService.serverInfoApi.pingServer(); await _apiService.serverInfoApi.pingServer();
Hive.box(userInfoBox).put(serverEndpointKey, serverEndpoint);
} catch (e) { } catch (e) {
debugPrint('Invalid Server Endpoint Url $e'); debugPrint('Invalid Server Endpoint Url $e');
return false; return false;

View file

@ -14,8 +14,7 @@ class OAuthService {
String serverUrl, String serverUrl,
) async { ) async {
// Resolve API server endpoint from user provided serverUrl // Resolve API server endpoint from user provided serverUrl
final serverEndpoint = await _apiService.resolveEndpoint(serverUrl); await _apiService.resolveAndSetEndpoint(serverUrl);
_apiService.setEndpoint(serverEndpoint);
return await _apiService.oAuthApi.generateConfig( return await _apiService.oAuthApi.generateConfig(
OAuthConfigDto(redirectUri: '$callbackUrlScheme:/'), OAuthConfigDto(redirectUri: '$callbackUrlScheme:/'),

View file

@ -42,9 +42,7 @@ class LoginForm extends HookConsumerWidget {
if (serverUrl.isNotEmpty) { if (serverUrl.isNotEmpty) {
isLoading.value = true; isLoading.value = true;
final serverEndpoint = final serverEndpoint =
await apiService.resolveEndpoint(serverUrl.toString()); await apiService.resolveAndSetEndpoint(serverUrl.toString());
apiService.setEndpoint(serverEndpoint);
Hive.box(userInfoBox).put(serverEndpointKey, serverEndpoint);
var loginConfig = await apiService.oAuthApi.generateConfig( var loginConfig = await apiService.oAuthApi.generateConfig(
OAuthConfigDto(redirectUri: serverEndpoint), OAuthConfigDto(redirectUri: serverEndpoint),
@ -218,11 +216,11 @@ class ServerEndpointInput extends StatelessWidget {
String? _validateInput(String? url) { String? _validateInput(String? url) {
if (url == null || url.isEmpty) return null; if (url == null || url.isEmpty) return null;
final validate = Uri.tryParse(sanitizeUrl(url)); final parsedUrl = Uri.tryParse(sanitizeUrl(url));
if (validate == null || if (parsedUrl == null ||
!validate.isAbsolute || !parsedUrl.isAbsolute ||
!validate.scheme.startsWith("http") || !parsedUrl.scheme.startsWith("http") ||
validate.host.isEmpty) { parsedUrl.host.isEmpty) {
return 'login_form_err_invalid_url'.tr(); return 'login_form_err_invalid_url'.tr();
} }
return null; return null;

View file

@ -1,6 +1,8 @@
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:immich_mobile/constants/hive_box.dart';
import 'package:immich_mobile/utils/url_helper.dart'; import 'package:immich_mobile/utils/url_helper.dart';
import 'package:openapi/api.dart'; import 'package:openapi/api.dart';
import 'package:http/http.dart'; import 'package:http/http.dart';
@ -16,6 +18,17 @@ class ApiService {
late ServerInfoApi serverInfoApi; late ServerInfoApi serverInfoApi;
late DeviceInfoApi deviceInfoApi; late DeviceInfoApi deviceInfoApi;
ApiService() {
if (Hive.isBoxOpen(userInfoBox)) {
final endpoint = Hive.box(userInfoBox).get(serverEndpointKey) as String;
if (endpoint.isNotEmpty) {
setEndpoint(endpoint);
}
} else {
debugPrint("Cannot init ApiServer endpoint, userInfoBox not open yet.");
}
}
setEndpoint(String endpoint) { setEndpoint(String endpoint) {
_apiClient = ApiClient(basePath: endpoint); _apiClient = ApiClient(basePath: endpoint);
userApi = UserApi(_apiClient); userApi = UserApi(_apiClient);
@ -27,6 +40,15 @@ class ApiService {
deviceInfoApi = DeviceInfoApi(_apiClient); deviceInfoApi = DeviceInfoApi(_apiClient);
} }
Future<String> resolveAndSetEndpoint(String serverUrl) async {
final endpoint = await _resolveEndpoint(serverUrl);
setEndpoint(endpoint);
// Save in hivebox for next startup
Hive.box(userInfoBox).put(serverEndpointKey, endpoint);
return endpoint;
}
/// Takes a server URL and attempts to resolve the API endpoint. /// Takes a server URL and attempts to resolve the API endpoint.
/// ///
/// Input: [schema://]host[:port][/path] /// Input: [schema://]host[:port][/path]
@ -34,18 +56,18 @@ class ApiService {
/// host - required /// host - required
/// 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 {
final url = sanitizeUrl(serverUrl); final url = sanitizeUrl(serverUrl);
// Check for /.well-known/immich // Check for /.well-known/immich
final wellKnownEndpoint = await getWellKnownEndpoint(url); final wellKnownEndpoint = await _getWellKnownEndpoint(url);
if (wellKnownEndpoint.isNotEmpty) return wellKnownEndpoint; if (wellKnownEndpoint.isNotEmpty) return wellKnownEndpoint;
// Otherwise, assume the URL provided is the api endpoint // Otherwise, assume the URL provided is the api endpoint
return url; return url;
} }
Future<String> getWellKnownEndpoint(String baseUrl) async { Future<String> _getWellKnownEndpoint(String baseUrl) async {
final Client client = Client(); final Client client = Client();
try { try {

View file

@ -23,10 +23,7 @@ class SplashScreenPage extends HookConsumerWidget {
try { try {
if (loginInfo != null) { if (loginInfo != null) {
// Resolve API server endpoint from user provided serverUrl // Resolve API server endpoint from user provided serverUrl
final serverEndpoint = await apiService.resolveAndSetEndpoint(loginInfo.serverUrl);
await apiService.resolveEndpoint(loginInfo.serverUrl);
apiService.setEndpoint(serverEndpoint);
Hive.box(userInfoBox).put(serverEndpointKey, serverEndpoint);
var isSuccess = await ref var isSuccess = await ref
.read(authenticationProvider.notifier) .read(authenticationProvider.notifier)