Files
geo_front/lib/services/share_service.dart
T
2026-05-15 19:09:37 +03:00

68 lines
1.9 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import '../config/api.dart';
class ShareService {
final http.Client _client;
ShareService({http.Client? client}) : _client = client ?? http.Client();
Future<Map<String, dynamic>> createShare(
String token,
double x,
double y,
) async {
final response = await _client.post(
Uri.parse(ApiConfig.shareUrl),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
body: jsonEncode({'x': x, 'y': y}),
);
if (response.statusCode == 201) {
try {
final data = jsonDecode(response.body) as Map<String, dynamic>;
return {
'geo_id': data['geo_id']?.toString() ?? '',
'share_id': data['share_id']?.toString() ?? '',
};
} catch (e) {
throw Exception(
'Failed to parse response: $e | Body: ${response.body.substring(0, response.body.length > 200 ? 200 : response.body.length)}',
);
}
} else {
final errorBody = response.body.length > 200
? response.body.substring(0, 200)
: response.body;
throw Exception(
'Failed to create share link: ${response.statusCode} - $errorBody',
);
}
}
Future<Map<String, dynamic>> getPositionByShareId(
String token,
String shareId,
) async {
final response = await _client.get(
Uri.parse('${ApiConfig.watchUrl}?share_id=$shareId'),
headers: {'Authorization': 'Bearer $token'},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
return {
'x': data['x'],
'y': data['y'],
'last_update': data['last_update']?.toString() ?? '',
'expires_at': data['expires_at']?.toString() ?? '',
};
} else {
throw Exception('Share link not found or no position available');
}
}
}