/// Smart Event Network — Flutter / Dart SDK (v1) /// /// Add to pubspec.yaml: `http: ^1.2.0` /// /// ```dart /// final sen = SmartEventClient(apiKey: 'sen_live_...'); /// final events = await sen.listEvents(); /// final result = await sen.scan(eventId: '...', token: 'SEN1....'); /// ``` library smart_event_card; import 'dart:convert'; import 'package:http/http.dart' as http; class SmartEventApiException implements Exception { final String message; final int status; final dynamic body; SmartEventApiException(this.message, this.status, this.body); @override String toString() => 'SmartEventApiException($status): $message'; } class SmartEventClient { final String apiKey; final String baseUrl; final http.Client _client; SmartEventClient({ required this.apiKey, this.baseUrl = 'https://smarteventnetwork.com/api/public/v1', http.Client? client, }) : _client = client ?? http.Client() { if (!apiKey.startsWith('sen_live_')) { throw ArgumentError('apiKey must start with sen_live_'); } } Map get _headers => { 'authorization': 'Bearer $apiKey', 'content-type': 'application/json', 'accept': 'application/json', }; Future _request(String method, String path, [Map? body]) async { final uri = Uri.parse('$baseUrl$path'); final res = await (method == 'GET' ? _client.get(uri, headers: _headers) : _client.post(uri, headers: _headers, body: body != null ? jsonEncode(body) : null)); Map json; try { json = res.body.isEmpty ? {} : jsonDecode(res.body) as Map; } catch (_) { throw SmartEventApiException('Invalid JSON', res.statusCode, res.body); } if (res.statusCode >= 400) { throw SmartEventApiException( json['error']?.toString() ?? 'HTTP ${res.statusCode}', res.statusCode, json); } return json['data'] ?? json; } Future> listEvents() async => (await _request('GET', '/events')) as List; Future> getEvent(String id) async => (await _request('GET', '/events/$id')) as Map; Future> verifyCard(String token) async => (await _request('GET', '/card/verify?token=${Uri.encodeComponent(token)}')) as Map; Future> scan({ required String eventId, String? cardNumber, String? token, }) async { final body = {'event_id': eventId}; if (cardNumber != null) body['card_number'] = cardNumber; if (token != null) body['token'] = token; return (await _request('POST', '/scan', body)) as Map; } void close() => _client.close(); }