sendLogs static method

Future<void> sendLogs({
  1. required String sessionId,
  2. required String providerId,
  3. required String logType,
  4. String? applicationId,
})

Implementation

static Future<void> sendLogs({
  required String sessionId,
  required String providerId,
  required String logType,
  String? applicationId,
}) async {
  // Skip logging if not in production environment
  const bool isProduction = bool.fromEnvironment('dart.vm.product');
  if (!isProduction) {
    return;
  }

  try {
    // Get device information
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    String brand = '';
    String model = '';
    String deviceId = '';

    if (defaultTargetPlatform == TargetPlatform.android) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      brand = androidInfo.brand;
      model = androidInfo.model;
      deviceId = androidInfo.id;
    }
    if (defaultTargetPlatform == TargetPlatform.iOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      brand = 'Apple';
      model = iosInfo.utsname.machine;
      deviceId = iosInfo.identifierForVendor ?? '';
    }

    // Get public IP address
    String publicIpAddress = await getPublicIp();

    // Prepare the body of the POST request
    Map<String, dynamic> data = {
      'sessionId': sessionId,
      'date': DateTime.now().toIso8601String(),
      'deviceId': deviceId,
      'deviceType': '$brand $model',
      'providerId': providerId,
      'applicationId': applicationId ?? '',
      'publicIpAddress': publicIpAddress,
      'logType': logType,
    };

    // Send the POST request
    final dio = Dio();
    dio.httpClientAdapter = NativeAdapter(
        createCupertinoConfiguration: () =>
            URLSessionConfiguration.ephemeralSessionConfiguration());
    dio.options.headers['Content-Type'] = 'application/json';
    final response = await dio.post<String>(
      ReclaimBackend.LOGS_API,
      data: jsonEncode(data),
    );

    if (response.statusCode != 200) {
      print('Failed to Send logs');
    }
  } catch (error) {
    print('Error sending logs to backend server: $error');
  }
}