mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2026-02-16 14:29:35 +00:00
enhancement: 测试
This commit is contained in:
parent
6efa41e035
commit
7d3e22afb7
9 changed files with 95 additions and 48 deletions
|
|
@ -3,26 +3,26 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:http/http.dart';
|
||||
// import 'package:http_cache_client/http_cache_client.dart';
|
||||
// import 'package:http_cache_core/http_cache_core.dart';
|
||||
// import 'package:http_cache_isar_store/http_cache_isar_store.dart';
|
||||
// import 'package:http_cache_hive_store/http_cache_hive_store.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:shelfsdk/audiobookshelf_api.dart';
|
||||
import 'package:vaani/db/cache/cache_key.dart';
|
||||
import 'package:vaani/db/cache_manager.dart';
|
||||
import 'package:vaani/shared/utils/error_response.dart';
|
||||
import 'package:vaani/features/settings/api_settings_provider.dart';
|
||||
import 'package:vaani/features/settings/models/authenticated_user.dart';
|
||||
import 'package:vaani/shared/extensions/obfuscation.dart';
|
||||
import 'package:vaani/shared/utils/error_response.dart';
|
||||
|
||||
part 'api_provider.g.dart';
|
||||
|
||||
// TODO: workaround for https://github.com/rrousselGit/riverpod/issues/3718
|
||||
typedef ResponseErrorHandler = void Function(
|
||||
Response response, [
|
||||
Object? error,
|
||||
]);
|
||||
// // TODO: workaround for https://github.com/rrousselGit/riverpod/issues/3718
|
||||
// typedef ResponseErrorHandler<T> = void Function(
|
||||
// T response, [
|
||||
// Object? error,
|
||||
// ]);
|
||||
|
||||
final _logger = Logger('api_provider');
|
||||
|
||||
|
|
@ -39,15 +39,17 @@ Uri makeBaseUrl(String address) {
|
|||
// Global options
|
||||
// final options = CacheOptions(
|
||||
// // A default store is required for the client.
|
||||
// store: IsarCacheStore("", name: "http_cache"),
|
||||
// store: HiveCacheStore(appDocumentsDir.path, hiveBoxName: "http_cache"),
|
||||
|
||||
// // All subsequent fields are optional to get a standard behaviour.
|
||||
|
||||
// // Default.
|
||||
// policy: CachePolicy.request,
|
||||
// // 对于给定的状态代码,返回之前缓存的错误响应。
|
||||
// // Returns a previous cached response on error for given status codes.
|
||||
// // Defaults to `[]`.
|
||||
// hitCacheOnErrorCodes: const [500],
|
||||
// // 允许在网络错误(例如脱机使用)时返回缓存响应。
|
||||
// // Allows to return a cached response on network errors (e.g. offline usage).
|
||||
// // Defaults to `false`.
|
||||
// hitCacheOnNetworkFailure: true,
|
||||
|
|
@ -72,9 +74,8 @@ AudiobookshelfApi audiobookshelfApi(Ref ref, Uri? baseUrl) {
|
|||
// try to get the base url from app settings
|
||||
final apiSettings = ref.watch(apiSettingsProvider);
|
||||
baseUrl ??= apiSettings.activeServer?.serverUrl;
|
||||
return AudiobookshelfApi(
|
||||
return DioAudiobookshelfApi(
|
||||
baseUrl: makeBaseUrl(baseUrl.toString()),
|
||||
// client:
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -88,9 +89,10 @@ AudiobookshelfApi authenticatedApi(Ref ref) {
|
|||
_logger.severe('No active user can not provide authenticated api');
|
||||
throw StateError('No active user');
|
||||
}
|
||||
return AudiobookshelfApi(
|
||||
return DioAudiobookshelfApi(
|
||||
baseUrl: makeBaseUrl(user.server.serverUrl.toString()),
|
||||
token: user.authToken,
|
||||
// client: CacheClient(Client(), options: options),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -102,7 +104,7 @@ FutureOr<bool> isServerAlive(Ref ref, String address) async {
|
|||
}
|
||||
|
||||
try {
|
||||
return await AudiobookshelfApi(baseUrl: makeBaseUrl(address))
|
||||
return await DioAudiobookshelfApi(baseUrl: makeBaseUrl(address))
|
||||
.server
|
||||
.ping() ??
|
||||
false;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ part of 'api_provider.dart';
|
|||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$audiobookshelfApiHash() => r'f23a06c404e11867a7f796877eaca99b8ff25458';
|
||||
String _$audiobookshelfApiHash() => r'd7fbddf9ce2b463468c8d4db5a1bc4a53b7b7278';
|
||||
|
||||
/// Copied from Dart SDK
|
||||
class _SystemHash {
|
||||
|
|
@ -170,7 +170,7 @@ class _AudiobookshelfApiProviderElement
|
|||
Uri? get baseUrl => (origin as AudiobookshelfApiProvider).baseUrl;
|
||||
}
|
||||
|
||||
String _$authenticatedApiHash() => r'284be2c39823c20fb70035a136c430862c28fa27';
|
||||
String _$authenticatedApiHash() => r'13bba42fa712f173d3b72761ae9d544854df26d0';
|
||||
|
||||
/// get the api instance for the authenticated user
|
||||
///
|
||||
|
|
@ -191,7 +191,7 @@ final authenticatedApiProvider = Provider<AudiobookshelfApi>.internal(
|
|||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
typedef AuthenticatedApiRef = ProviderRef<AudiobookshelfApi>;
|
||||
String _$isServerAliveHash() => r'bb3a53cae1eb64b8760a56864feed47b7a3f1c29';
|
||||
String _$isServerAliveHash() => r'3afd608ced03a23fa7300d4a59368d170406ecc8';
|
||||
|
||||
/// ping the server to check if it is reachable
|
||||
///
|
||||
|
|
@ -355,7 +355,7 @@ class ServerStatusFamily extends Family<AsyncValue<ServerStatusResponse?>> {
|
|||
/// Copied from [serverStatus].
|
||||
ServerStatusProvider call(
|
||||
Uri baseUrl, [
|
||||
void Function(Response, [Object?])? responseErrorHandler,
|
||||
void Function(ApiResponse, [Object?])? responseErrorHandler,
|
||||
]) {
|
||||
return ServerStatusProvider(
|
||||
baseUrl,
|
||||
|
|
@ -398,7 +398,7 @@ class ServerStatusProvider
|
|||
/// Copied from [serverStatus].
|
||||
ServerStatusProvider(
|
||||
Uri baseUrl, [
|
||||
void Function(Response, [Object?])? responseErrorHandler,
|
||||
void Function(ApiResponse, [Object?])? responseErrorHandler,
|
||||
]) : this._internal(
|
||||
(ref) => serverStatus(
|
||||
ref as ServerStatusRef,
|
||||
|
|
@ -430,7 +430,7 @@ class ServerStatusProvider
|
|||
}) : super.internal();
|
||||
|
||||
final Uri baseUrl;
|
||||
final void Function(Response, [Object?])? responseErrorHandler;
|
||||
final void Function(ApiResponse, [Object?])? responseErrorHandler;
|
||||
|
||||
@override
|
||||
Override overrideWith(
|
||||
|
|
@ -480,7 +480,7 @@ mixin ServerStatusRef on AutoDisposeFutureProviderRef<ServerStatusResponse?> {
|
|||
Uri get baseUrl;
|
||||
|
||||
/// The parameter `responseErrorHandler` of this provider.
|
||||
void Function(Response, [Object?])? get responseErrorHandler;
|
||||
void Function(ApiResponse, [Object?])? get responseErrorHandler;
|
||||
}
|
||||
|
||||
class _ServerStatusProviderElement
|
||||
|
|
@ -491,7 +491,7 @@ class _ServerStatusProviderElement
|
|||
@override
|
||||
Uri get baseUrl => (origin as ServerStatusProvider).baseUrl;
|
||||
@override
|
||||
void Function(Response, [Object?])? get responseErrorHandler =>
|
||||
void Function(ApiResponse, [Object?])? get responseErrorHandler =>
|
||||
(origin as ServerStatusProvider).responseErrorHandler;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue