init method

Future init()

Implementation

Future init() async {
  if (_initProgress) return;
  _initProgress = true;
  final Directory cookieDir = await _getCookiePath();
  _cookieJar = PersistCookieJar(storage: FileStorage(cookieDir.path));
  _interceptors.add(CookieManager(_cookieJar));

  var device = '';
  try {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    addHeader('Origin',
        'appwrite-${Platform.operatingSystem}://${packageInfo.packageName}');

    //creating custom user agent
    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      final andinfo = await deviceInfoPlugin.androidInfo;
      device =
          '(Linux; U; Android ${andinfo.version.release}; ${andinfo.brand} ${andinfo.model})';
    }
    if (Platform.isIOS) {
      final iosinfo = await deviceInfoPlugin.iosInfo;
      device = '${iosinfo.utsname.machine} iOS/${iosinfo.systemVersion}';
    }
    if (Platform.isLinux) {
      final lininfo = await deviceInfoPlugin.linuxInfo;
      device = '(Linux; U; ${lininfo.id} ${lininfo.version})';
    }
    if (Platform.isWindows) {
      final wininfo = await deviceInfoPlugin.windowsInfo;
      device =
          '(Windows NT; ${wininfo.computerName})'; //can't seem to get much info here
    }
    if (Platform.isMacOS) {
      final macinfo = await deviceInfoPlugin.macOsInfo;
      device = '(Macintosh; ${macinfo.model})';
    }
    addHeader('user-agent',
        '${packageInfo.packageName}/${packageInfo.version} $device');
  } catch (e) {
    debugPrint('Error getting device info: $e');
    device = Platform.operatingSystem;
    addHeader('user-agent', '$device');
  }

  _initialized = true;
  _initProgress = false;
}