Skip to content

Commit

Permalink
feat: openfoodfacts#306 - added "user" parameter to WRITE methods
Browse files Browse the repository at this point in the history
Impacted files:
* `api_addProductImage_test.dart`: added `User user` parameters when needed
* `openfoodfacts.dart`: added `User user` parameter to WRITE methods; added ending commas
  • Loading branch information
monsieurtanuki committed Dec 14, 2021
1 parent ea91655 commit 4215573
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 33 deletions.
94 changes: 64 additions & 30 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,12 @@ class OpenFoodAPIClient {
queryType: queryType,
);

Response response = await HttpHelper().doGetRequest(insightUri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType);
Response response = await HttpHelper().doGetRequest(
insightUri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType,
);
var result =
InsightsResult.fromJson(json.decode(utf8.decode(response.bodyBytes)));

Expand All @@ -479,10 +481,12 @@ class OpenFoodAPIClient {
queryType: queryType,
);

Response response = await HttpHelper().doGetRequest(insightsUri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType);
Response response = await HttpHelper().doGetRequest(
insightsUri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType,
);

return InsightsResult.fromJson(
json.decode(utf8.decode(response.bodyBytes)));
Expand Down Expand Up @@ -510,10 +514,12 @@ class OpenFoodAPIClient {
queryType: queryType,
);

Response response = await HttpHelper().doGetRequest(robotoffQuestionUri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType);
Response response = await HttpHelper().doGetRequest(
robotoffQuestionUri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType,
);
var result = RobotoffQuestionResult.fromJson(
json.decode(utf8.decode(response.bodyBytes)));

Expand Down Expand Up @@ -550,10 +556,12 @@ class OpenFoodAPIClient {
queryType: queryType,
);

Response response = await HttpHelper().doGetRequest(robotoffQuestionUri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType);
Response response = await HttpHelper().doGetRequest(
robotoffQuestionUri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType,
);
var result = RobotoffQuestionResult.fromJson(
json.decode(utf8.decode(response.bodyBytes)));

Expand Down Expand Up @@ -618,10 +626,12 @@ class OpenFoodAPIClient {
queryType: queryType,
);

Response response = await HttpHelper().doGetRequest(spellingCorrectionUri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType);
Response response = await HttpHelper().doGetRequest(
spellingCorrectionUri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType,
);
SpellingCorrection result = SpellingCorrection.fromJson(
json.decode(utf8.decode(response.bodyBytes)));

Expand Down Expand Up @@ -700,8 +710,11 @@ class OpenFoodAPIClient {
path: '/cgi/auth.pl',
queryType: queryType,
);
Response response =
await HttpHelper().doPostRequest(loginUri, user.toData(), user);
Response response = await HttpHelper().doPostRequest(
loginUri,
user.toData(),
user,
);
return response.statusCode == 200;
}

Expand Down Expand Up @@ -833,8 +846,11 @@ class OpenFoodAPIClient {
},
);
try {
final Response response = await HttpHelper()
.doGetRequest(uri, userAgent: OpenFoodAPIConfiguration.userAgent);
final Response response = await HttpHelper().doGetRequest(
uri,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType,
);
if (response.statusCode != 200) {
return null;
}
Expand Down Expand Up @@ -866,8 +882,11 @@ class OpenFoodAPIClient {
);

try {
final Response response = await HttpHelper()
.doGetRequest(uri, userAgent: OpenFoodAPIConfiguration.userAgent);
final Response response = await HttpHelper().doGetRequest(
uri,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType,
);
if (response.statusCode != 200) {
return KnowledgePanels.empty();
}
Expand Down Expand Up @@ -900,6 +919,7 @@ class OpenFoodAPIClient {
final Response response = await HttpHelper().doGetRequest(
uri,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType,
);
if (response.statusCode != 200) {
throw Exception('Could not retrieve ordered nutrients!');
Expand All @@ -922,13 +942,15 @@ class OpenFoodAPIClient {
required final OpenFoodFactsLanguage language,
required final String imgid,
required final ImageAngle angle,
required final User user,
final QueryType? queryType,
}) async =>
await _callProductImageCrop(
barcode: barcode,
imageField: imageField,
language: language,
imgid: imgid,
user: user,
extraParameters: <String, String>{
'angle': angle.degreesClockwise,
},
Expand All @@ -953,6 +975,7 @@ class OpenFoodAPIClient {
required final int y1,
required final int x2,
required final int y2,
required final User user,
final ImageAngle angle = ImageAngle.NOON,
final QueryType? queryType,
}) async =>
Expand All @@ -961,6 +984,7 @@ class OpenFoodAPIClient {
imageField: imageField,
language: language,
imgid: imgid,
user: user,
extraParameters: <String, String>{
'x1': x1.toString(),
'y1': y1.toString(),
Expand All @@ -981,6 +1005,7 @@ class OpenFoodAPIClient {
required final OpenFoodFactsLanguage language,
required final String imgid,
required final Map<String, String> extraParameters,
required final User user,
final QueryType? queryType,
}) async {
final String id = '${imageField.value}_${language.code}';
Expand All @@ -996,8 +1021,12 @@ class OpenFoodAPIClient {
queryParameters: queryParameters,
);

final Response response = await HttpHelper()
.doGetRequest(uri, userAgent: OpenFoodAPIConfiguration.userAgent);
final Response response = await HttpHelper().doGetRequest(
uri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType,
);
if (response.statusCode != 200) {
throw Exception(
'Bad response (${response.statusCode}): ${response.body}');
Expand Down Expand Up @@ -1033,6 +1062,7 @@ class OpenFoodAPIClient {
required final String barcode,
required final ImageField imageField,
required final OpenFoodFactsLanguage language,
required final User user,
final QueryType? queryType,
}) async {
final String id = '${imageField.value}_${language.code}';
Expand All @@ -1042,8 +1072,12 @@ class OpenFoodAPIClient {
queryParameters: <String, String>{'code': barcode, 'id': id},
);

final Response response = await HttpHelper()
.doGetRequest(uri, userAgent: OpenFoodAPIConfiguration.userAgent);
final Response response = await HttpHelper().doGetRequest(
uri,
user: user,
userAgent: OpenFoodAPIConfiguration.userAgent,
queryType: queryType,
);
if (response.statusCode != 200) {
throw Exception(
'Bad response (${response.statusCode}): ${response.body}');
Expand Down
10 changes: 7 additions & 3 deletions test/api_addProductImage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'test_constants.dart';

void main() {
OpenFoodAPIConfiguration.globalQueryType = QueryType.TEST;
const User user = TestConstants.TEST_USER;

/// Common constants for several image operations
const String barcode = '4250752200784';
Expand Down Expand Up @@ -83,7 +84,7 @@ void main() {
imageUri: Uri.file('test/test_assets/front_de.jpg'),
);
final Status status = await OpenFoodAPIClient.addProductImage(
TestConstants.TEST_USER,
user,
image,
);

Expand All @@ -99,7 +100,7 @@ void main() {
imageUri: Uri.file('test/test_assets/ingredients_en.jpg'),
);
Status status = await OpenFoodAPIClient.addProductImage(
TestConstants.TEST_USER,
user,
image,
);

Expand All @@ -113,7 +114,7 @@ void main() {
ProductQueryConfiguration('7622210449283');
final ProductResult result = await OpenFoodAPIClient.getProduct(
configurations,
user: TestConstants.TEST_USER,
user: user,
);
expect(result.status, isNotNull);
expect(result.product!.images, isNotEmpty);
Expand All @@ -140,6 +141,7 @@ void main() {
for (final ImageAngle angle in ImageAngle.values) {
final String? newUrl = await OpenFoodAPIClient.setProductImageAngle(
barcode: barcode,
user: user,
imageField: imageField,
language: language,
imgid: imgid!,
Expand Down Expand Up @@ -178,6 +180,7 @@ void main() {
for (final ImageAngle angle in ImageAngle.values) {
final String? newUrl = await OpenFoodAPIClient.setProductImageCrop(
barcode: barcode,
user: user,
imageField: imageField,
language: language,
imgid: imgid!,
Expand Down Expand Up @@ -205,6 +208,7 @@ void main() {
const ImageField unselectedImageField = ImageField.INGREDIENTS;
await OpenFoodAPIClient.unselectProductImage(
barcode: barcode,
user: user,
imageField: unselectedImageField,
language: language,
);
Expand Down

0 comments on commit 4215573

Please sign in to comment.