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: Added globalUser to OpenFoodAPIConfiguration #329

Merged
merged 3 commits into from Dec 19, 2021
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
4 changes: 3 additions & 1 deletion lib/utils/HttpHelper.dart
Expand Up @@ -3,6 +3,8 @@ import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:openfoodfacts/model/UserAgent.dart';

import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/UriReader.dart';
import 'package:path/path.dart';
Expand Down Expand Up @@ -141,7 +143,7 @@ class HttpHelper {
'Accept': 'application/json',
'UserAgent':
OpenFoodAPIConfiguration.userAgent?.toValueString() ?? USER_AGENT,
'From': (user != null) ? user.userId : FROM,
'From': OpenFoodAPIConfiguration.getUser(user)?.toValueString() ?? FROM,
});

if (isTestModeActive) {
Expand Down
7 changes: 7 additions & 0 deletions lib/utils/OpenFoodAPIConfiguration.dart
@@ -1,3 +1,4 @@
import 'package:openfoodfacts/model/User.dart';
import 'package:openfoodfacts/model/UserAgent.dart';

import 'CountryHelper.dart';
Expand All @@ -13,6 +14,9 @@ class OpenFoodAPIConfiguration {
///Defines a global userAgent to tell the backend the source of the request.
static UserAgent? userAgent;

///Defines a global user to avoid adding it to every request
static User? globalUser;
M123-dev marked this conversation as resolved.
Show resolved Hide resolved

///change the uriScheme of the requests
static String uriScheme = 'https';

Expand Down Expand Up @@ -52,6 +56,9 @@ class OpenFoodAPIConfiguration {
static QueryType getQueryType(final QueryType? queryType) =>
queryType ?? globalQueryType;

///Returns the [User] to use, using a default value
static User? getUser(final User? user) => user ?? globalUser;

/// Returns the most relevant country code
static String? computeCountryCode(
final OpenFoodFactsCountry? country,
Expand Down