api.service.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:immich_mobile/shared/models/store.dart';
  4. import 'package:immich_mobile/utils/url_helper.dart';
  5. import 'package:openapi/api.dart';
  6. import 'package:http/http.dart';
  7. class ApiService {
  8. late ApiClient _apiClient;
  9. late UserApi userApi;
  10. late AuthenticationApi authenticationApi;
  11. late OAuthApi oAuthApi;
  12. late AlbumApi albumApi;
  13. late AssetApi assetApi;
  14. late SearchApi searchApi;
  15. late ServerInfoApi serverInfoApi;
  16. late PartnerApi partnerApi;
  17. ApiService() {
  18. final endpoint = Store.tryGet(StoreKey.serverEndpoint);
  19. if (endpoint != null && endpoint.isNotEmpty) {
  20. setEndpoint(endpoint);
  21. }
  22. }
  23. String? _authToken;
  24. setEndpoint(String endpoint) {
  25. _apiClient = ApiClient(basePath: endpoint);
  26. if (_authToken != null) {
  27. setAccessToken(_authToken!);
  28. }
  29. userApi = UserApi(_apiClient);
  30. authenticationApi = AuthenticationApi(_apiClient);
  31. oAuthApi = OAuthApi(_apiClient);
  32. albumApi = AlbumApi(_apiClient);
  33. assetApi = AssetApi(_apiClient);
  34. serverInfoApi = ServerInfoApi(_apiClient);
  35. searchApi = SearchApi(_apiClient);
  36. partnerApi = PartnerApi(_apiClient);
  37. }
  38. Future<String> resolveAndSetEndpoint(String serverUrl) async {
  39. final endpoint = await _resolveEndpoint(serverUrl);
  40. setEndpoint(endpoint);
  41. // Save in hivebox for next startup
  42. Store.put(StoreKey.serverEndpoint, endpoint);
  43. return endpoint;
  44. }
  45. /// Takes a server URL and attempts to resolve the API endpoint.
  46. ///
  47. /// Input: [schema://]host[:port][/path]
  48. /// schema - optional (default: https)
  49. /// host - required
  50. /// port - optional (default: based on schema)
  51. /// path - optional
  52. Future<String> _resolveEndpoint(String serverUrl) async {
  53. final url = sanitizeUrl(serverUrl);
  54. // Check for /.well-known/immich
  55. final wellKnownEndpoint = await _getWellKnownEndpoint(url);
  56. if (wellKnownEndpoint.isNotEmpty) return wellKnownEndpoint;
  57. // Otherwise, assume the URL provided is the api endpoint
  58. return url;
  59. }
  60. Future<String> _getWellKnownEndpoint(String baseUrl) async {
  61. final Client client = Client();
  62. try {
  63. final res = await client.get(
  64. Uri.parse("$baseUrl/.well-known/immich"),
  65. headers: {"Accept": "application/json"},
  66. );
  67. if (res.statusCode == 200) {
  68. final data = jsonDecode(res.body);
  69. final endpoint = data['api']['endpoint'].toString();
  70. if (endpoint.startsWith('/')) {
  71. // Full URL is relative to base
  72. return "$baseUrl$endpoint";
  73. }
  74. return endpoint;
  75. }
  76. } catch (e) {
  77. debugPrint("Could not locate /.well-known/immich at $baseUrl");
  78. }
  79. return "";
  80. }
  81. setAccessToken(String accessToken) {
  82. _authToken = accessToken;
  83. _apiClient.addDefaultHeader('Authorization', 'Bearer $accessToken');
  84. }
  85. ApiClient get apiClient => _apiClient;
  86. }