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: #158 - introduction to folksonomy #332
Changes from 3 commits
b5e4f2c
973681c
1f2817e
635014a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,372 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:http/http.dart'; | ||
|
||
import 'model/KeyStats.dart'; | ||
import 'model/ProductList.dart'; | ||
import 'model/ProductStats.dart'; | ||
import 'model/ProductTag.dart'; | ||
import 'utils/HttpHelper.dart'; | ||
import 'utils/QueryType.dart'; | ||
import 'utils/UriHelper.dart'; | ||
|
||
/// Client calls of the Folksonomy API (Open Food Facts) | ||
/// | ||
/// cf. https://api.folksonomy.openfoodfacts.org/docs | ||
class FolksonomyAPIClient { | ||
FolksonomyAPIClient._(); | ||
|
||
/// "hello world" | ||
static Future<void> hello({ | ||
final QueryType? queryType, | ||
}) async { | ||
final Response response = await HttpHelper().doGetRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: '/', | ||
queryType: queryType, | ||
), | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
} | ||
|
||
/// Returns all the [ProductStats] with that tag key, or with tag key == value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does |
||
static Future<List<ProductStats>> getProductStats({ | ||
final String? key, | ||
final String? value, | ||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO | ||
if (owner != null) { | ||
parameters['owner'] = owner; | ||
} | ||
*/ | ||
if (key == null && value != null) { | ||
throw Exception( | ||
'Does a value have a meaning without its key? I don\'t think so.'); | ||
} | ||
if (key != null) { | ||
parameters['k'] = key; | ||
} | ||
if (value != null) { | ||
parameters['v'] = value; | ||
} | ||
final Response response = await HttpHelper().doGetRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'products/stats', | ||
queryParameters: parameters, | ||
queryType: queryType, | ||
), | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
final List<ProductStats> result = <ProductStats>[]; | ||
final List<dynamic> json = jsonDecode(response.body) as List<dynamic>; | ||
for (var element in json) { | ||
result.add(ProductStats.fromJson(element)); | ||
} | ||
return result; | ||
} | ||
|
||
/// Returns all the products with that tag key. | ||
/// | ||
/// The key of the returned map is the barcode, the value is the tag value. | ||
static Future<Map<String, String>> getProducts({ | ||
required final String key, | ||
final String? value, | ||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO | ||
if (owner != null) { | ||
parameters['owner'] = owner; | ||
} | ||
*/ | ||
parameters['k'] = key; | ||
if (value != null) { | ||
parameters['v'] = value; | ||
} | ||
final Response response = await HttpHelper().doGetRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'products', | ||
queryParameters: parameters, | ||
queryType: queryType, | ||
), | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
final Map<String, String> result = <String, String>{}; | ||
final List<dynamic> json = jsonDecode(response.body) as List<dynamic>; | ||
for (var element in json) { | ||
final ProductList productList = ProductList.fromJson(element); | ||
if (productList.key != key) { | ||
throw Exception('Unexpected key: ${productList.key}'); | ||
monsieurtanuki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
result[productList.barcode] = productList.value; | ||
} | ||
return result; | ||
} | ||
|
||
/// Returns all the [ProductTag]s for this product | ||
/// | ||
monsieurtanuki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// The key of the returned map is the key. | ||
monsieurtanuki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
static Future<Map<String, ProductTag>> getProductTags({ | ||
required final String barcode, | ||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO | ||
if (owner != null) { | ||
parameters['owner'] = owner; | ||
} | ||
*/ | ||
final Response response = await HttpHelper().doGetRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'product/$barcode', | ||
queryParameters: parameters, | ||
queryType: queryType, | ||
), | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
final Map<String, ProductTag> result = <String, ProductTag>{}; | ||
if (response.body == 'null') { | ||
// not found | ||
return result; | ||
} | ||
final List<dynamic> json = jsonDecode(response.body) as List<dynamic>; | ||
for (var element in json) { | ||
final ProductTag productTag = ProductTag.fromJson(element); | ||
result[productTag.key] = productTag; | ||
} | ||
return result; | ||
} | ||
|
||
/// Returns the [ProductTag] for this product and this tag key | ||
/// | ||
/// Returns null if not found. | ||
static Future<ProductTag?> getProductTag({ | ||
required final String barcode, | ||
required final String key, | ||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO | ||
if (owner != null) { | ||
parameters['owner'] = owner; | ||
} | ||
*/ | ||
final Response response = await HttpHelper().doGetRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'product/$barcode/$key', | ||
queryParameters: parameters, | ||
queryType: queryType, | ||
), | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
if (response.body == 'null') { | ||
// not found | ||
return null; | ||
} | ||
final Map<String, dynamic> json = | ||
jsonDecode(response.body) as Map<String, dynamic>; | ||
return ProductTag.fromJson(json); | ||
} | ||
|
||
/// Returns all the [ProductTag]s for this product, with their subkeys. | ||
/// | ||
/// The key of the returned map is the key. | ||
static Future<Map<String, ProductTag>> getProductTagWithSubKeys({ | ||
required final String barcode, | ||
required final String key, | ||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO | ||
if (owner != null) { | ||
parameters['owner'] = owner; | ||
} | ||
*/ | ||
final Response response = await HttpHelper().doGetRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'product/$barcode/$key*', // look at the star! | ||
queryParameters: parameters, | ||
queryType: queryType, | ||
), | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
final Map<String, ProductTag> result = <String, ProductTag>{}; | ||
if (response.body == 'null') { | ||
// not found | ||
return result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, null or a mention in the comment |
||
} | ||
final List<dynamic> json = jsonDecode(response.body) as List<dynamic>; | ||
for (var element in json) { | ||
final ProductTag productTag = ProductTag.fromJson(element); | ||
result[productTag.key] = productTag; | ||
} | ||
return result; | ||
} | ||
|
||
/* TODO | ||
Future<void> deleteProductTag({ | ||
required final String barcode, | ||
required final String key, | ||
required final int version, | ||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO | ||
if (owner != null) { | ||
parameters['owner'] = owner; | ||
} | ||
*/ | ||
final Response response = await HttpHelper().doDeleteRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'product/$barcode/$key', | ||
queryParameters: parameters, | ||
queryType: queryType, | ||
), | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
} | ||
*/ | ||
|
||
/// Folksonomy: returns the versions of [ProductTag] for this product and key. | ||
static Future<List<ProductTag>> getProductTagVersions({ | ||
required final String barcode, | ||
required final String key, | ||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO | ||
if (owner != null) { | ||
parameters['owner'] = owner; | ||
} | ||
*/ | ||
final Response response = await HttpHelper().doGetRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'product/$barcode/$key/versions', | ||
queryParameters: parameters, | ||
queryType: queryType, | ||
), | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
final List<ProductTag> result = <ProductTag>[]; | ||
if (response.body == 'null') { | ||
// not found | ||
return result; | ||
} | ||
final List<dynamic> json = jsonDecode(response.body) as List<dynamic>; | ||
for (var element in json) { | ||
result.add(ProductTag.fromJson(element)); | ||
} | ||
return result; | ||
} | ||
|
||
/* TODO | ||
/// productTag.version must be equal to previous version + 1 | ||
static Future<void> updateProductTag({ | ||
required final ProductTag productTag, | ||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO | ||
if (owner != null) { | ||
parameters['owner'] = owner; | ||
} | ||
*/ | ||
final Response response = await HttpHelper().doPutRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'product', | ||
queryParameters: parameters, | ||
queryType: queryType, | ||
), | ||
productTag.toJson().toString(), | ||
userAgent: OpenFoodAPIConfiguration.userAgent, | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
} | ||
*/ | ||
|
||
/* TODO | ||
/// productTag.version must be equal to 1 | ||
static Future<void> addProductTag({ | ||
required final ProductTag productTag, | ||
final User? user, | ||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO | ||
if (owner != null) { | ||
parameters['owner'] = owner; | ||
} | ||
*/ | ||
final Response response = await HttpHelper().doPostRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'product', | ||
queryParameters: parameters, | ||
queryType: queryType, | ||
), | ||
{}, // TODO later productTag.toJson(), | ||
user, | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
} | ||
*/ | ||
|
||
static Future<Map<String, KeyStats>> getKeys({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No comment here. From the docs:
|
||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO | ||
if (owner != null) { | ||
parameters['owner'] = owner; | ||
} | ||
*/ | ||
final Response response = await HttpHelper().doGetRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'keys', | ||
queryParameters: parameters, | ||
queryType: queryType, | ||
), | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
final Map<String, KeyStats> result = <String, KeyStats>{}; | ||
final List<dynamic> json = jsonDecode(response.body) as List<dynamic>; | ||
for (var element in json) { | ||
final KeyStats item = KeyStats.fromJson(element); | ||
result[item.key] = item; | ||
} | ||
return result; | ||
} | ||
|
||
static Future<void> ping({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about |
||
final QueryType? queryType, | ||
}) async { | ||
final Response response = await HttpHelper().doGetRequest( | ||
UriHelper.getFolksonomyUri( | ||
path: 'ping', | ||
queryType: queryType, | ||
), | ||
queryType: queryType, | ||
); | ||
_checkResponse(response); | ||
} | ||
|
||
static void _checkResponse(final Response response) { | ||
if (response.statusCode != 200) { | ||
print('ERROR: ${response.body}'); | ||
throw Exception('Wrong status code: ${response.statusCode}'); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a void doesn't make sense, lets return the answer instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As for
ping
, returning avoid
does make sense. At least for me, and that's an interesting point.If you compare to the current code in openfoodfacts, for instance https://github.com/openfoodfacts/openfoodfacts-dart/blob/master/lib/openfoodfacts.dart#L886-L890, we see that @jasmeet0817 has a problem with throwing an
Exception
. Should the methods handle the exception and return a clean useless value (and a step further: call Sentry) or let the developer handle the exception?I think we should let the exceptions being thrown. We should tell the developers that our methods throw exceptions (e.g. when there's no internet, or when the server is down), and let them handle the exceptions their own way, for instance with Sentry in smoothie.
In the specific case of
hello
orping
, we don't give a damn about a value: we just want to know if everything was alright.