Spaces:
Build error
Build error
David Li commited on
Commit ·
2e3927c
1
Parent(s): 6b43158
fix: update dart off server
Browse files- bin/{cli.dart → dart_off_server.dart} +16 -2
- lib/core.dart +32 -2
- pubspec.lock +21 -0
- pubspec.yaml +1 -0
bin/{cli.dart → dart_off_server.dart}
RENAMED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
import 'package:dart_off_server/core.dart' as
|
| 2 |
import 'package:alfred/alfred.dart';
|
| 3 |
import 'dart:io';
|
|
|
|
| 4 |
|
| 5 |
void main(List<String> arguments) async {
|
| 6 |
// get port from arguments
|
|
@@ -10,12 +11,16 @@ void main(List<String> arguments) async {
|
|
| 10 |
} else {
|
| 11 |
port = int.tryParse(arguments.first) ?? 6565;
|
| 12 |
}
|
|
|
|
| 13 |
final app = Alfred();
|
| 14 |
|
| 15 |
// print line
|
| 16 |
// print('Starting up server: on port $port');
|
| 17 |
|
| 18 |
-
app.get('/', (req, res)
|
|
|
|
|
|
|
|
|
|
| 19 |
app.get('/text', (req, res) => 'Text response');
|
| 20 |
|
| 21 |
app.get('/json', (req, res) => {'json_response': true});
|
|
@@ -35,5 +40,14 @@ void main(List<String> arguments) async {
|
|
| 35 |
body != null; //true
|
| 36 |
});
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
await app.listen(port); //Listening on port 6565
|
| 39 |
}
|
|
|
|
| 1 |
+
import 'package:dart_off_server/core.dart' as openfood;
|
| 2 |
import 'package:alfred/alfred.dart';
|
| 3 |
import 'dart:io';
|
| 4 |
+
import 'package:openfoodfacts/openfoodfacts.dart';
|
| 5 |
|
| 6 |
void main(List<String> arguments) async {
|
| 7 |
// get port from arguments
|
|
|
|
| 11 |
} else {
|
| 12 |
port = int.tryParse(arguments.first) ?? 6565;
|
| 13 |
}
|
| 14 |
+
openfood.mkConfiguration();
|
| 15 |
final app = Alfred();
|
| 16 |
|
| 17 |
// print line
|
| 18 |
// print('Starting up server: on port $port');
|
| 19 |
|
| 20 |
+
app.get('/', (req, res) {
|
| 21 |
+
res.headers.contentType = ContentType.html;
|
| 22 |
+
return '<html><body><h1>Test HTML</h1></body></html>';
|
| 23 |
+
});
|
| 24 |
app.get('/text', (req, res) => 'Text response');
|
| 25 |
|
| 26 |
app.get('/json', (req, res) => {'json_response': true});
|
|
|
|
| 40 |
body != null; //true
|
| 41 |
});
|
| 42 |
|
| 43 |
+
app.get('/food/search', (req, res) {
|
| 44 |
+
final query = req.uri.queryParameters;
|
| 45 |
+
return openfood.search(query).then((value) {
|
| 46 |
+
res.json(value);
|
| 47 |
+
}).catchError((error) {
|
| 48 |
+
res.json(error);
|
| 49 |
+
});
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
await app.listen(port); //Listening on port 6565
|
| 53 |
}
|
lib/core.dart
CHANGED
|
@@ -1,3 +1,33 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
}
|
|
|
|
| 1 |
+
import 'package:openfoodfacts/openfoodfacts.dart';
|
| 2 |
+
|
| 3 |
+
Future<SearchResult> search(Map<String, String> query) {
|
| 4 |
+
var parametersList = <Parameter>[];
|
| 5 |
+
// check for terms in quer
|
| 6 |
+
if (query.containsKey('terms')) {
|
| 7 |
+
// print('terms: ${query['terms']}');
|
| 8 |
+
var terms = query['terms'];
|
| 9 |
+
if (terms != null && terms.isNotEmpty) {
|
| 10 |
+
parametersList.add(SearchTerms(terms: terms.split(',')));
|
| 11 |
+
}
|
| 12 |
+
}
|
| 13 |
+
ProductSearchQueryConfiguration configuration =
|
| 14 |
+
ProductSearchQueryConfiguration(
|
| 15 |
+
parametersList: parametersList,
|
| 16 |
+
version: ProductQueryVersion.v3,
|
| 17 |
+
);
|
| 18 |
+
return OpenFoodAPIClient.searchProducts(
|
| 19 |
+
User(userId: '', password: ''),
|
| 20 |
+
configuration,
|
| 21 |
+
);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
void mkConfiguration() {
|
| 25 |
+
OpenFoodAPIConfiguration.userAgent = UserAgent(
|
| 26 |
+
name: 'dart_off_server',
|
| 27 |
+
url: 'https://friendlyuser-dart-off-server.hf.space/');
|
| 28 |
+
OpenFoodAPIConfiguration.globalLanguages = <OpenFoodFactsLanguage>[
|
| 29 |
+
OpenFoodFactsLanguage.ENGLISH
|
| 30 |
+
];
|
| 31 |
+
|
| 32 |
+
OpenFoodAPIConfiguration.globalCountry = OpenFoodFactsCountry.CANADA;
|
| 33 |
}
|
pubspec.lock
CHANGED
|
@@ -92,6 +92,13 @@ packages:
|
|
| 92 |
url: "https://pub.dartlang.org"
|
| 93 |
source: hosted
|
| 94 |
version: "2.1.1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
http_multi_server:
|
| 96 |
dependency: transitive
|
| 97 |
description:
|
|
@@ -120,6 +127,13 @@ packages:
|
|
| 120 |
url: "https://pub.dartlang.org"
|
| 121 |
source: hosted
|
| 122 |
version: "0.6.5"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
lints:
|
| 124 |
dependency: "direct dev"
|
| 125 |
description:
|
|
@@ -169,6 +183,13 @@ packages:
|
|
| 169 |
url: "https://pub.dartlang.org"
|
| 170 |
source: hosted
|
| 171 |
version: "2.0.1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
package_config:
|
| 173 |
dependency: transitive
|
| 174 |
description:
|
|
|
|
| 92 |
url: "https://pub.dartlang.org"
|
| 93 |
source: hosted
|
| 94 |
version: "2.1.1"
|
| 95 |
+
http:
|
| 96 |
+
dependency: transitive
|
| 97 |
+
description:
|
| 98 |
+
name: http
|
| 99 |
+
url: "https://pub.dartlang.org"
|
| 100 |
+
source: hosted
|
| 101 |
+
version: "0.13.5"
|
| 102 |
http_multi_server:
|
| 103 |
dependency: transitive
|
| 104 |
description:
|
|
|
|
| 127 |
url: "https://pub.dartlang.org"
|
| 128 |
source: hosted
|
| 129 |
version: "0.6.5"
|
| 130 |
+
json_annotation:
|
| 131 |
+
dependency: transitive
|
| 132 |
+
description:
|
| 133 |
+
name: json_annotation
|
| 134 |
+
url: "https://pub.dartlang.org"
|
| 135 |
+
source: hosted
|
| 136 |
+
version: "4.7.0"
|
| 137 |
lints:
|
| 138 |
dependency: "direct dev"
|
| 139 |
description:
|
|
|
|
| 183 |
url: "https://pub.dartlang.org"
|
| 184 |
source: hosted
|
| 185 |
version: "2.0.1"
|
| 186 |
+
openfoodfacts:
|
| 187 |
+
dependency: "direct main"
|
| 188 |
+
description:
|
| 189 |
+
name: openfoodfacts
|
| 190 |
+
url: "https://pub.dartlang.org"
|
| 191 |
+
source: hosted
|
| 192 |
+
version: "2.2.1"
|
| 193 |
package_config:
|
| 194 |
dependency: transitive
|
| 195 |
description:
|
pubspec.yaml
CHANGED
|
@@ -8,6 +8,7 @@ environment:
|
|
| 8 |
|
| 9 |
dependencies:
|
| 10 |
alfred: ^1.0.0+1
|
|
|
|
| 11 |
|
| 12 |
dev_dependencies:
|
| 13 |
lints: ^2.0.0
|
|
|
|
| 8 |
|
| 9 |
dependencies:
|
| 10 |
alfred: ^1.0.0+1
|
| 11 |
+
openfoodfacts: ^2.2.1
|
| 12 |
|
| 13 |
dev_dependencies:
|
| 14 |
lints: ^2.0.0
|