Skip to content
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

fix: added "app_name" parameter for prices methods uploadProof and createPrice #938

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/src/model/parameter/page_size.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import '../../interface/parameter.dart';

/// "Page size" search API parameter
///
/// Typically defaults to 50 (used to be 24). Max value seems to be 100.
class PageSize extends Parameter {
@override
String getName() => 'page_size';
Expand Down
81 changes: 47 additions & 34 deletions lib/src/open_prices_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,28 @@ class OpenPricesAPIClient {
static String _getHost(final UriProductHelper uriHelper) =>
uriHelper.getHost(_subdomain);

static Uri getUri({
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(
final GetPricesParameters parameters, {
final UriProductHelper uriHelper = uriHelperFoodProd,
final String? bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/prices',
queryParameters: parameters.getQueryParameters(),
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -74,9 +87,9 @@ class OpenPricesAPIClient {
required final LocationOSMType locationOSMType,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/locations/osm/${locationOSMType.offTag}/$locationOSMId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -98,10 +111,10 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
final String? bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/locations',
queryParameters: parameters.getQueryParameters(),
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -125,9 +138,9 @@ class OpenPricesAPIClient {
final int locationId, {
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/locations/$locationId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -148,9 +161,9 @@ class OpenPricesAPIClient {
final int productId, {
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/products/$productId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -173,9 +186,9 @@ class OpenPricesAPIClient {
final String productCode, {
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/products/code/$productCode',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -201,9 +214,9 @@ class OpenPricesAPIClient {
static Future<MaybeError<String>> getStatus({
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/status',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand Down Expand Up @@ -234,9 +247,9 @@ class OpenPricesAPIClient {
final bool setCookie = false,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/auth${setCookie ? '?set_cookie=1' : ''}',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await post(
uri,
Expand All @@ -261,9 +274,9 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/session',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -288,9 +301,9 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/session',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doDeleteRequest(
uri,
Expand All @@ -308,9 +321,9 @@ class OpenPricesAPIClient {
required final String bearerToken,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/prices',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final StringBuffer body = StringBuffer();
body.write('{');
Expand Down Expand Up @@ -372,9 +385,9 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/prices/$priceId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doDeleteRequest(
uri,
Expand All @@ -393,10 +406,10 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/proofs',
queryParameters: parameters.getQueryParameters(),
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -423,9 +436,9 @@ class OpenPricesAPIClient {
required final String bearerToken,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/proofs/upload',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);

final http.MultipartRequest request = http.MultipartRequest('POST', uri);
Expand Down Expand Up @@ -473,9 +486,9 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/proofs/$proofId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand All @@ -501,9 +514,9 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
required final String bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/proofs/$proofId',
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doDeleteRequest(
uri,
Expand All @@ -521,10 +534,10 @@ class OpenPricesAPIClient {
final UriProductHelper uriHelper = uriHelperFoodProd,
final String? bearerToken,
}) async {
final Uri uri = uriHelper.getUri(
final Uri uri = getUri(
path: '/api/v1/users',
queryParameters: parameters.getQueryParameters(),
forcedHost: _getHost(uriHelper),
uriHelper: uriHelper,
);
final Response response = await HttpHelper().doGetRequest(
uri,
Expand Down
12 changes: 12 additions & 0 deletions lib/src/prices/currency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -927,4 +927,16 @@ enum Currency {

/// Not really attached to a specific country at all.
final bool noCountry;

static Currency? fromName(final String? name) {
if (name == null) {
return null;
}
for (final Currency currency in values) {
if (currency.name == name) {
return currency;
}
}
return null;
}
}
12 changes: 8 additions & 4 deletions lib/src/prices/price.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,35 @@ class Price extends JsonObject {
@JsonKey(name: 'proof_id')
int? proofId;

/// Price ID. Read-only.
@JsonKey()
late int id;

/// Product ID. Read-only.
@JsonKey(name: 'product_id')
int? productId;

/// Location ID. Read-only.
@JsonKey(name: 'location_id')
int? locationId;

/// Proof.
/// Proof. Read-only.
@JsonKey()
Proof? proof;

/// Location.
/// Location. Read-only.
@JsonKey()
Location? location;

/// Product.
/// Product. Read-only.
@JsonKey()
PriceProduct? product;

/// Owner.
/// Owner. Read-only.
@JsonKey()
late String owner;

/// Creation timestamp. Read-only.
@JsonKey(fromJson: JsonHelper.stringTimestampToDate)
late DateTime created;

Expand Down
20 changes: 19 additions & 1 deletion lib/src/prices/proof.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'package:json_annotation/json_annotation.dart';
import 'proof_type.dart';

import '../interface/json_object.dart';
import '../open_prices_api_client.dart';
import '../utils/json_helper.dart';
import '../utils/uri_helper.dart';

part 'proof.g.dart';

Expand All @@ -11,25 +13,31 @@ part 'proof.g.dart';
/// cf. `ProofFull` in https://prices.openfoodfacts.org/api/docs
@JsonSerializable()
class Proof extends JsonObject {
/// Proof ID. Read-only.
@JsonKey()
late int id;

/// Image file path. Read-only.
@JsonKey(name: 'file_path')
String? filePath;

/// Mime type. Read-only.
@JsonKey()
late String mimetype;

/// Proof type. Read-only.
@JsonKey()
ProofType? type;

/// Number of prices for this proof.
/// Number of prices for this proof. Read-only.
@JsonKey(name: 'price_count')
late int priceCount;

/// Owner. Read-only.
@JsonKey()
late String owner;

/// Creation timestamp. Read-only.
@JsonKey(fromJson: JsonHelper.stringTimestampToDate)
late DateTime created;

Expand All @@ -39,4 +47,14 @@ class Proof extends JsonObject {

@override
Map<String, dynamic> toJson() => _$ProofToJson(this);

/// Returns the URL of the proof image.
Uri? getFileUrl({required final UriProductHelper uriProductHelper}) =>
filePath == null
? null
: OpenPricesAPIClient.getUri(
path: 'img/$filePath',
uriHelper: uriProductHelper,
addUserAgentParameters: false,
);
}
4 changes: 4 additions & 0 deletions lib/src/prices/proof_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ enum ProofType implements OffTagged {

@override
final String offTag;

/// Returns the first [ProofType] that matches the [offTag].
static ProofType? fromOffTag(final String? offTag) =>
OffTagged.fromOffTag(offTag, ProofType.values) as ProofType?;
}
1 change: 0 additions & 1 deletion test/api_get_taxonomy_origins_server_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:test/expect.dart';
import 'package:test/test.dart';

import 'test_constants.dart';
Expand Down
2 changes: 1 addition & 1 deletion test/api_get_user_products_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void main() {
group('$OpenFoodAPIClient get user products', () {
const String userId = 'monsieurtanuki';
// should be big enough to get everything on page1
const int pageSize = 1000;
const int pageSize = 100;
final String toBeCompletedTag = ProductState.COMPLETED.toBeCompletedTag;

Future<int> getCount(
Expand Down
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
Loading