mirror of
				https://github.com/KevinMidboe/immich.git
				synced 2025-10-29 17:40:28 +00:00 
			
		
		
		
	Refactor mobile to use OpenApi generated SDK (#336)
This commit is contained in:
		
							
								
								
									
										30
									
								
								mobile/lib/shared/services/api.service.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								mobile/lib/shared/services/api.service.dart
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||||
| import 'package:openapi/api.dart'; | ||||
|  | ||||
| final apiServiceProvider = Provider((ref) => ApiService()); | ||||
|  | ||||
| class ApiService { | ||||
|   late ApiClient _apiClient; | ||||
|  | ||||
|   late UserApi userApi; | ||||
|   late AuthenticationApi authenticationApi; | ||||
|   late AlbumApi albumApi; | ||||
|   late AssetApi assetApi; | ||||
|   late ServerInfoApi serverInfoApi; | ||||
|   late DeviceInfoApi deviceInfoApi; | ||||
|  | ||||
|   setEndpoint(String endpoint) { | ||||
|     _apiClient = ApiClient(basePath: endpoint); | ||||
|  | ||||
|     userApi = UserApi(_apiClient); | ||||
|     authenticationApi = AuthenticationApi(_apiClient); | ||||
|     albumApi = AlbumApi(_apiClient); | ||||
|     assetApi = AssetApi(_apiClient); | ||||
|     serverInfoApi = ServerInfoApi(_apiClient); | ||||
|     deviceInfoApi = DeviceInfoApi(_apiClient); | ||||
|   } | ||||
|  | ||||
|   setAccessToken(String accessToken) { | ||||
|     _apiClient.addDefaultHeader('Authorization', 'bearer $accessToken'); | ||||
|   } | ||||
| } | ||||
| @@ -2,6 +2,7 @@ import 'package:flutter_udid/flutter_udid.dart'; | ||||
| import 'dart:io' show Platform; | ||||
|  | ||||
| import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||||
| import 'package:openapi/api.dart'; | ||||
|  | ||||
| final deviceInfoServiceProvider = Provider((_) => DeviceInfoService()); | ||||
|  | ||||
| @@ -9,12 +10,12 @@ class DeviceInfoService { | ||||
|   Future<Map<String, dynamic>> getDeviceInfo() async { | ||||
|     // Get device info | ||||
|     var deviceId = await FlutterUdid.consistentUdid; | ||||
|     var deviceType = ""; | ||||
|     var deviceType = DeviceTypeEnum.ANDROID; | ||||
|  | ||||
|     if (Platform.isAndroid) { | ||||
|       deviceType = "ANDROID"; | ||||
|       deviceType = DeviceTypeEnum.ANDROID; | ||||
|     } else if (Platform.isIOS) { | ||||
|       deviceType = "IOS"; | ||||
|       deviceType = DeviceTypeEnum.IOS; | ||||
|     } | ||||
|  | ||||
|     return {"deviceId": deviceId, "deviceType": deviceType}; | ||||
|   | ||||
| @@ -1,21 +0,0 @@ | ||||
| import 'package:hive/hive.dart'; | ||||
| import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||||
| import 'package:immich_mobile/constants/hive_box.dart'; | ||||
|  | ||||
| final localStorageServiceProvider = Provider((_) => LocalStorageService()); | ||||
|  | ||||
| class LocalStorageService { | ||||
|   late Box _box; | ||||
|  | ||||
|   LocalStorageService() { | ||||
|     _box = Hive.box(userInfoBox); | ||||
|   } | ||||
|  | ||||
|   T get<T>(String key) { | ||||
|     return _box.get(key); | ||||
|   } | ||||
|  | ||||
|   put<T>(String key, T value) { | ||||
|     return _box.put(key, value); | ||||
|   } | ||||
| } | ||||
| @@ -33,10 +33,11 @@ class NetworkService { | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   Future<dynamic> getRequest( | ||||
|       {required String url, | ||||
|       bool isByteResponse = false, | ||||
|       bool isStreamReponse = false}) async { | ||||
|   Future<dynamic> getRequest({ | ||||
|     required String url, | ||||
|     bool isByteResponse = false, | ||||
|     bool isStreamReponse = false, | ||||
|   }) async { | ||||
|     try { | ||||
|       var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey); | ||||
|  | ||||
|   | ||||
| @@ -1,33 +1,33 @@ | ||||
| import 'package:dio/dio.dart'; | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||||
| import 'package:immich_mobile/shared/models/server_info.model.dart'; | ||||
| import 'package:immich_mobile/shared/models/server_version.model.dart'; | ||||
| import 'package:immich_mobile/shared/services/network.service.dart'; | ||||
| import 'package:immich_mobile/shared/services/api.service.dart'; | ||||
| import 'package:openapi/api.dart'; | ||||
|  | ||||
| final serverInfoServiceProvider = | ||||
|     Provider((ref) => ServerInfoService(ref.watch(networkServiceProvider))); | ||||
| final serverInfoServiceProvider = Provider( | ||||
|   (ref) => ServerInfoService( | ||||
|     ref.watch(apiServiceProvider), | ||||
|   ), | ||||
| ); | ||||
|  | ||||
| class ServerInfoService { | ||||
|   final NetworkService _networkService; | ||||
|   ServerInfoService(this._networkService); | ||||
|   final ApiService _apiService; | ||||
|   ServerInfoService(this._apiService); | ||||
|  | ||||
|   Future<ServerInfo> getServerInfo() async { | ||||
|     Response response = await _networkService.getRequest(url: 'server-info'); | ||||
|  | ||||
|     return ServerInfo.fromJson(response.toString()); | ||||
|   Future<ServerInfoResponseDto?> getServerInfo() async { | ||||
|     try { | ||||
|       return await _apiService.serverInfoApi.getServerInfo(); | ||||
|     } catch (e) { | ||||
|       debugPrint("Error [getServerInfo] ${e.toString()}"); | ||||
|       return null; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   Future<ServerVersion?> getServerVersion() async { | ||||
|   Future<ServerVersionReponseDto?> getServerVersion() async { | ||||
|     try { | ||||
|       Response response = | ||||
|           await _networkService.getRequest(url: 'server-info/version'); | ||||
|  | ||||
|       return ServerVersion.fromJson(response.toString()); | ||||
|       return await _apiService.serverInfoApi.getServerVersion(); | ||||
|     } catch (e) { | ||||
|       debugPrint("Error getting server info"); | ||||
|       return null; | ||||
|     } | ||||
|  | ||||
|     return null; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -1,70 +1,49 @@ | ||||
| import 'dart:convert'; | ||||
|  | ||||
| import 'package:dio/dio.dart'; | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:hive/hive.dart'; | ||||
| import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||||
| import 'package:http/http.dart'; | ||||
| import 'package:http_parser/http_parser.dart'; | ||||
| import 'package:image_picker/image_picker.dart'; | ||||
| import 'package:immich_mobile/constants/hive_box.dart'; | ||||
| import 'package:immich_mobile/shared/models/upload_profile_image_repsonse.model.dart'; | ||||
| import 'package:immich_mobile/shared/models/user.model.dart'; | ||||
| import 'package:immich_mobile/shared/services/network.service.dart'; | ||||
| import 'package:immich_mobile/utils/dio_http_interceptor.dart'; | ||||
| import 'package:immich_mobile/shared/services/api.service.dart'; | ||||
| import 'package:immich_mobile/utils/files_helper.dart'; | ||||
| import 'package:openapi/api.dart'; | ||||
|  | ||||
| final userServiceProvider = | ||||
|     Provider((ref) => UserService(ref.watch(networkServiceProvider))); | ||||
| final userServiceProvider = Provider( | ||||
|   (ref) => UserService( | ||||
|     ref.watch(apiServiceProvider), | ||||
|   ), | ||||
| ); | ||||
|  | ||||
| class UserService { | ||||
|   final NetworkService _networkService; | ||||
|   UserService(this._networkService); | ||||
|   final ApiService _apiService; | ||||
|  | ||||
|   Future<List<User>> getAllUsersInfo() async { | ||||
|   UserService(this._apiService); | ||||
|  | ||||
|   Future<List<UserResponseDto>?> getAllUsersInfo({required bool isAll}) async { | ||||
|     try { | ||||
|       var res = await _networkService.getRequest(url: 'user'); | ||||
|       List<dynamic> decodedData = jsonDecode(res.toString()); | ||||
|       List<User> result = List.from(decodedData.map((e) => User.fromMap(e))); | ||||
|  | ||||
|       return result; | ||||
|       return await _apiService.userApi.getAllUsers(isAll); | ||||
|     } catch (e) { | ||||
|       debugPrint("Error getAllUsersInfo  ${e.toString()}"); | ||||
|       debugPrint("Error [getAllUsersInfo]  ${e.toString()}"); | ||||
|       return null; | ||||
|     } | ||||
|  | ||||
|     return []; | ||||
|   } | ||||
|  | ||||
|   Future<UploadProfileImageResponse?> uploadProfileImage(XFile image) async { | ||||
|     var dio = Dio(); | ||||
|     dio.interceptors.add(AuthenticatedRequestInterceptor()); | ||||
|     String savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey); | ||||
|     var mimeType = FileHelper.getMimeType(image.path); | ||||
|  | ||||
|     final imageData = MultipartFile.fromBytes( | ||||
|       await image.readAsBytes(), | ||||
|       filename: image.name, | ||||
|       contentType: MediaType( | ||||
|         mimeType["type"], | ||||
|         mimeType["subType"], | ||||
|       ), | ||||
|     ); | ||||
|  | ||||
|     final formData = FormData.fromMap({'file': imageData}); | ||||
|  | ||||
|   Future<CreateProfileImageResponseDto?> uploadProfileImage(XFile image) async { | ||||
|     try { | ||||
|       Response res = await dio.post( | ||||
|         '$savedEndpoint/user/profile-image', | ||||
|         data: formData, | ||||
|       var mimeType = FileHelper.getMimeType(image.path); | ||||
|  | ||||
|       return await _apiService.userApi.createProfileImage( | ||||
|         MultipartFile.fromBytes( | ||||
|           'file', | ||||
|           await image.readAsBytes(), | ||||
|           filename: image.name, | ||||
|           contentType: MediaType( | ||||
|             mimeType["type"], | ||||
|             mimeType["subType"], | ||||
|           ), | ||||
|         ), | ||||
|       ); | ||||
|  | ||||
|       var payload = UploadProfileImageResponse.fromJson(res.toString()); | ||||
|  | ||||
|       return payload; | ||||
|     } on DioError catch (e) { | ||||
|       debugPrint("Error uploading file: ${e.response}"); | ||||
|       return null; | ||||
|     } catch (e) { | ||||
|       debugPrint("Error uploading file: $e"); | ||||
|       debugPrint("Error [uploadProfileImage] ${e.toString()}"); | ||||
|       return null; | ||||
|     } | ||||
|   } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user