Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added unique id to API Request header. #3675

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/smooth_app/lib/helpers/network_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,37 @@ import 'package:flutter/services.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:smooth_app/helpers/app_helper.dart';
import 'package:uuid/uuid.dart';

/// Initializes both the user agent && the SSL certificate
Future<void> setupAppNetworkConfig() async {
await _initUserAgent();
return _importSSLCertificate();
}

String _getUuidId() {
if (OpenFoodAPIConfiguration.uuid != null) {
return OpenFoodAPIConfiguration.uuid!;
}

const Uuid uuid = Uuid();
OpenFoodAPIConfiguration.uuid = uuid.v4();
return OpenFoodAPIConfiguration.uuid!;
}

Future<void> _initUserAgent() async {
final PackageInfo packageInfo = await PackageInfo.fromPlatform();

final String name = 'Smoothie - ${packageInfo.appName}';
final String version = '${packageInfo.version}+${packageInfo.buildNumber}';
final String system =
'${Platform.operatingSystem}+${Platform.operatingSystemVersion}';
final String id = _getUuidId();
final String comment = _getAppInfoComment(
name: name,
version: version,
system: system,
id: id,
);
OpenFoodAPIConfiguration.userAgent = UserAgent(
name: name,
Expand All @@ -39,6 +52,8 @@ String _getAppInfoComment({
String version = '',
bool withSystem = true,
String system = '',
bool withId = true,
String id = '',
}) {
String appInfo = '';
const String infoDelimiter = ' - ';
Expand All @@ -54,6 +69,10 @@ String _getAppInfoComment({
appInfo += infoDelimiter;
appInfo += system;
}
if (withId) {
appInfo += infoDelimiter;
appInfo += id;
}
return appInfo;
}

Expand Down