feat: add ApiService for HTTP POST
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
31
lib/services/api_service.dart
Normal file
31
lib/services/api_service.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class ApiService {
|
||||
final http.Client _client;
|
||||
|
||||
ApiService({http.Client? client}) : _client = client ?? http.Client();
|
||||
|
||||
Future<bool> sendScanData(
|
||||
String url, {
|
||||
required String barcode,
|
||||
required String codeType,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _client
|
||||
.post(
|
||||
Uri.parse(url),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({
|
||||
'barcode': barcode,
|
||||
'code_type': codeType,
|
||||
'timestamp': DateTime.now().toUtc().toIso8601String(),
|
||||
}),
|
||||
)
|
||||
.timeout(const Duration(seconds: 5));
|
||||
return response.statusCode >= 200 && response.statusCode < 300;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user