2023-01-19 08:45:37 -07:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
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';
|
2022-07-13 05:23:48 -07:00
|
|
|
import 'package:openapi/api.dart';
|
2023-01-19 08:45:37 -07:00
|
|
|
import 'package:http/http.dart';
|
2022-07-13 05:23:48 -07:00
|
|
|
|
|
|
|
class ApiService {
|
|
|
|
late ApiClient _apiClient;
|
|
|
|
|
|
|
|
late UserApi userApi;
|
|
|
|
late AuthenticationApi authenticationApi;
|
2022-11-20 10:43:10 -07:00
|
|
|
late OAuthApi oAuthApi;
|
2022-07-13 05:23:48 -07:00
|
|
|
late AlbumApi albumApi;
|
|
|
|
late AssetApi assetApi;
|
|
|
|
late ServerInfoApi serverInfoApi;
|
|
|
|
late DeviceInfoApi deviceInfoApi;
|
|
|
|
|
2023-01-19 08:45:37 -07:00
|
|
|
ApiService() {
|
|
|
|
if (Hive.isBoxOpen(userInfoBox)) {
|
2023-01-19 12:06:57 -07:00
|
|
|
final endpoint = Hive.box(userInfoBox).get(serverEndpointKey) as String?;
|
|
|
|
if (endpoint != null && endpoint.isNotEmpty) {
|
2023-01-19 08:45:37 -07:00
|
|
|
setEndpoint(endpoint);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debugPrint("Cannot init ApiServer endpoint, userInfoBox not open yet.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 05:23:48 -07:00
|
|
|
setEndpoint(String endpoint) {
|
|
|
|
_apiClient = ApiClient(basePath: endpoint);
|
|
|
|
userApi = UserApi(_apiClient);
|
|
|
|
authenticationApi = AuthenticationApi(_apiClient);
|
2022-11-20 10:43:10 -07:00
|
|
|
oAuthApi = OAuthApi(_apiClient);
|
2022-07-13 05:23:48 -07:00
|
|
|
albumApi = AlbumApi(_apiClient);
|
|
|
|
assetApi = AssetApi(_apiClient);
|
|
|
|
serverInfoApi = ServerInfoApi(_apiClient);
|
|
|
|
deviceInfoApi = DeviceInfoApi(_apiClient);
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:45:37 -07:00
|
|
|
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.
|
|
|
|
///
|
|
|
|
/// 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);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 05:23:48 -07:00
|
|
|
setAccessToken(String accessToken) {
|
2022-07-18 12:14:25 -07:00
|
|
|
_apiClient.addDefaultHeader('Authorization', 'Bearer $accessToken');
|
2022-07-13 05:23:48 -07:00
|
|
|
}
|
|
|
|
}
|