diff --git a/lib/api/core.dart b/lib/api/core.dart index b28da078b81..e86c78b2aea 100644 --- a/lib/api/core.dart +++ b/lib/api/core.dart @@ -1,7 +1,9 @@ import 'dart:convert'; import 'dart:io'; +import 'package:device_info_plus/device_info_plus.dart'; import 'package:http/http.dart' as http; +import 'package:package_info_plus/package_info_plus.dart'; import '../log.dart'; import '../model/localizations.dart'; @@ -69,6 +71,28 @@ class ApiConnection { } } + Future addUserAgent(http.BaseRequest request) async { + final packageInfo = await PackageInfo.fromPlatform(); + final deviceInfoPlugin = DeviceInfoPlugin(); + final String applicationVersion = packageInfo.version; + final String systemName; + final String systemVersion; + if (Platform.isIOS) { + final deviceInfo = await deviceInfoPlugin.iosInfo; + systemName = deviceInfo.systemName; + systemVersion = deviceInfo.systemVersion; + } else if (Platform.isAndroid) { + final deviceInfo = await deviceInfoPlugin.androidInfo; + systemName = "Android"; + systemVersion = deviceInfo.version.release; + } else { + systemName = "Desktop"; + systemVersion = "?"; + } + print('ZulipMobile/$applicationVersion ($systemName $systemVersion)'); + request.headers['User-Agent'] = 'ZulipMobile/$applicationVersion ($systemName $systemVersion)'; + } + final http.Client _client; bool _isOpen = true; @@ -80,6 +104,7 @@ class ApiConnection { assert(debugLog("${request.method} ${request.url}")); addAuth(request); + await addUserAgent(request); final http.StreamedResponse response; try {