diff --git a/mobile/lib/core/network/ente_interceptor.dart b/mobile/lib/core/network/ente_interceptor.dart index 5eb8d16d5..676fe94cb 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 b3930ddaf..076fb2584 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 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().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();