2024-08-25 10:38:24 +05:30
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
|
|
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';
|
2024-09-21 10:14:21 +05:30
|
|
|
import 'package:immich_mobile/utils/mixins/log.mixin.dart';
|
2024-08-25 10:38:24 +05:30
|
|
|
import 'package:openapi/api.dart';
|
|
|
|
|
2024-10-17 23:33:00 +05:30
|
|
|
class ImApiClient extends ApiClient with LogMixin {
|
|
|
|
ImApiClient({required String endpoint}) : super(basePath: endpoint);
|
2024-08-25 10:38:24 +05:30
|
|
|
|
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) {
|
2024-09-21 10:14:21 +05:30
|
|
|
log.e("Token invalid. Redirecting to login route");
|
2024-08-25 10:38:24 +05:30
|
|
|
await di<AppRouter>().replaceAll([const LoginRoute()]);
|
|
|
|
throw ApiException(res.statusCode, "Unauthorized");
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|