// Fiskon Relay Terminal SDK for Dart / Flutter. Depends on package:http (add `http: ^1.2.0` to pubspec.yaml). // // final relay = Relay(apiKey: 'YOUR_SECRET_KEY'); // servers / trusted code only // final device = Relay(publicKey: 'pk_...'); // safe inside your Flutter app // await device.registerDevice({'token': fcmToken, 'platform': 'android', 'external_id': 'user-42'}); import 'dart:async'; import 'dart:convert'; import 'package:http/http.dart' as http; const String defaultBase = 'https://relay.naweriindustries.com/api/v1'; class RelayException implements Exception { final String message; final String? code; final int status; final dynamic data; RelayException(this.message, {this.code, this.status = 0, this.data}); @override String toString() => 'RelayException($code, $status): $message'; } class Relay { final String? apiKey; final String? publicKey; final String baseUrl; final Duration timeout; final http.Client _http; Relay({this.apiKey, this.publicKey, String baseUrl = defaultBase, this.timeout = const Duration(seconds: 30), http.Client? client}) : baseUrl = baseUrl.endsWith('/') ? baseUrl.substring(0, baseUrl.length - 1) : baseUrl, _http = client ?? http.Client() { if (apiKey == null && publicKey == null) throw ArgumentError('Provide an apiKey or a publicKey'); } Future> _request(String method, String path, [Map? body, String? idempotencyKey, bool client = false]) async { final headers = {'Accept': 'application/json'}; if (client && publicKey != null && apiKey == null) { headers['X-Public-Key'] = publicKey!; } else if (apiKey != null) { headers['Authorization'] = 'Bearer $apiKey'; } else { throw RelayException('This call needs the secret apiKey', code: 'no_key'); } if (idempotencyKey != null) headers['Idempotency-Key'] = idempotencyKey; final req = http.Request(method, Uri.parse(baseUrl + path))..headers.addAll(headers); if (body != null) { req.headers['Content-Type'] = 'application/json'; req.body = jsonEncode(body); } final res = await http.Response.fromStream(await _http.send(req).timeout(timeout)); Map json; try { json = jsonDecode(res.body) as Map; } catch (_) { json = {'error': 1, 'code': 'bad_response', 'message': 'HTTP ${res.statusCode}'}; } if (res.statusCode >= 400 || json['error'] == 1) { throw RelayException(json['message']?.toString() ?? 'Request failed', code: json['code']?.toString(), status: res.statusCode, data: json['data']); } return json; } Future> sendEmail(Map p, {String? idempotencyKey}) => _request('POST', '/email', p, idempotencyKey); Future> sendSms(Map p, {String? idempotencyKey}) => _request('POST', '/sms', p, idempotencyKey); Future> sendWhatsapp(Map p, {String? idempotencyKey}) => _request('POST', '/whatsapp', p, idempotencyKey); Future> sendPush(Map p, {String? idempotencyKey}) => _request('POST', '/push', p, idempotencyKey); Future> registerDevice(Map p) => _request('POST', '/push/devices', p, null, true); Future> removeDevice(Map p) => _request('DELETE', '/push/devices', p, null, true); Future> sendTransactional(Map p, {String? idempotencyKey}) => _request('POST', '/transactional/email', p, idempotencyKey); Future> templates() => _request('GET', '/templates'); Future> sendOtp(Map p) => _request('POST', '/otp', p); Future> verifyOtp(Map p) => _request('POST', '/otp/verify', p); Future> balance() => _request('GET', '/balance'); Future> status(String channel, int id) => _request('GET', '/messages/$channel/$id'); }