- search schmearch
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
@ -6,7 +7,9 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:f0ckapp/models/mediaitem_model.dart';
|
||||
import 'package:f0ckapp/models/suggestion_model.dart';
|
||||
|
||||
final FlutterSecureStorage storage = FlutterSecureStorage();
|
||||
final FlutterSecureStorage storage = const FlutterSecureStorage(
|
||||
aOptions: AndroidOptions(encryptedSharedPreferences: true),
|
||||
);
|
||||
|
||||
Future<List<MediaItem>> fetchMedia({
|
||||
int? older,
|
||||
@ -25,7 +28,7 @@ Future<List<MediaItem>> fetchMedia({
|
||||
},
|
||||
);
|
||||
|
||||
final response = await http.get(url);
|
||||
final http.Response response = await http.get(url);
|
||||
if (response.statusCode == 200) {
|
||||
final List<dynamic> jsonList = jsonDecode(response.body);
|
||||
return jsonList.map((item) => MediaItem.fromJson(item)).toList();
|
||||
@ -37,7 +40,7 @@ Future<List<MediaItem>> fetchMedia({
|
||||
Future<MediaItem> fetchMediaDetail(int itemId) async {
|
||||
final Uri url = Uri.parse('https://api.f0ck.me/item/${itemId.toString()}');
|
||||
|
||||
final response = await http.get(url);
|
||||
final http.Response response = await http.get(url);
|
||||
if (response.statusCode == 200) {
|
||||
final Map<String, dynamic> jsonResponse = jsonDecode(response.body);
|
||||
|
||||
@ -50,39 +53,45 @@ Future<MediaItem> fetchMediaDetail(int itemId) async {
|
||||
}
|
||||
|
||||
Future<List<Suggestion>> fetchSuggestions(String query) async {
|
||||
final Uri uri = Uri.parse(
|
||||
'https://f0ck.me/api/v2/admin/tags/suggest?q=$query',
|
||||
); // wip: new route in pyapi
|
||||
final response = await http.get(uri);
|
||||
final Uri uri = Uri.parse('https://api.f0ck.me/search/?q=$query');
|
||||
try {
|
||||
final http.Response response = await http
|
||||
.get(uri)
|
||||
.timeout(const Duration(seconds: 5));
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final Map<String, dynamic> decoded = jsonDecode(response.body);
|
||||
if (decoded['success'] == true && decoded.containsKey('suggestions')) {
|
||||
final List<dynamic> suggestionsList = decoded['suggestions'];
|
||||
return suggestionsList
|
||||
.map(
|
||||
(dynamic jsonItem) =>
|
||||
Suggestion.fromJson(jsonItem as Map<String, dynamic>),
|
||||
)
|
||||
.toList()
|
||||
..sort(
|
||||
(Suggestion a, Suggestion b) =>
|
||||
(b.score * b.tagged).compareTo(a.score * a.tagged),
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final dynamic decoded = jsonDecode(response.body);
|
||||
|
||||
if (decoded is List) {
|
||||
return decoded
|
||||
.map((item) => Suggestion.fromJson(item as Map<String, dynamic>))
|
||||
.toList()
|
||||
..sort((a, b) => b.score.compareTo(a.score));
|
||||
} else {
|
||||
throw Exception('Unerwartetes Format: Erwartet wurde eine Liste.');
|
||||
}
|
||||
} else if (response.statusCode == 400) {
|
||||
final dynamic error = jsonDecode(response.body);
|
||||
final String message = error is Map<String, dynamic>
|
||||
? error['detail']?.toString() ?? 'Unbekannter Fehler.'
|
||||
: 'Unbekannter Fehler.';
|
||||
throw Exception('Client-Fehler 400: $message');
|
||||
} else {
|
||||
throw Exception('Nichts gefunden.');
|
||||
throw Exception(
|
||||
'Fehler beim Abrufen der Vorschläge: ${response.statusCode}',
|
||||
);
|
||||
}
|
||||
} else {
|
||||
throw Exception(
|
||||
'Fehler beim Abrufen der Vorschläge: ${response.statusCode}',
|
||||
);
|
||||
} on TimeoutException {
|
||||
throw Exception('Anfrage an die API hat zu lange gedauert.');
|
||||
} catch (e) {
|
||||
throw Exception('Fehler beim Verarbeiten der Anfrage: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> login(String username, String password) async {
|
||||
final Uri url = Uri.parse('https://api.f0ck.me/login');
|
||||
|
||||
final response = await http.post(
|
||||
final http.Response response = await http.post(
|
||||
url,
|
||||
body: {'username': username, 'password': password},
|
||||
);
|
||||
|
Reference in New Issue
Block a user