fix: use openapi_patching from v1

This commit is contained in:
shenlong-tanwen
2024-09-25 22:26:02 +05:30
parent 9f29bce308
commit a09710ec7b
7 changed files with 71 additions and 120 deletions
@@ -1,8 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
import 'package:immich_mobile/domain/interfaces/store.interface.dart';
import 'package:immich_mobile/domain/models/store.model.dart';
@@ -66,53 +64,6 @@ class ImmichApiClient extends ApiClient with LogMixin {
return res;
}
// ignore: avoid-dynamic
static dynamic _patchDto(dynamic value, String targetType) {
switch (targetType) {
case 'UserPreferencesResponseDto':
if (value is Map && value['rating'] == null) {
value['rating'] = RatingResponse().toJson();
}
}
}
// ignore: avoid-dynamic
static dynamic fromJson(
// ignore: avoid-dynamic
dynamic value,
String targetType, {
bool growable = false,
}) {
_patchDto(value, targetType);
return ApiClient.fromJson(value, targetType, growable: growable);
}
@override
// ignore: avoid-dynamic
Future<dynamic> deserializeAsync(
String value,
String targetType, {
bool growable = false,
}) =>
deserialize(value, targetType, growable: growable);
@override
// ignore: avoid-dynamic
Future<dynamic> deserialize(
String value,
String targetType, {
bool growable = false,
}) async {
targetType = targetType.replaceAll(' ', '');
return targetType == 'String'
? value
: fromJson(
await compute((String j) => json.decode(j), value),
targetType,
growable: growable,
);
}
UsersApi getUsersApi() => UsersApi(this);
ServerApi getServerApi() => ServerApi(this);
AuthenticationApi getAuthenticationApi() => AuthenticationApi(this);
+53
View File
@@ -0,0 +1,53 @@
// ignore_for_file: avoid-dynamic, avoid-unsafe-collection-methods
import 'package:openapi/api.dart';
dynamic upgradeDto(dynamic value, String targetType) {
switch (targetType) {
case 'UserPreferencesResponseDto':
if (value is Map) {
addDefault(value, 'download.includeEmbeddedVideos', false);
addDefault(value, 'folders', FoldersResponse().toJson());
addDefault(value, 'memories', MemoriesResponse().toJson());
addDefault(value, 'ratings', RatingsResponse().toJson());
addDefault(value, 'people', PeopleResponse().toJson());
addDefault(value, 'tags', TagsResponse().toJson());
}
break;
case 'ServerConfigDto':
if (value is Map) {
addDefault(
value,
'mapLightStyleUrl',
'https://tiles.immich.cloud/v1/style/light.json',
);
addDefault(
value,
'mapDarkStyleUrl',
'https://tiles.immich.cloud/v1/style/dark.json',
);
}
case 'UserResponseDto' || 'UserAdminResponseDto':
if (value is Map) {
addDefault(value, 'profileChangedAt', DateTime.now().toIso8601String());
}
break;
}
}
addDefault(dynamic value, String keys, dynamic defaultValue) {
// Loop through the keys and assign the default value if the key is not present
List<String> keyList = keys.split('.');
dynamic current = value;
for (int i = 0; i < keyList.length - 1; i++) {
if (current[keyList[i]] == null) {
current[keyList[i]] = {};
}
current = current[keyList[i]];
}
if (current[keyList.last] == null) {
current[keyList.last] = defaultValue;
}
}