Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Update NetworkClient when configuration changes

vishnukvmd37c4295df928
diff --git a/mobile/lib/core/network/ente_interceptor.dart b/mobile/lib/core/network/ente_interceptor.dart
index 5eb8d16d51..676fe94cb3 100644
--- a/mobile/lib/core/network/ente_interceptor.dart
+++ b/mobile/lib/core/network/ente_interceptor.dart
@@ -1,14 +1,12 @@
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:photos/core/configuration.dart';
-import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
class EnteRequestInterceptor extends Interceptor {
- final SharedPreferences _preferences;
final String enteEndpoint;
- EnteRequestInterceptor(this._preferences, this.enteEndpoint);
+ EnteRequestInterceptor(this.enteEndpoint);
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
@@ -20,7 +18,7 @@ class EnteRequestInterceptor extends Interceptor {
}
// ignore: prefer_const_constructors
options.headers.putIfAbsent("x-request-id", () => Uuid().v4().toString());
- final String? tokenValue = _preferences.getString(Configuration.tokenKey);
+ final String? tokenValue = Configuration.instance.getToken();
if (tokenValue != null) {
options.headers.putIfAbsent("X-Auth-Token", () => tokenValue);
}
diff --git a/mobile/lib/core/network/network.dart b/mobile/lib/core/network/network.dart
index b3930ddafe..076fb2584b 100644
--- a/mobile/lib/core/network/network.dart
+++ b/mobile/lib/core/network/network.dart
@@ -3,26 +3,21 @@ import 'dart:io';
import 'package:dio/dio.dart';
import 'package:fk_user_agent/fk_user_agent.dart';
import 'package:package_info_plus/package_info_plus.dart';
-import 'package:photos/core/constants.dart';
+import "package:photos/core/configuration.dart";
+import "package:photos/core/event_bus.dart";
import 'package:photos/core/network/ente_interceptor.dart';
-import 'package:shared_preferences/shared_preferences.dart';
+import "package:photos/events/endpoint_updated_event.dart";
int kConnectTimeout = 15000;
class NetworkClient {
- // apiEndpoint points to the Ente server's API endpoint
- static const apiEndpoint = String.fromEnvironment(
- "endpoint",
- defaultValue: kDefaultProductionEndpoint,
- );
-
late Dio _dio;
late Dio _enteDio;
Future<void> init() async {
await FkUserAgent.init();
final packageInfo = await PackageInfo.fromPlatform();
- final preferences = await SharedPreferences.getInstance();
+ final endpoint = Configuration.instance.getHttpEndpoint();
_dio = Dio(
BaseOptions(
connectTimeout: kConnectTimeout,
@@ -35,7 +30,7 @@ class NetworkClient {
);
_enteDio = Dio(
BaseOptions(
- baseUrl: apiEndpoint,
+ baseUrl: endpoint,
connectTimeout: kConnectTimeout,
headers: {
HttpHeaders.userAgentHeader: FkUserAgent.userAgent,
@@ -44,7 +39,18 @@ class NetworkClient {
},
),
);
- _enteDio.interceptors.add(EnteRequestInterceptor(preferences, apiEndpoint));
+ _setupInterceptors(endpoint);
+
+ Bus.instance.on<EndpointUpdatedEvent>().listen((event) {
+ final endpoint = Configuration.instance.getHttpEndpoint();
+ _enteDio.options.baseUrl = endpoint;
+ _setupInterceptors(endpoint);
+ });
+ }
+
+ void _setupInterceptors(String endpoint) {
+ _enteDio.interceptors.clear();
+ _enteDio.interceptors.add(EnteRequestInterceptor(endpoint));
}
NetworkClient._privateConstructor();