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

feat: #370 - added "uuid" to configuration #378

Merged
merged 1 commit into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/utils/HttpHelper.dart
Expand Up @@ -33,17 +33,18 @@ class HttpHelper {
static Map<String, String>? addUserAgentParameters(
Map<String, String>? map,
) {
if (OpenFoodAPIConfiguration.userAgent == null) {
return map;
}
if (OpenFoodAPIConfiguration.userAgent!.name != null) {
if (OpenFoodAPIConfiguration.userAgent?.name != null) {
map ??= <String, String>{};
map['app_name'] = OpenFoodAPIConfiguration.userAgent!.name!;
}
if (OpenFoodAPIConfiguration.userAgent!.version != null) {
if (OpenFoodAPIConfiguration.userAgent?.version != null) {
map ??= <String, String>{};
map['app_version'] = OpenFoodAPIConfiguration.userAgent!.version!;
}
if (OpenFoodAPIConfiguration.uuid != null) {
map ??= <String, String>{};
map['app_uuid'] = OpenFoodAPIConfiguration.uuid!;
}
return map;
}

Expand Down
3 changes: 3 additions & 0 deletions lib/utils/OpenFoodAPIConfiguration.dart
Expand Up @@ -14,6 +14,9 @@ class OpenFoodAPIConfiguration {
///Defines a global userAgent to tell the backend the source of the request.
static UserAgent? userAgent;

///Defines a global uuid to tell the backend the source of the request.
static String? uuid;

///Defines a global user to avoid adding it to every request
static User? globalUser;

Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Expand Up @@ -15,8 +15,8 @@ dependencies:
meta: ^1.7.0

dev_dependencies:
analyzer: ^3.0.0
analyzer: ^3.2.0
build_runner: ^2.1.7
json_serializable: ^6.1.3
json_serializable: ^6.1.4
lints: ^1.0.1
test: ^1.20.0
49 changes: 49 additions & 0 deletions test/configuration_test.dart
Expand Up @@ -9,6 +9,7 @@ void main() {

test('Get Uri', () {
OpenFoodAPIConfiguration.userAgent = null;
OpenFoodAPIConfiguration.uuid = null;
Uri uri = UriHelper.getUri(
path: '/test/test.pl',
);
Expand All @@ -32,6 +33,7 @@ void main() {
const String version = '12';
OpenFoodAPIConfiguration.userAgent =
UserAgent(name: name, version: version);
OpenFoodAPIConfiguration.uuid = null;
Uri uri;

uri = UriHelper.getUri(
Expand Down Expand Up @@ -71,8 +73,52 @@ void main() {
);
});

test('Get Uri with uuid', () {
const String uuid = 'uuidTest';
OpenFoodAPIConfiguration.userAgent = null;
OpenFoodAPIConfiguration.uuid = uuid;
Uri uri;

uri = UriHelper.getUri(
path: '/test/test.pl',
);
expect(
uri.toString(),
'https://world.openfoodfacts.org/test/test.pl?app_uuid=$uuid',
);

uri = UriHelper.getUri(
path: '/test/test.pl',
queryParameters: <String, String>{'test': 'true', 'queryType': 'PROD'},
);
expect(
uri.toString(),
'https://world.openfoodfacts.org/test/test.pl?test=true&queryType=PROD&app_uuid=$uuid',
);

uri = UriHelper.getUri(
path: '/test/test.pl',
addUserAgentParameters: false,
);
expect(
uri.toString(),
'https://world.openfoodfacts.org/test/test.pl',
);

uri = UriHelper.getUri(
path: '/test/test.pl',
queryParameters: <String, String>{'test': 'true', 'queryType': 'PROD'},
addUserAgentParameters: false,
);
expect(
uri.toString(),
'https://world.openfoodfacts.org/test/test.pl?test=true&queryType=PROD',
);
});

test('Get Test Uri', () {
OpenFoodAPIConfiguration.userAgent = null;
OpenFoodAPIConfiguration.uuid = null;
Uri uri = UriHelper.getUri(
path: '/test/test.pl',
queryType: QueryType.TEST,
Expand All @@ -95,6 +141,7 @@ void main() {

test('Get Robotoff Uri', () {
OpenFoodAPIConfiguration.userAgent = null;
OpenFoodAPIConfiguration.uuid = null;
Uri uri = UriHelper.getRobotoffUri(
path: '/test/test.pl',
);
Expand All @@ -115,6 +162,7 @@ void main() {

test('Get Robotoff Test Uri', () {
OpenFoodAPIConfiguration.userAgent = null;
OpenFoodAPIConfiguration.uuid = null;
Uri uri = UriHelper.getRobotoffUri(
path: '/test/test.pl',
queryType: QueryType.TEST,
Expand All @@ -137,6 +185,7 @@ void main() {

test('Get Uri with different uriScheme', () {
OpenFoodAPIConfiguration.userAgent = null;
OpenFoodAPIConfiguration.uuid = null;
OpenFoodAPIConfiguration.uriScheme = 'http';
Uri uri = UriHelper.getUri(
path: '/test/test.pl',
Expand Down