2023-07-28 05:05:27 +02:00
|
|
|
import 'dart:async';
|
2023-01-19 17:45:37 +02:00
|
|
|
import 'dart:convert';
|
2023-07-28 05:05:27 +02:00
|
|
|
import 'dart:io';
|
2023-01-19 17:45:37 +02:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2023-03-23 03:36:44 +02:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2023-01-19 17:45:37 +02:00
|
|
|
import 'package:immich_mobile/utils/url_helper.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2023-01-19 17:45:37 +02:00
|
|
|
import 'package:http/http.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
|
|
|
|
class ApiService {
|
|
|
|
late ApiClient _apiClient;
|
|
|
|
|
|
|
|
late UserApi userApi;
|
|
|
|
late AuthenticationApi authenticationApi;
|
2022-11-20 19:43:10 +02:00
|
|
|
late OAuthApi oAuthApi;
|
2022-07-13 14:23:48 +02:00
|
|
|
late AlbumApi albumApi;
|
|
|
|
late AssetApi assetApi;
|
2023-03-23 17:08:14 +02:00
|
|
|
late SearchApi searchApi;
|
2022-07-13 14:23:48 +02:00
|
|
|
late ServerInfoApi serverInfoApi;
|
2023-05-25 05:52:43 +02:00
|
|
|
late PartnerApi partnerApi;
|
2023-06-23 17:44:02 +02:00
|
|
|
late PersonApi personApi;
|
2023-09-10 14:51:18 +02:00
|
|
|
late AuditApi auditApi;
|
2023-10-22 17:05:10 +02:00
|
|
|
late SharedLinkApi sharedLinkApi;
|
2023-11-09 18:10:56 +02:00
|
|
|
late SystemConfigApi systemConfigApi;
|
2023-11-06 17:46:26 +02:00
|
|
|
late ActivityApi activityApi;
|
2022-07-13 14:23:48 +02:00
|
|
|
|
2023-01-19 17:45:37 +02:00
|
|
|
ApiService() {
|
2023-03-23 03:36:44 +02:00
|
|
|
final endpoint = Store.tryGet(StoreKey.serverEndpoint);
|
|
|
|
if (endpoint != null && endpoint.isNotEmpty) {
|
|
|
|
setEndpoint(endpoint);
|
2023-01-19 17:45:37 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-04 22:35:13 +02:00
|
|
|
String? _accessToken;
|
2023-01-19 17:45:37 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
setEndpoint(String endpoint) {
|
|
|
|
_apiClient = ApiClient(basePath: endpoint);
|
2024-02-04 22:35:13 +02:00
|
|
|
if (_accessToken != null) {
|
|
|
|
setAccessToken(_accessToken!);
|
2023-03-04 00:38:30 +02:00
|
|
|
}
|
2022-07-13 14:23:48 +02:00
|
|
|
userApi = UserApi(_apiClient);
|
|
|
|
authenticationApi = AuthenticationApi(_apiClient);
|
2022-11-20 19:43:10 +02:00
|
|
|
oAuthApi = OAuthApi(_apiClient);
|
2022-07-13 14:23:48 +02:00
|
|
|
albumApi = AlbumApi(_apiClient);
|
|
|
|
assetApi = AssetApi(_apiClient);
|
|
|
|
serverInfoApi = ServerInfoApi(_apiClient);
|
2023-03-23 17:08:14 +02:00
|
|
|
searchApi = SearchApi(_apiClient);
|
2023-05-25 05:52:43 +02:00
|
|
|
partnerApi = PartnerApi(_apiClient);
|
2023-06-23 17:44:02 +02:00
|
|
|
personApi = PersonApi(_apiClient);
|
2023-09-10 14:51:18 +02:00
|
|
|
auditApi = AuditApi(_apiClient);
|
2023-10-22 17:05:10 +02:00
|
|
|
sharedLinkApi = SharedLinkApi(_apiClient);
|
2023-11-09 18:10:56 +02:00
|
|
|
systemConfigApi = SystemConfigApi(_apiClient);
|
2023-11-06 17:46:26 +02:00
|
|
|
activityApi = ActivityApi(_apiClient);
|
2022-07-13 14:23:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-19 17:45:37 +02:00
|
|
|
Future<String> resolveAndSetEndpoint(String serverUrl) async {
|
|
|
|
final endpoint = await _resolveEndpoint(serverUrl);
|
|
|
|
setEndpoint(endpoint);
|
|
|
|
|
|
|
|
// Save in hivebox for next startup
|
2023-03-23 03:36:44 +02:00
|
|
|
Store.put(StoreKey.serverEndpoint, endpoint);
|
2023-01-19 17:45:37 +02:00
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Takes a server URL and attempts to resolve the API endpoint.
|
|
|
|
///
|
|
|
|
/// Input: [schema://]host[:port][/path]
|
|
|
|
/// schema - optional (default: https)
|
|
|
|
/// host - required
|
|
|
|
/// port - optional (default: based on schema)
|
|
|
|
/// path - optional
|
|
|
|
Future<String> _resolveEndpoint(String serverUrl) async {
|
|
|
|
final url = sanitizeUrl(serverUrl);
|
|
|
|
|
2023-07-28 05:05:27 +02:00
|
|
|
if (!await _isEndpointAvailable(serverUrl)) {
|
|
|
|
throw ApiException(503, "Server is not reachable");
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:45:37 +02:00
|
|
|
// Check for /.well-known/immich
|
|
|
|
final wellKnownEndpoint = await _getWellKnownEndpoint(url);
|
|
|
|
if (wellKnownEndpoint.isNotEmpty) return wellKnownEndpoint;
|
|
|
|
|
|
|
|
// Otherwise, assume the URL provided is the api endpoint
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2023-07-28 05:05:27 +02:00
|
|
|
Future<bool> _isEndpointAvailable(String serverUrl) async {
|
|
|
|
final Client client = Client();
|
|
|
|
|
|
|
|
if (!serverUrl.endsWith('/api')) {
|
|
|
|
serverUrl += '/api';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throw Socket or Timeout exceptions,
|
|
|
|
// we do not care if the endpoints hits an HTTP error
|
|
|
|
try {
|
|
|
|
await client
|
|
|
|
.get(
|
|
|
|
Uri.parse(serverUrl),
|
|
|
|
)
|
|
|
|
.timeout(const Duration(seconds: 5));
|
|
|
|
} on TimeoutException catch (_) {
|
|
|
|
return false;
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:45:37 +02:00
|
|
|
Future<String> _getWellKnownEndpoint(String baseUrl) async {
|
|
|
|
final Client client = Client();
|
|
|
|
|
|
|
|
try {
|
|
|
|
final res = await client.get(
|
|
|
|
Uri.parse("$baseUrl/.well-known/immich"),
|
|
|
|
headers: {"Accept": "application/json"},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
final data = jsonDecode(res.body);
|
|
|
|
final endpoint = data['api']['endpoint'].toString();
|
|
|
|
|
|
|
|
if (endpoint.startsWith('/')) {
|
|
|
|
// Full URL is relative to base
|
|
|
|
return "$baseUrl$endpoint";
|
|
|
|
}
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Could not locate /.well-known/immich at $baseUrl");
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
setAccessToken(String accessToken) {
|
2024-02-04 22:35:13 +02:00
|
|
|
_accessToken = accessToken;
|
|
|
|
_apiClient.addDefaultHeader('x-immich-user-token', accessToken);
|
2022-07-13 14:23:48 +02:00
|
|
|
}
|
2023-03-04 00:38:30 +02:00
|
|
|
|
|
|
|
ApiClient get apiClient => _apiClient;
|
2022-07-13 14:23:48 +02:00
|
|
|
}
|