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 1 commit
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
14 changes: 7 additions & 7 deletions lib/utils/HttpHelper.dart
@@ -1,13 +1,13 @@
import 'package:meta/meta.dart';
import 'dart:async';
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';

import 'dart:async';
import 'dart:convert';

import 'OpenFoodAPIConfiguration.dart';
import 'QueryType.dart';

Expand Down Expand Up @@ -42,7 +42,7 @@ class HttpHelper {
http.Response response = await http.get(
uri,
headers: _buildHeaders(
user: user,
user: OpenFoodAPIConfiguration.getUser(user),
userAgent: userAgent,
isTestModeActive:
OpenFoodAPIConfiguration.getQueryType(queryType) == QueryType.PROD
Expand All @@ -66,7 +66,7 @@ class HttpHelper {
http.Response response = await http.post(
uri,
headers: _buildHeaders(
user: user,
user: OpenFoodAPIConfiguration.getUser(user),
isTestModeActive:
OpenFoodAPIConfiguration.getQueryType(queryType) == QueryType.PROD
? false
Expand All @@ -91,7 +91,7 @@ class HttpHelper {

request.headers.addAll(
_buildHeaders(
user: user,
user: OpenFoodAPIConfiguration.getUser(user),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should simplify the code and call OpenFoodAPIConfiguration.getUser(user) only once: inside _buildHeaders. Does that make sense?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me: Ignore the thing I am doing at the same time for the userAgent,

Fixed

isTestModeActive:
OpenFoodAPIConfiguration.getQueryType(queryType) == QueryType.PROD
? false
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