69 lines
1.8 KiB
Dart
69 lines
1.8 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
import 'package:f0ckapp/models/MediaItem.dart';
|
|
|
|
final storage = FlutterSecureStorage();
|
|
|
|
Future<List<MediaItem>> fetchMedia({
|
|
int? older,
|
|
String? type,
|
|
int? mode,
|
|
bool? random,
|
|
String? tag,
|
|
}) async {
|
|
final Uri url = Uri.parse('https://api.f0ck.me/items/get').replace(
|
|
queryParameters: {
|
|
'type': type ?? 'image',
|
|
'mode': (mode ?? 0).toString(),
|
|
'random': (random! ? 1 : 0).toString(),
|
|
if (tag != null) 'tag': tag,
|
|
if (older != null) 'older': older.toString(),
|
|
},
|
|
);
|
|
|
|
final response = await http.get(url);
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> jsonList = jsonDecode(response.body);
|
|
return jsonList.map((item) => MediaItem.fromJson(item)).toList();
|
|
} else {
|
|
throw Exception('Fehler beim Abrufen der Medien: ${response.statusCode}');
|
|
}
|
|
}
|
|
|
|
Future<MediaItem> fetchMediaDetail(int itemId) async {
|
|
final Uri url = Uri.parse('https://api.f0ck.me/item/${itemId.toString()}');
|
|
|
|
final response = await http.get(url);
|
|
if (response.statusCode == 200) {
|
|
final Map<String, dynamic> jsonResponse = jsonDecode(response.body);
|
|
|
|
return MediaItem.fromJson(jsonResponse);
|
|
} else {
|
|
throw Exception(
|
|
'Fehler beim Abrufen der Media-Details: ${response.statusCode}',
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<bool> login(String username, String password) async {
|
|
final Uri url = Uri.parse('https://api.f0ck.me/login');
|
|
|
|
final response = await http.post(
|
|
url,
|
|
body: {'username': username, 'password': password},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
final token = data['token'];
|
|
|
|
await storage.write(key: "token", value: token);
|
|
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|