temporarySaveProductV3 static method

Future<ProductResultV3> temporarySaveProductV3(
  1. User user,
  2. String barcode, {
  3. List<ProductPackaging>? packagings,
  4. QueryType? queryType,
  5. OpenFoodFactsCountry? country,
  6. OpenFoodFactsLanguage? language,
})

Temporary: saves product packagings V3 style.

For the moment that's the only field supported in WRITE by API V3. Long term target is of course more something like saveProduct.

Implementation

static Future<ProductResultV3> temporarySaveProductV3(
  final User user,
  final String barcode, {
  final List<ProductPackaging>? packagings,
  final QueryType? queryType,
  final OpenFoodFactsCountry? country,
  final OpenFoodFactsLanguage? language,
}) async {
  final Map<String, dynamic> parameterMap = <String, dynamic>{};
  parameterMap.addAll(user.toData());
  if (packagings == null) {
    // For the moment it's the only purpose of this method: saving packagings.
    throw Exception('packagings cannot be null');
  }
  parameterMap['product'] = {};
  parameterMap['product']['packagings'] = packagings;
  if (language != null) {
    parameterMap['lc'] = language.offTag;
    parameterMap['tags_lc'] = language.offTag;
  }
  if (country != null) {
    parameterMap['cc'] = country.offTag;
  }

  var productUri = UriHelper.getPatchUri(
    path: '/api/v3/product/$barcode',
    queryType: queryType,
  );

  final Response response = await HttpHelper().doPatchRequest(
    productUri,
    parameterMap,
    user,
    queryType: queryType,
  );
  return ProductResultV3.fromJson(jsonDecode(response.body));
}