selectable suggestion provider
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:exceptions/exceptions.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:weblibre/core/http_error_handler.dart';
|
||||
import 'package:weblibre/features/search/domain/entities/abstract/i_search_suggestion_provider.dart';
|
||||
|
||||
part 'brave.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class BraveAutosuggestService extends _$BraveAutosuggestService
|
||||
implements ISearchSuggestionProvider {
|
||||
static final _baseUrl = Uri.https('search.brave.com', 'api/suggest');
|
||||
|
||||
late http.Client _client;
|
||||
|
||||
@override
|
||||
void build() {
|
||||
_client = http.Client();
|
||||
|
||||
ref.onDispose(() {
|
||||
_client.close();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Result<List<String>>> getSuggestions(String query) {
|
||||
return Result.fromAsync(() async {
|
||||
final response = await _client
|
||||
.get(_baseUrl.replace(queryParameters: {'q': query}))
|
||||
.timeout(const Duration(seconds: 5));
|
||||
|
||||
final results = jsonDecode(utf8.decode(response.bodyBytes)) as List;
|
||||
return switch (results.last) {
|
||||
final String result => [result],
|
||||
final List resultList => resultList.cast(),
|
||||
_ => [],
|
||||
};
|
||||
}, exceptionHandler: handleHttpError);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'brave.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$braveAutosuggestServiceHash() =>
|
||||
r'3b2f465dcf4b44b0450975d3909fc7066d171bf2';
|
||||
|
||||
/// See also [BraveAutosuggestService].
|
||||
@ProviderFor(BraveAutosuggestService)
|
||||
final braveAutosuggestServiceProvider =
|
||||
NotifierProvider<BraveAutosuggestService, void>.internal(
|
||||
BraveAutosuggestService.new,
|
||||
name: r'braveAutosuggestServiceProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$braveAutosuggestServiceHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$BraveAutosuggestService = Notifier<void>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:exceptions/exceptions.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:weblibre/core/http_error_handler.dart';
|
||||
import 'package:weblibre/features/search/domain/entities/abstract/i_search_suggestion_provider.dart';
|
||||
|
||||
part 'duckduckgo.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class DuckDuckGoAutosuggestService extends _$DuckDuckGoAutosuggestService
|
||||
implements ISearchSuggestionProvider {
|
||||
static final _baseUrl = Uri.https('duckduckgo.com', 'ac/');
|
||||
|
||||
late http.Client _client;
|
||||
|
||||
@override
|
||||
void build() {
|
||||
_client = http.Client();
|
||||
|
||||
ref.onDispose(() {
|
||||
_client.close();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Result<List<String>>> getSuggestions(String query) {
|
||||
return Result.fromAsync(() async {
|
||||
final response = await _client
|
||||
.get(_baseUrl.replace(queryParameters: {'type': 'list', 'q': query}))
|
||||
.timeout(const Duration(seconds: 5));
|
||||
|
||||
final results = jsonDecode(utf8.decode(response.bodyBytes)) as List;
|
||||
return switch (results.last) {
|
||||
final String result => [result],
|
||||
final List resultList => resultList.cast(),
|
||||
_ => [],
|
||||
};
|
||||
}, exceptionHandler: handleHttpError);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'duckduckgo.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$duckDuckGoAutosuggestServiceHash() =>
|
||||
r'1679e6d9bce7cb380b1ee60c768abe2fddb79478';
|
||||
|
||||
/// See also [DuckDuckGoAutosuggestService].
|
||||
@ProviderFor(DuckDuckGoAutosuggestService)
|
||||
final duckDuckGoAutosuggestServiceProvider =
|
||||
NotifierProvider<DuckDuckGoAutosuggestService, void>.internal(
|
||||
DuckDuckGoAutosuggestService.new,
|
||||
name: r'duckDuckGoAutosuggestServiceProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$duckDuckGoAutosuggestServiceHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$DuckDuckGoAutosuggestService = Notifier<void>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
||||
@@ -0,0 +1,46 @@
|
||||
// ignore_for_file: avoid_dynamic_calls
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:exceptions/exceptions.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:weblibre/core/http_error_handler.dart';
|
||||
import 'package:weblibre/features/search/domain/entities/abstract/i_search_suggestion_provider.dart';
|
||||
|
||||
part 'qwant.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class QwantAutosuggestService extends _$QwantAutosuggestService
|
||||
implements ISearchSuggestionProvider {
|
||||
static final _baseUrl = Uri.https('api.qwant.com', 'v3/suggest');
|
||||
|
||||
late http.Client _client;
|
||||
|
||||
@override
|
||||
void build() {
|
||||
_client = http.Client();
|
||||
|
||||
ref.onDispose(() {
|
||||
_client.close();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Result<List<String>>> getSuggestions(String query) {
|
||||
return Result.fromAsync(() async {
|
||||
final response = await _client
|
||||
.get(_baseUrl.replace(queryParameters: {'q': query}))
|
||||
.timeout(const Duration(seconds: 5));
|
||||
|
||||
final results =
|
||||
jsonDecode(utf8.decode(response.bodyBytes)) as Map<String, dynamic>;
|
||||
if (results['status'] != 'success') {
|
||||
throw Exception('qwant api error');
|
||||
}
|
||||
|
||||
final items = results['data']['items'] as List;
|
||||
return items.map((item) => item['value'] as String).toList();
|
||||
}, exceptionHandler: handleHttpError);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'qwant.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$qwantAutosuggestServiceHash() =>
|
||||
r'a942cd073289d20d43647de5c0f43fb08790045f';
|
||||
|
||||
/// See also [QwantAutosuggestService].
|
||||
@ProviderFor(QwantAutosuggestService)
|
||||
final qwantAutosuggestServiceProvider =
|
||||
NotifierProvider<QwantAutosuggestService, void>.internal(
|
||||
QwantAutosuggestService.new,
|
||||
name: r'qwantAutosuggestServiceProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$qwantAutosuggestServiceHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$QwantAutosuggestService = Notifier<void>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
||||
@@ -1,5 +1,17 @@
|
||||
import 'package:exceptions/exceptions.dart';
|
||||
|
||||
enum SearchSuggestionProviders {
|
||||
brave('Brave', 'brave'),
|
||||
ddg('DuckDuckGo', 'ddg'),
|
||||
kagi('Kagi', 'kagi'),
|
||||
qwant('Qwant', 'qwant');
|
||||
|
||||
final String label;
|
||||
final String relatedBang;
|
||||
|
||||
const SearchSuggestionProviders(this.label, this.relatedBang);
|
||||
}
|
||||
|
||||
abstract interface class ISearchSuggestionProvider {
|
||||
Future<Result<List<String>>> getSuggestions(String query);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user