Files
geo_front/lib/services/share_service.dart
T

73 lines
2.0 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) {
final data = jsonDecode(response.body);
return {
'geo_id': data['geo_id'],
'share_id': data['share_id'],
};
} else {
throw Exception('Failed to create share link');
}
}
Future<Map<String, dynamic>> getPosition(String token, String uniqueId) async {
final response = await _client.get(
Uri.parse('${ApiConfig.watchUrl}?unique_id=$uniqueId'),
headers: {
'Authorization': 'Bearer $token',
},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
return {
'id': data['id'],
'x': data['x'],
'y': data['y'],
'created_at': data['created_at'],
'expires_at': data['expires_at'],
};
} else {
throw Exception('Share link not found or no position available');
}
}
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);
return {
'x': data['x'],
'y': data['y'],
'created_at': data['created_at'],
'expires_at': data['expires_at'],
};
} else {
throw Exception('Share link not found or no position available');
}
}
}