@@ -6,7 +6,8 @@ import 'package:dio/dio.dart';
66import 'package:dio/io.dart' ;
77import 'package:ht_http_client/src/interceptors/auth_interceptor.dart' ;
88import 'package:ht_http_client/src/interceptors/error_interceptor.dart' ;
9- import 'package:ht_shared/ht_shared.dart' ; // Updated import
9+ import 'package:ht_shared/ht_shared.dart' ;
10+ import 'package:logging/logging.dart' ;
1011
1112/// {@template ht_http_client}
1213/// A robust HTTP client built on top of Dio, providing simplified API calls,
@@ -29,13 +30,16 @@ class HtHttpClient {
2930 /// or testing
3031 /// - list of additional [interceptors] to be added alongside
3132 /// the default Auth and Error interceptors.
33+ /// - [logger] : Optional [Logger] instance for logging HTTP requests and responses.
3234 HtHttpClient ({
3335 required String baseUrl,
3436 required TokenProvider tokenProvider,
3537 required bool isWeb,
3638 Dio ? dioInstance,
3739 List <Interceptor >? interceptors,
38- }) : _dio = dioInstance ?? Dio () {
40+ Logger ? logger,
41+ }) : _dio = dioInstance ?? Dio (),
42+ _logger = logger ?? Logger ('HtHttpClient' ) {
3943 // Configure base options
4044 _dio.options = BaseOptions (
4145 baseUrl: baseUrl,
@@ -68,6 +72,9 @@ class HtHttpClient {
6872 /// The configured Dio instance used for making requests.
6973 final Dio _dio;
7074
75+ /// The logger instance for this HTTP client.
76+ final Logger _logger;
77+
7178 /// Performs a GET request.
7279 ///
7380 /// - [path] : The endpoint path appended to the "baseUrl".
@@ -83,23 +90,27 @@ class HtHttpClient {
8390 Options ? options,
8491 CancelToken ? cancelToken,
8592 }) async {
93+ _logger.info (
94+ 'GET request to: $path , Query Parameters: $queryParameters ' ,
95+ );
8696 try {
8797 final response = await _dio.get <T >(
8898 path,
8999 queryParameters: queryParameters,
90100 options: options,
91101 cancelToken: cancelToken,
92102 );
103+ _logger.info ('GET request to $path successful. Status: ${response .statusCode }' );
93104 // Dio automatically throws for non-2xx, ErrorInterceptor maps it
94105 return response.data as T ;
95106 } on DioException catch (e) {
96- // ErrorInterceptor should have mapped this, but catch ensures propagation
97- // If the error embedded in DioException is our custom one, rethrow it.
98107 if (e.error is HtHttpException ) {
108+ _logger.severe (
109+ 'GET request to $path failed with HtHttpException: ${e .error }' ,
110+ );
99111 throw e.error! ;
100112 }
101- // Otherwise, rethrow the original DioException
102- // (should ideally not happen)
113+ _logger.severe ('GET request to $path failed with DioException: $e ' );
103114 rethrow ;
104115 }
105116 }
@@ -121,6 +132,9 @@ class HtHttpClient {
121132 Options ? options,
122133 CancelToken ? cancelToken,
123134 }) async {
135+ _logger.info (
136+ 'POST request to: $path , Query Parameters: $queryParameters , Data: $data ' ,
137+ );
124138 try {
125139 final response = await _dio.post <T >(
126140 path,
@@ -129,9 +143,16 @@ class HtHttpClient {
129143 options: options,
130144 cancelToken: cancelToken,
131145 );
146+ _logger.info ('POST request to $path successful. Status: ${response .statusCode }' );
132147 return response.data as T ;
133148 } on DioException catch (e) {
134- if (e.error is HtHttpException ) throw e.error! ;
149+ if (e.error is HtHttpException ) {
150+ _logger.severe (
151+ 'POST request to $path failed with HtHttpException: ${e .error }' ,
152+ );
153+ throw e.error! ;
154+ }
155+ _logger.severe ('POST request to $path failed with DioException: $e ' );
135156 rethrow ;
136157 }
137158 }
@@ -153,6 +174,9 @@ class HtHttpClient {
153174 Options ? options,
154175 CancelToken ? cancelToken,
155176 }) async {
177+ _logger.info (
178+ 'PUT request to: $path , Query Parameters: $queryParameters , Data: $data ' ,
179+ );
156180 try {
157181 final response = await _dio.put <T >(
158182 path,
@@ -161,9 +185,16 @@ class HtHttpClient {
161185 options: options,
162186 cancelToken: cancelToken,
163187 );
188+ _logger.info ('PUT request to $path successful. Status: ${response .statusCode }' );
164189 return response.data as T ;
165190 } on DioException catch (e) {
166- if (e.error is HtHttpException ) throw e.error! ;
191+ if (e.error is HtHttpException ) {
192+ _logger.severe (
193+ 'PUT request to $path failed with HtHttpException: ${e .error }' ,
194+ );
195+ throw e.error! ;
196+ }
197+ _logger.severe ('PUT request to $path failed with DioException: $e ' );
167198 rethrow ;
168199 }
169200 }
@@ -185,6 +216,9 @@ class HtHttpClient {
185216 Options ? options,
186217 CancelToken ? cancelToken,
187218 }) async {
219+ _logger.info (
220+ 'DELETE request to: $path , Query Parameters: $queryParameters , Data: $data ' ,
221+ );
188222 try {
189223 final response = await _dio.delete <T >(
190224 path,
@@ -193,9 +227,16 @@ class HtHttpClient {
193227 options: options,
194228 cancelToken: cancelToken,
195229 );
230+ _logger.info ('DELETE request to $path successful. Status: ${response .statusCode }' );
196231 return response.data as T ;
197232 } on DioException catch (e) {
198- if (e.error is HtHttpException ) throw e.error! ;
233+ if (e.error is HtHttpException ) {
234+ _logger.severe (
235+ 'DELETE request to $path failed with HtHttpException: ${e .error }' ,
236+ );
237+ throw e.error! ;
238+ }
239+ _logger.severe ('DELETE request to $path failed with DioException: $e ' );
199240 rethrow ;
200241 }
201242 }
0 commit comments