Skip to content

Commit

Permalink
Merge pull request #4 from yzzzd/beta
Browse files Browse the repository at this point in the history
2.0.1(2)
  • Loading branch information
yzzzd authored Feb 22, 2024
2 parents 8fcae6f + 720228b commit 01fd314
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@

## 2.0.1 (2024-02)

* Use ``ApiDataResponse<T>`` to get single line hit API with ``runInlineData()`` function
* Use your own class ``Response`` that should extends from ``ApiResponse`` to get single line hit API with ``runInline()`` function. The ``response`` object will **still** be of type ``Response`` even if there is an error response.

```dart
final response = await ApiObserver.runInline<LoginResponse>(api: () => _apiService.login(username, password), creator: () => LoginResponse('', null));
if (response.successed()) {
// TODO: something.
} else {
// TODO: something else.
}
```

* Alternate, you can use ``ApiDataResponse<T>`` to get single line hit API with ``runInlineData()`` function, if you don\'t want provide the ``creator function``

```dart
@POST('/login')
Expand Down
46 changes: 26 additions & 20 deletions lib/api/api_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,17 @@ import 'package:flutter_core/api/api_response.dart';
import 'package:flutter_core/api/core_api_response.dart';

class ApiObserver {
/* static Future<T> run1<T extends ApiResponse>(Function() api) async {
var response;
try {
response = await api();
} on DioException catch (e) {
//response = ApiResponse.fromJson(e.response?.data);
} catch (e) {
response = ApiResponse(500, '$e');
}
return response;
} */

static run<T extends CoreApiResponse>({required Function() api, required Function(T response) onSuccess, required Function(ApiResponse response) onError}) async {
try {
final response = await api();
response.code = ApiCode.success;
onSuccess(response);
} on DioException catch (e) {
final ApiResponse response;
if (e.response?.statusCode == ApiCode.error) {
response = ApiResponse('${e.response?.data}');
final response = ApiResponse('');
if (e.response?.data == null) {
response.message = '${e.message}';
} else {
if (e.response?.data == null) {
response = ApiResponse('${e.response?.data}');
} else {
response = ApiResponse.fromJson(e.response?.data);
}
response.message = '${e.response?.data['message']}';
}
response.code = e.response?.statusCode ?? ApiCode.error;
onError(response);
Expand All @@ -42,6 +26,28 @@ class ApiObserver {
}
}

static Future<T> runInline<T extends ApiResponse>({required Function() api, required T Function() creator}) async {
try {
final response = await api();
response.code = ApiCode.success;
return response;
} on DioException catch (e) {
final response = creator();
if (e.response?.data == null) {
response.message = '${e.message}';
} else {
response.message = '${e.response?.data['message']}';
}
response.code = e.response?.statusCode ?? ApiCode.error;
return response;
} catch (e) {
final response = creator();
response.code = ApiCode.error;
response.message = '$e';
return response;
}
}

static runWithData({required Function() api, required Function(ApiDataResponse response) onSuccess, required Function(ApiDataResponse response) onError}) async {
try {
final response = await api();
Expand Down
2 changes: 1 addition & 1 deletion lib/api/api_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ part 'api_response.g.dart';
class ApiResponse extends CoreApiResponse {

@JsonKey(name: 'message')
final String message;
String message;

ApiResponse(this.message);

Expand Down

0 comments on commit 01fd314

Please sign in to comment.