From f58d439ab3e078fb8d687a619a464aaadc585334 Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Wed, 13 Dec 2023 16:40:11 +0000 Subject: [PATCH] core: Determine User-Agent using package_info_plus package --- lib/api/core.dart | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/api/core.dart b/lib/api/core.dart index cb520167fe..c6867cd8bc 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,8 +71,24 @@ class ApiConnection { } } - void addUserAgent(http.BaseRequest request) { - request.headers['User-Agent'] = 'ZulipMobile/?.?.? (Android 14)'; + Future addUserAgent(http.BaseRequest request) async { + final packageInfo = await PackageInfo.fromPlatform(); + final String applicationVersion = packageInfo.version; + final String systemName; + final String systemVersion; + final deviceInfo = await DeviceInfoPlugin().deviceInfo; + switch (deviceInfo) { + case IosDeviceInfo(): + systemName = deviceInfo.systemName; + systemVersion = deviceInfo.systemVersion; + case AndroidDeviceInfo(): + systemName = "Android"; + systemVersion = deviceInfo.version.release; + default: + systemName = "Desktop"; + systemVersion = "?"; + } + request.headers['User-Agent'] = 'ZulipMobile/$applicationVersion ($systemName $systemVersion)'; } final http.Client _client; @@ -84,7 +102,7 @@ class ApiConnection { assert(debugLog("${request.method} ${request.url}")); addAuth(request); - addUserAgent(request); + await addUserAgent(request); final http.StreamedResponse response; try {