Skip to content

Commit

Permalink
fix: added "app_name" parameter for prices methods uploadProof and cr…
Browse files Browse the repository at this point in the history
…eatePrice

Impacted files:
* `api_get_taxonomy_origins_server_test.dart`: minor refactoring
* `api_get_user_products_test.dart`: set max page size to new server max page size (100)
* `api_search_products_test.dart`: set default page size to new server default page size (50)
* `api_suggestion_manager_test.dart`: now targeting PROD; minor refactoring
* `currency.dart`: new `fromName` method
* `open_prices_api_client.dart`: added `app_name` parameter for `uploadProof` and `createPrice`; refactored around new method `getUri`
* `page_size.dart`: added comments
* `price.dart`: added comments
* `proof.dart`: new `getFileUrl` method; added comments
* `proof_type.dart`: new `fromOffTag` method
  • Loading branch information
monsieurtanuki committed Jun 6, 2024
1 parent 1256fe6 commit 3a5607b
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 54 deletions.
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
91 changes: 57 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,26 @@ 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,
}) =>
uriHelper.getUri(
path: path,
queryParameters: queryParameters,
forcedHost: _getHost(uriHelper),
);

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 +85,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 +109,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 +136,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 +159,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 +184,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 +212,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 +245,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 +272,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 +299,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 +319,15 @@ class OpenPricesAPIClient {
required final String bearerToken,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final String? appName = OpenFoodAPIConfiguration.userAgent?.name;
final Uri uri = getUri(
path: '/api/v1/prices',
forcedHost: _getHost(uriHelper),
queryParameters: appName == null
? null
: <String, String>{
'app_name': appName,
},
uriHelper: uriHelper,
);
final StringBuffer body = StringBuffer();
body.write('{');
Expand Down Expand Up @@ -372,9 +389,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 +410,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 +440,15 @@ class OpenPricesAPIClient {
required final String bearerToken,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
final String? appName = OpenFoodAPIConfiguration.userAgent?.name;
final Uri uri = getUri(
path: '/api/v1/proofs/upload',
forcedHost: _getHost(uriHelper),
queryParameters: appName == null
? null
: <String, String>{
'app_name': appName,
},
uriHelper: uriHelper,
);

final http.MultipartRequest request = http.MultipartRequest('POST', uri);
Expand Down Expand Up @@ -473,9 +496,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 +524,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 +544,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
19 changes: 18 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,13 @@ 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,
);
}
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
Loading

0 comments on commit 3a5607b

Please sign in to comment.