diff --git a/CHANGELOG.md b/CHANGELOG.md index 291ba8b..67ec46c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,19 @@ ## 2.0.1 (2024-02) -* Use ``ApiDataResponse`` 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(api: () => _apiService.login(username, password), creator: () => LoginResponse('', null)); + +if (response.successed()) { + // TODO: something. +} else { + // TODO: something else. +} +``` + +* Alternate, you can use ``ApiDataResponse`` to get single line hit API with ``runInlineData()`` function, if you don\'t want provide the ``creator function`` ```dart @POST('/login') diff --git a/lib/api/api_observer.dart b/lib/api/api_observer.dart index 9a5bcf0..8599ba5 100644 --- a/lib/api/api_observer.dart +++ b/lib/api/api_observer.dart @@ -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 run1(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({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); @@ -42,6 +26,28 @@ class ApiObserver { } } + static Future runInline({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(); diff --git a/lib/api/api_response.dart b/lib/api/api_response.dart index bc08440..8a04929 100644 --- a/lib/api/api_response.dart +++ b/lib/api/api_response.dart @@ -7,7 +7,7 @@ part 'api_response.g.dart'; class ApiResponse extends CoreApiResponse { @JsonKey(name: 'message') - final String message; + String message; ApiResponse(this.message);