Skip to content

Commit

Permalink
User agent fields added to ALL prices URLs (except proof file URLs)
Browse files Browse the repository at this point in the history
Impacted files:
* `api_prices_test.dart`: test for new method `getFileUrl`
* `open_prices_api_client.dart`: similar URI user agent management as off-dart
* `proof.dart`: minor fix
  • Loading branch information
monsieurtanuki committed Jun 6, 2024
1 parent 3a5607b commit aa494d1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
14 changes: 2 additions & 12 deletions lib/src/open_prices_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ class OpenPricesAPIClient {
required final String path,
final Map<String, dynamic>? queryParameters,
final UriProductHelper uriHelper = uriHelperFoodProd,
final bool? addUserAgentParameters,
}) =>
uriHelper.getUri(
path: path,
queryParameters: queryParameters,
forcedHost: _getHost(uriHelper),
addUserAgentParameters: addUserAgentParameters,
);

static Future<MaybeError<GetPricesResult>> getPrices(
Expand Down Expand Up @@ -319,14 +321,8 @@ class OpenPricesAPIClient {
required final String bearerToken,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final String? appName = OpenFoodAPIConfiguration.userAgent?.name;
final Uri uri = getUri(
path: '/api/v1/prices',
queryParameters: appName == null
? null
: <String, String>{
'app_name': appName,
},
uriHelper: uriHelper,
);
final StringBuffer body = StringBuffer();
Expand Down Expand Up @@ -440,14 +436,8 @@ class OpenPricesAPIClient {
required final String bearerToken,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final String? appName = OpenFoodAPIConfiguration.userAgent?.name;
final Uri uri = getUri(
path: '/api/v1/proofs/upload',
queryParameters: appName == null
? null
: <String, String>{
'app_name': appName,
},
uriHelper: uriHelper,
);

Expand Down
1 change: 1 addition & 0 deletions lib/src/prices/proof.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ class Proof extends JsonObject {
: OpenPricesAPIClient.getUri(
path: 'img/$filePath',
uriHelper: uriProductHelper,
addUserAgentParameters: false,
);
}
8 changes: 8 additions & 0 deletions test/api_prices_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:http/http.dart' as http;

import 'package:http_parser/http_parser.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:test/test.dart';
Expand All @@ -8,6 +10,7 @@ void main() {
const UriProductHelper uriHelper = uriHelperFoodTest;
const User user = TestConstants.TEST_USER;
const String invalidBearerToken = 'invalid bearer token';
const int HTTP_OK = 200;

group('$OpenPricesAPIClient default', () {
test('getStatus', () async {
Expand Down Expand Up @@ -610,6 +613,11 @@ void main() {
expect(maybeProof.value.mimetype, proof.mimetype);
expect(maybeProof.value.created, proof.created);
expect(maybeProof.value.filePath, proof.filePath);
if (proof.filePath != null) {
final Uri uri = proof.getFileUrl(uriProductHelper: uriHelper)!;
final http.Response response = await http.get(uri);
expect(response.statusCode, HTTP_OK);
}
});

test('upload', () async {
Expand Down

0 comments on commit aa494d1

Please sign in to comment.