0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-02-11 01:18:24 -05:00
immich/mobile-v2/lib/utils/immich_api_client.dart

122 lines
3.4 KiB
Dart
Raw Normal View History

2024-09-02 02:16:47 +05:30
import 'dart:convert';
2024-08-25 10:38:24 +05:30
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
2024-09-02 02:16:47 +05:30
import 'package:flutter/foundation.dart';
2024-08-25 10:38:24 +05:30
import 'package:http/http.dart';
import 'package:immich_mobile/domain/interfaces/store.interface.dart';
import 'package:immich_mobile/domain/models/store.model.dart';
import 'package:immich_mobile/presentation/router/router.dart';
import 'package:immich_mobile/service_locator.dart';
import 'package:immich_mobile/utils/constants/globals.dart';
import 'package:immich_mobile/utils/mixins/log_context.mixin.dart';
import 'package:openapi/api.dart';
class ImmichApiClient extends ApiClient with LogContext {
ImmichApiClient({required String endpoint}) : super(basePath: endpoint);
2024-09-10 01:14:05 +05:30
Map<String, String> get headers => defaultHeaderMap;
2024-09-02 02:16:47 +05:30
2024-08-25 10:38:24 +05:30
Future<void> init({String? accessToken}) async {
final token =
accessToken ?? (await di<IStoreRepository>().get(StoreKey.accessToken));
if (token != null) {
addDefaultHeader(kImmichHeaderAuthKey, token);
}
final deviceInfo = DeviceInfoPlugin();
final String deviceModel;
if (Platform.isIOS) {
deviceModel = (await deviceInfo.iosInfo).utsname.machine;
} else {
deviceModel = (await deviceInfo.androidInfo).model;
}
addDefaultHeader(kImmichHeaderDeviceModel, deviceModel);
addDefaultHeader(kImmichHeaderDeviceType, Platform.operatingSystem);
}
@override
Future<Response> invokeAPI(
String path,
String method,
List<QueryParam> queryParams,
Object? body,
Map<String, String> headerParams,
Map<String, String> formParams,
String? contentType,
) async {
final res = await super.invokeAPI(
path,
method,
queryParams,
body,
headerParams,
formParams,
contentType,
);
if (res.statusCode == HttpStatus.unauthorized) {
log.severe("Token invalid. Redirecting to login route");
await di<AppRouter>().replaceAll([const LoginRoute()]);
throw ApiException(res.statusCode, "Unauthorized");
}
return res;
}
// ignore: avoid-dynamic
static dynamic _patchDto(dynamic value, String targetType) {
switch (targetType) {
case 'UserPreferencesResponseDto':
2024-09-14 22:29:51 +05:30
if (value is Map && value['rating'] == null) {
value['rating'] = RatingResponse().toJson();
2024-08-25 10:38:24 +05:30
}
}
}
// 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);
}
2024-09-02 02:16:47 +05:30
@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,
);
}
2024-08-25 10:38:24 +05:30
UsersApi getUsersApi() => UsersApi(this);
ServerApi getServerApi() => ServerApi(this);
AuthenticationApi getAuthenticationApi() => AuthenticationApi(this);
OAuthApi getOAuthApi() => OAuthApi(this);
2024-09-02 02:16:47 +05:30
SyncApi getSyncApi() => SyncApi(this);
2024-08-25 10:38:24 +05:30
}