Skip to content

armgit / ente

Public Imported from GitHub
0

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

Make sure Network uses the latest network configuration

vishnukvmda45129b75bb7
diff --git a/auth/lib/core/network.dart b/auth/lib/core/network.dart
index b9fba1cd8c..1942fcbb83 100644
--- a/auth/lib/core/network.dart
+++ b/auth/lib/core/network.dart
@@ -2,28 +2,24 @@ import 'dart:io';
import 'package:dio/dio.dart';
import 'package:ente_auth/core/configuration.dart';
-import 'package:ente_auth/core/constants.dart';
+import 'package:ente_auth/core/event_bus.dart';
+import 'package:ente_auth/events/endpoint_updated_event.dart';
import 'package:fk_user_agent/fk_user_agent.dart';
import 'package:flutter/foundation.dart';
import 'package:package_info_plus/package_info_plus.dart';
-import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
int kConnectTimeout = 15000;
class Network {
- // 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,
@@ -34,10 +30,10 @@ class Network {
},
),
);
- _dio.interceptors.add(RequestIdInterceptor());
+
_enteDio = Dio(
BaseOptions(
- baseUrl: apiEndpoint,
+ baseUrl: endpoint,
connectTimeout: kConnectTimeout,
headers: {
HttpHeaders.userAgentHeader: FkUserAgent.userAgent,
@@ -46,7 +42,13 @@ class Network {
},
),
);
- _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);
+ });
}
Network._privateConstructor();
@@ -55,34 +57,41 @@ class Network {
Dio getDio() => _dio;
Dio get enteDio => _enteDio;
+
+ void _setupInterceptors(String endpoint) {
+ _dio.interceptors.clear();
+ _dio.interceptors.add(RequestIdInterceptor());
+
+ _enteDio.interceptors.clear();
+ _enteDio.interceptors.add(EnteRequestInterceptor(endpoint));
+ }
}
class RequestIdInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
- // ignore: prefer_const_constructors
- options.headers.putIfAbsent("x-request-id", () => Uuid().v4().toString());
+ options.headers
+ .putIfAbsent("x-request-id", () => const Uuid().v4().toString());
return super.onRequest(options, handler);
}
}
class EnteRequestInterceptor extends Interceptor {
- final SharedPreferences _preferences;
- final String enteEndpoint;
+ final String endpoint;
- EnteRequestInterceptor(this._preferences, this.enteEndpoint);
+ EnteRequestInterceptor(this.endpoint);
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
if (kDebugMode) {
assert(
- options.baseUrl == enteEndpoint,
+ options.baseUrl == endpoint,
"interceptor should only be used for API endpoint",
);
}
- // ignore: prefer_const_constructors
- options.headers.putIfAbsent("x-request-id", () => Uuid().v4().toString());
- final String? tokenValue = _preferences.getString(Configuration.tokenKey);
+ options.headers
+ .putIfAbsent("x-request-id", () => const Uuid().v4().toString());
+ final String? tokenValue = Configuration.instance.getToken();
if (tokenValue != null) {
options.headers.putIfAbsent("X-Auth-Token", () => tokenValue);
}