mirror of
https://github.com/immich-app/immich.git
synced 2026-03-22 05:39:38 +03:00
* use cookiejar * cookie duping hook * remove old pref * handle network switching on logout * remove bootstrapCookies * dead code * fix cast * use constants * use new event name * update api
56 lines
1.9 KiB
Dart
56 lines
1.9 KiB
Dart
import 'dart:ffi';
|
|
import 'dart:io';
|
|
|
|
import 'package:cupertino_http/cupertino_http.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:immich_mobile/providers/infrastructure/platform.provider.dart';
|
|
import 'package:ok_http/ok_http.dart';
|
|
import 'package:web_socket/web_socket.dart';
|
|
|
|
class NetworkRepository {
|
|
static http.Client? _client;
|
|
static Pointer<Void>? _clientPointer;
|
|
|
|
static Future<void> init() async {
|
|
final clientPointer = Pointer<Void>.fromAddress(await networkApi.getClientPointer());
|
|
if (clientPointer == _clientPointer) {
|
|
return;
|
|
}
|
|
_clientPointer = clientPointer;
|
|
_client?.close();
|
|
if (Platform.isIOS) {
|
|
final session = URLSession.fromRawPointer(clientPointer.cast());
|
|
_client = CupertinoClient.fromSharedSession(session);
|
|
} else {
|
|
_client = OkHttpClient.fromJniGlobalRef(clientPointer);
|
|
}
|
|
}
|
|
|
|
static Future<void> setHeaders(Map<String, String> headers, List<String> serverUrls, {String? token}) async {
|
|
await networkApi.setRequestHeaders(headers, serverUrls, token);
|
|
if (Platform.isIOS) {
|
|
await init();
|
|
}
|
|
}
|
|
|
|
// ignore: avoid-unused-parameters
|
|
static Future<WebSocket> createWebSocket(Uri uri, {Map<String, String>? headers, Iterable<String>? protocols}) {
|
|
if (Platform.isIOS) {
|
|
final session = URLSession.fromRawPointer(_clientPointer!.cast());
|
|
return CupertinoWebSocket.connectWithSession(session, uri, protocols: protocols);
|
|
} else {
|
|
return OkHttpWebSocket.connectFromJniGlobalRef(_clientPointer!, uri, protocols: protocols);
|
|
}
|
|
}
|
|
|
|
const NetworkRepository();
|
|
|
|
/// Returns a shared HTTP client that uses native SSL configuration.
|
|
///
|
|
/// On iOS: Uses SharedURLSessionManager's URLSession.
|
|
/// On Android: Uses SharedHttpClientManager's OkHttpClient.
|
|
///
|
|
/// Must call [init] before using this method.
|
|
static http.Client get client => _client!;
|
|
}
|