Skip to content

Commit

Permalink
Improvements to API error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
alefealvessilva committed Mar 25, 2023
1 parent 815db2a commit 28ea751
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 36 deletions.
26 changes: 5 additions & 21 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
## [1.0.0] - 25-03-2023

- Defined the architecture of the project

## Breaking Changes
### This release generated BREAKING CHANGES see the docs!

- Defined the architecture of the project
- Improvements were made to the method for querying received transactions see the docs for more information
- Improvements all error cases see the docs for more information

- Changed variable name in PixBB Instantiate :

- dev_app_key => developerApplicationKey
## [0.1.7] - 25-03-2023

- Changed the way to use the fetchTransaction method:
- Before
```dart
pixBB.getToken().then(
(token) => pixBB.fetchRecentReceivedTransactions(
accessToken: token.accessToken,
);
```
- Afeter
```dart
pixBB.getToken().then(
(token) => pixBB.fetchTransactions(
token: token,
),
```
- Fixed testing errors
98 changes: 85 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,46 @@
</div>
<br>

## [v1.0.0] Breaking Changes

- Changed variable name in PixBB Instantiate :

- dev_app_key => developerApplicationKey

- Changed the way to use the fetchTransaction method:
- Before

```dart
final listPix = await pixBB.fetchRecentReceivedTransactions(
accessToken: token.accessToken,
);
```

- After

```dart
final listPix = await pixBB.fetchTransactions(
token: token,
);
//Returns the last 4 days transactions
```

```dart
final listPix = await pixBB.fetchTransactions(
token: token,
dateTimeRange: DateTimeRange(
start: DateTime.now().subtract(Duration(days: 1)),
end: DateTime.now(),
);
);
//Returns the transactions of the specified date range
// Attention the maximum difference between the dates must be 4 days
```

<br>

---

<!-- TABLE OF CONTENTS -->
<details>
<summary>Table of Contents</summary>
Expand Down Expand Up @@ -174,34 +214,66 @@ _For more examples, please refer to the_ [Documentation](https://pub.dev/documen

## Use cases

**Request an access token**
### **Request an access token**

Request a token

```dart
accessToken = await pixBB.getToken();
final token = await pixBB.getToken();
```

**Get recent trasactions**
### **Fetch Pix Transactions**

Request a list of transactions received from the last 4 days
- Default Time Range

```dart
await getRecentReceivedTransactions(accessToken: ccessToken);
final listPix = await pixBB.fetchTransactions(
token: token,
);
//Returns the last 4 days transactions
```

**Get transactions by date**
- Custom Time Range

Request a list of transactions as of a specific date
```dart
final listPix = await pixBB.fetchTransactions(
token: token,
dateTimeRange: DateTimeRange(
start: DateTime.now().subtract(Duration(days: 1)),
end: DateTime.now(),
);
);
//Returns the transactions of the specified date range
// Attention the maximum difference between the dates must be 4 days
```

### **Handling errors**

⚠️ Attention the maximum difference in days between the start and end date must be 4 days ⚠️
- This package provides several ways to handle errors that may occur.

```dart
await getTransactionsByDate(
accessToken: accessToken,
initialDate: DateTime.now().subtract(const Duration(days: 4)),
finalDate: DateTime.now(),
);
try {
// Code that may throw an exception
} on BBDateException catch (e) {
// Handle error in date range
print('BB Date error: ${e.message}');
} on BBApiException catch (e) {
// Handle error response from Banco do Brasil's API
print('BB API error: ${e.message}');
} on BBCertificateException catch (e) {
// Handle error with Pix certificate
print('Pix certificate error: ${e.message}');
} on BBHttpException catch (e) {
// Handle HTTP request error
print('HTTP request error: ${e.message}');
} on BBUnknownException catch (e) {
// Handle unknown error
print('Unknown error: ${e.message}');
} catch (e) {
// Handle other types of exceptions
print('Unexpected error: $e');
}
```

<!-- CONTRIBUTING -->
Expand Down
19 changes: 19 additions & 0 deletions lib/pix_bb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ enum Ambiente {
homologacao,
}

/// The main class that provides the Pix Banco do Brasil's API functionalities.
///
/// This class encapsulates the operations related to:
/// - Authentication token management,
/// - Transaction retrieval.
class PixBB {
final Ambiente _ambiente;
final String _basicKey;
Expand Down Expand Up @@ -50,13 +55,27 @@ class PixBB {
}
}

/// Retrieves a valid token from the API.
///
/// Returns a [Future] with a [Token] object representing the token.
///
/// Throws a [PixException] if there's an error while fetching the token.
Future<Token> getToken() {
final repository = TokenRepository(_client);
return repository
.getToken(url: _tokenUrl, basicKey: _basicKey)
.getOrThrow();
}

/// Retrieves a list of transactions from the API.
///
/// [token] is a valid [Token] object used for authentication.
///
/// [dateTimeRange] is an optional [DateTimeRange] object representing the range of dates to retrieve transactions from.
///
/// Returns a [Future] with a [List] of [Pix] objects representing the transactions.
///
/// Throws a [PixException] if there's an error while fetching the transactions.
Future<List<Pix>> fetchTransactions({
required Token token,
DateTimeRange? dateTimeRange,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/errors/bb_api_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BBApiException implements PixException {
@override
String get message => _error;

/// The error message returned by the API.
/// The error data returned by the API.
Map<String, dynamic> get errorData => _errorData;

/// The type of exception, which in this case is always [ApiErrorType.apiErrorType].
Expand All @@ -39,7 +39,7 @@ class BBApiException implements PixException {

/// Creates a new [PixException] instance for an API error with the given [error].
static PixException apiError(Map<String, dynamic> errorMap) {
String errorMessage = 'uncaughtError';
String errorMessage = 'uncaughtMessage';

if (errorMap.containsKey('error_description')) {
errorMessage = errorMap['error_description'];
Expand Down
2 changes: 2 additions & 0 deletions lib/src/errors/bb_http_exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class BBHttpException implements PixException {
}) : _error = error,
_type = type;

/// Exception to represent a http-related type.
@override
Enum get exceptionType => _type;

/// Exception to represent a http-related error.
@override
dynamic get message => _error;

Expand Down

0 comments on commit 28ea751

Please sign in to comment.