34 lines
960 B
Dart
34 lines
960 B
Dart
|
import 'dart:convert';
|
||
|
import 'dart:developer';
|
||
|
import 'dart:io';
|
||
|
|
||
|
class FoodFactLookupClient {
|
||
|
FoodFactLookupClient();
|
||
|
|
||
|
static const String host = "world.openfoodfacts.org";
|
||
|
static const String url = "https://world.openfoodfacts.org/api/v3/product/";
|
||
|
|
||
|
String getProductUrl(String ean) {
|
||
|
return "$url$ean.json";
|
||
|
}
|
||
|
|
||
|
Future<FoodFactModel> retrieveFoodInfo(String ean) async {
|
||
|
HttpClient client = HttpClient();
|
||
|
var request = await client.getUrl(Uri.parse(getProductUrl(ean)));
|
||
|
var response = await request.close();
|
||
|
var asString = await response.transform(utf8.decoder).join();
|
||
|
return FoodFactModel.fromJson(jsonDecode(asString));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class FoodFactModel {
|
||
|
final String name;
|
||
|
|
||
|
FoodFactModel({required this.name});
|
||
|
|
||
|
factory FoodFactModel.fromJson(Map<String, dynamic> json) {
|
||
|
log("key product.product_name: ${json['product']['product_name']}");
|
||
|
return FoodFactModel(name: json['product']['product_name']);
|
||
|
}
|
||
|
}
|