Files
SigHej/app/lib/services/api_service.dart

44 lines
1.4 KiB
Dart
Raw Permalink Normal View History

2026-05-12 18:21:25 +02:00
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:sighej/models/match_result.dart';
const _baseUrl = String.fromEnvironment('API_URL', defaultValue: 'http://10.0.2.2:8000');
/// Registers the user's profile with the backend session store.
Future<void> registerSession(
String bleToken,
List<String> interests, {
String name = '',
String tagline = '',
}) async {
final response = await http.post(
Uri.parse('$_baseUrl/session'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'ble_token': bleToken,
'interests': interests,
if (name.isNotEmpty) 'name': name,
if (tagline.isNotEmpty) 'tagline': tagline,
}),
);
if (response.statusCode != 200) {
throw Exception('Failed to register session: ${response.statusCode}');
}
}
/// Reports a detected BLE token to the backend and returns the match result.
/// Returns null if the backend returns a non-200 status (soft failure).
Future<MatchResult?> reportMatch(String ownToken, String detectedToken) async {
try {
final response = await http.post(
Uri.parse('$_baseUrl/match'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'own_token': ownToken, 'detected_token': detectedToken}),
);
if (response.statusCode != 200) return null;
return MatchResult.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
} catch (_) {
return null;
}
}