Skip to content

Commit

Permalink
fix: 970 - refactored around new Prices API (#971)
Browse files Browse the repository at this point in the history
Impacted files:
* `api_prices_test.dart`: refactored around new Prices API
* `open_prices_api_client.dart`: different status code for "delete user session"; simplified `updatePrice` and `updateProof`
* `price_user.dart`: field `isModerator` is now optional
* `price_user.g.dart`: generated
* `session.dart`: additional status codes and error messages
  • Loading branch information
monsieurtanuki authored Sep 7, 2024
1 parent 09fc147 commit a8c696b
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 159 deletions.
24 changes: 7 additions & 17 deletions lib/src/open_prices_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class OpenPricesAPIClient {
uriHelper: uriHelper,
bearerToken: bearerToken,
);
if (response.statusCode == 200) {
if (response.statusCode == 204) {
return MaybeError<bool>.value(true);
}
return MaybeError<bool>.responseError(response);
Expand Down Expand Up @@ -369,7 +369,7 @@ class OpenPricesAPIClient {
///
/// This endpoint requires authentication.
/// A user can update only owned prices.
static Future<MaybeError<Price>> updatePrice(
static Future<MaybeError<bool>> updatePrice(
final int priceId, {
required final UpdatePriceParameters parameters,
final UriProductHelper uriHelper = uriHelperFoodProd,
Expand All @@ -388,14 +388,9 @@ class OpenPricesAPIClient {
addUserAgentParameters: false,
);
if (response.statusCode == 200) {
try {
final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response);
return MaybeError<Price>.value(Price.fromJson(decodedResponse));
} catch (e) {
//
}
return MaybeError<bool>.value(true);
}
return MaybeError<Price>.responseError(response);
return MaybeError<bool>.responseError(response);
}

/// Deletes a price.
Expand Down Expand Up @@ -540,7 +535,7 @@ class OpenPricesAPIClient {
///
/// This endpoint requires authentication.
/// A user can update only owned proofs.
static Future<MaybeError<Proof>> updateProof(
static Future<MaybeError<bool>> updateProof(
final int proofId, {
required final UpdateProofParameters parameters,
final UriProductHelper uriHelper = uriHelperFoodProd,
Expand All @@ -559,14 +554,9 @@ class OpenPricesAPIClient {
addUserAgentParameters: false,
);
if (response.statusCode == 200) {
try {
final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response);
return MaybeError<Proof>.value(Proof.fromJson(decodedResponse));
} catch (e) {
//
}
return MaybeError<bool>.value(true);
}
return MaybeError<Proof>.responseError(response);
return MaybeError<bool>.responseError(response);
}

/// Deletes a proof.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/prices/price_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PriceUser extends JsonObject {

/// Number of prices for this user.
@JsonKey(name: 'is_moderator')
late bool isModerator;
bool? isModerator;

PriceUser();

Expand Down
2 changes: 1 addition & 1 deletion lib/src/prices/price_user.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions lib/src/prices/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class Session extends JsonObject {
@override
Map<String, dynamic> toJson() => _$SessionToJson(this);

/// Status Code when the authentication fails.
static const int invalidAuthStatusCode = 401;

/// Error message when the authentication fails.
static const String invalidAuthMessage = 'Invalid authentication credentials';

/// Status Code when we try an edit operation with a wrong authentication.
static const int invalidActionWithAuthStatusCode = 403;

/// Error message when we try an edit operation with a wrong authentication.
static const String invalidActionWithAuthMessage =
'Authentication credentials were not provided.';
}
Loading

0 comments on commit a8c696b

Please sign in to comment.