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
Merged
monsieurtanuki
merged 4 commits into
openfoodfacts:master
from
monsieurtanuki:feature/#158
Dec 20, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b5e4f2c
feat: #158 - introduction to folksonomy
monsieurtanuki 973681c
feat: #158 - fix after dart analyze remarks
monsieurtanuki 1f2817e
feat: #158 - fix after unrelated failed tests
monsieurtanuki 635014a
feat: #158 - refactored after code review
monsieurtanuki File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,380 @@ | ||
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 an optional filter. | ||
/// | ||
/// The result can be filtered with that [key], or with [key] = [value]. | ||
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>[]; | ||
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(ProductStats.fromJson(element)); | ||
} | ||
return result; | ||
} | ||
|
||
/// Returns all the products with that [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 tag key. | ||
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); | ||
} | ||
*/ | ||
|
||
/// Returns the versions of [ProductTag] for this [barcode] 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); | ||
} | ||
*/ | ||
|
||
/// Returns the list of tag keys with statistics. | ||
static Future<Map<String, KeyStats>> getKeys({ | ||
final QueryType? queryType, | ||
}) async { | ||
final Map<String, String> parameters = <String, String>{}; | ||
/* TODO "The keys list can be restricted to private tags from some owner" | ||
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); | ||
} | ||
|
||
/// Throws a detailed exception if relevant. Does nothing if [response] is OK. | ||
static void _checkResponse(final Response response) { | ||
if (response.statusCode != 200) { | ||
// TODO have a look at ValidationError in https://api.folksonomy.openfoodfacts.org/docs | ||
throw Exception('Wrong status code: ${response.statusCode}'); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.