Geschlecht und Merkmal
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
/// Lightweight API-Client mit einfachem 401-Retry via Refresh-Token.
|
||||
class ApiClient {
|
||||
ApiClient(
|
||||
this.baseUrl, {
|
||||
this.getAccessToken,
|
||||
this.onUnauthorized, // z.B. () => tokenStorage.refreshAccess()
|
||||
this.onUnauthorized,
|
||||
});
|
||||
|
||||
final String baseUrl; // z.B. https://api.windesign.at/hedgehogs.php?r=
|
||||
@@ -38,9 +37,8 @@ class ApiClient {
|
||||
|
||||
Future<T> _requestWithRetry<T>(String method, String path,
|
||||
{Object? body}) async {
|
||||
http.Response res = await _send(method, path, body: body);
|
||||
var res = await _send(method, path, body: body);
|
||||
|
||||
// Bei 401 einmal Refresh versuchen
|
||||
if (res.statusCode == 401 && onUnauthorized != null) {
|
||||
await onUnauthorized!.call();
|
||||
res = await _send(method, path, body: body);
|
||||
@@ -49,9 +47,8 @@ class ApiClient {
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||
if (res.body.isEmpty) return (null as T);
|
||||
final decoded = jsonDecode(res.body);
|
||||
return decoded as T; // Aufrufer achtet auf Typ (Map/List)
|
||||
return decoded as T;
|
||||
}
|
||||
|
||||
throw ApiException(res.statusCode, res.body);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user