feat(frontend): simulate authenticated user sessions
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import "dart:convert";
|
||||
import "dart:typed_data";
|
||||
import "package:http/http.dart" as http;
|
||||
import "auth_service.dart";
|
||||
|
||||
/// Erreur levée par le client API (serveur injoignable ou réponse en échec).
|
||||
class ApiException implements Exception {
|
||||
@@ -24,27 +25,19 @@ class ApiFile {
|
||||
final String contentType;
|
||||
}
|
||||
|
||||
/// Point d'accès unique au backend : centralise l'URL de base, l'en-tête
|
||||
/// d'authentification (simulée) et la gestion des erreurs.
|
||||
/// Point d'accès unique au backend : centralise l'URL, l'authentification et
|
||||
/// la gestion des erreurs.
|
||||
class ApiClient {
|
||||
ApiClient._();
|
||||
|
||||
static const String _baseUrl = "http://localhost:3000/api";
|
||||
static const String _utilisateurEmail = "lucas.martin@ensitech.eu";
|
||||
static const String responsableEmail = "karim.benali@ensup.eu";
|
||||
|
||||
static Future<dynamic> get(
|
||||
String chemin, {
|
||||
String utilisateurEmail = _utilisateurEmail,
|
||||
}) async {
|
||||
static Future<dynamic> get(String chemin) async {
|
||||
final uri = Uri.parse("$_baseUrl$chemin");
|
||||
|
||||
late final http.Response reponse;
|
||||
try {
|
||||
reponse = await http.get(
|
||||
uri,
|
||||
headers: {"x-user-email": utilisateurEmail},
|
||||
);
|
||||
reponse = await http.get(uri, headers: AuthService.requestHeaders());
|
||||
} catch (_) {
|
||||
throw const ApiException(
|
||||
"Impossible de joindre le serveur. Vérifiez que le backend est démarré.",
|
||||
@@ -60,16 +53,12 @@ class ApiClient {
|
||||
static Future<ApiFile> getFile(
|
||||
String chemin, {
|
||||
required String fallbackFileName,
|
||||
String utilisateurEmail = _utilisateurEmail,
|
||||
}) async {
|
||||
final uri = Uri.parse("$_baseUrl$chemin");
|
||||
|
||||
late final http.Response reponse;
|
||||
try {
|
||||
reponse = await http.get(
|
||||
uri,
|
||||
headers: {"x-user-email": utilisateurEmail},
|
||||
);
|
||||
reponse = await http.get(uri, headers: AuthService.requestHeaders());
|
||||
} catch (_) {
|
||||
throw const ApiException(
|
||||
"Impossible de joindre le serveur. Vérifiez que le backend est démarré.",
|
||||
@@ -88,11 +77,7 @@ class ApiClient {
|
||||
);
|
||||
}
|
||||
|
||||
static Future<dynamic> post(
|
||||
String chemin,
|
||||
Map<String, dynamic> body, {
|
||||
String utilisateurEmail = _utilisateurEmail,
|
||||
}) async {
|
||||
static Future<dynamic> post(String chemin, Map<String, dynamic> body) async {
|
||||
final uri = Uri.parse("$_baseUrl$chemin");
|
||||
|
||||
late final http.Response reponse;
|
||||
@@ -100,8 +85,8 @@ class ApiClient {
|
||||
reponse = await http.post(
|
||||
uri,
|
||||
headers: {
|
||||
...AuthService.requestHeaders(),
|
||||
"content-type": "application/json",
|
||||
"x-user-email": utilisateurEmail,
|
||||
},
|
||||
body: jsonEncode(body),
|
||||
);
|
||||
@@ -117,11 +102,7 @@ class ApiClient {
|
||||
throw ApiException(_messageErreur(reponse));
|
||||
}
|
||||
|
||||
static Future<dynamic> patch(
|
||||
String chemin,
|
||||
Map<String, dynamic> body, {
|
||||
String utilisateurEmail = _utilisateurEmail,
|
||||
}) async {
|
||||
static Future<dynamic> patch(String chemin, Map<String, dynamic> body) async {
|
||||
final uri = Uri.parse("$_baseUrl$chemin");
|
||||
|
||||
late final http.Response reponse;
|
||||
@@ -129,8 +110,8 @@ class ApiClient {
|
||||
reponse = await http.patch(
|
||||
uri,
|
||||
headers: {
|
||||
...AuthService.requestHeaders(),
|
||||
"content-type": "application/json",
|
||||
"x-user-email": utilisateurEmail,
|
||||
},
|
||||
body: jsonEncode(body),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user