Skip to content

Commit

Permalink
core: Determine User-Agent using package_info_plus package
Browse files Browse the repository at this point in the history
  • Loading branch information
sirpengi committed Dec 13, 2023
1 parent 431015a commit f58d439
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/api/core.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -69,8 +71,24 @@ class ApiConnection {
}
}

void addUserAgent(http.BaseRequest request) {
request.headers['User-Agent'] = 'ZulipMobile/?.?.? (Android 14)';
Future<void> 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;
Expand All @@ -84,7 +102,7 @@ class ApiConnection {
assert(debugLog("${request.method} ${request.url}"));

addAuth(request);
addUserAgent(request);
await addUserAgent(request);

final http.StreamedResponse response;
try {
Expand Down

0 comments on commit f58d439

Please sign in to comment.