1+ import 'dart:developer' ;
2+
13import 'package:dio/dio.dart' ;
24import 'package:flutter/foundation.dart' ;
5+ import 'package:get/get.dart' as nav;
36
47import '../../config/config.dart' ;
8+ import '../../constants/routes.dart' ;
9+ import '../../constants/strings.dart' ;
510import '../local/storage_service.dart' ;
611
712class ApiService {
@@ -19,32 +24,29 @@ class ApiService {
1924 Map <String , dynamic >? headers,
2025 Map <String , dynamic >? query,
2126 }) async {
27+ log ('[API] [GET] >> $endpoint ' );
2228 Response ? response;
2329 try {
2430 response = await _channel.get (
2531 (baseUrl ?? Environment .baseUrl) + endpoint,
2632 queryParameters: query,
2733 options: Options (
28- validateStatus: (status) {
29- return status! < 500 ;
30- },
34+ validateStatus: _validate,
3135 headers: headers,
3236 ),
3337 );
34- return {
35- 'status_code' : response.statusCode ?? 0 ,
36- 'data' : response.data ?? 'null' ,
37- };
3838 } on Exception catch (exception, stacktrace) {
3939 debugPrint (
4040 'ERROR: Failed during a GET request.\n '
4141 'Endpoint: $endpoint \n Query: $query \n '
4242 'Exception: $exception \n Stacktrace: $stacktrace ' ,
4343 );
44+ rethrow ;
4445 }
46+ log ('[API] [GET] << received ${response .statusCode } from $endpoint ' );
4547 return {
46- 'status_code' : response? .statusCode ?? 0 ,
47- 'data' : response? .data ?? 'null' ,
48+ 'status_code' : response.statusCode ?? 0 ,
49+ 'data' : response.data ?? 'null' ,
4850 };
4951 }
5052
@@ -54,32 +56,29 @@ class ApiService {
5456 Map <String , dynamic >? headers,
5557 Map <String , dynamic >? data,
5658 }) async {
59+ log ('[API] [POST] >> $endpoint ' );
5760 Response ? response;
5861 try {
5962 response = await _channel.post (
6063 Environment .baseUrl + endpoint,
6164 data: data,
6265 options: Options (
63- validateStatus: (status) {
64- return status! < 500 ;
65- },
66+ validateStatus: _validate,
6667 headers: headers,
6768 ),
6869 );
69- return {
70- 'status_code' : response.statusCode ?? 0 ,
71- 'data' : response.data ?? 'null' ,
72- };
7370 } on Exception catch (exception, stacktrace) {
7471 debugPrint (
7572 'ERROR: Failed during a POST request.\n '
7673 'Endpoint: $endpoint \n Data: $data \n '
7774 'Exception: $exception \n Stacktrace: $stacktrace ' ,
7875 );
76+ rethrow ;
7977 }
78+ log ('[API] [POST] << received ${response .statusCode } from $endpoint ' );
8079 return {
81- 'status_code' : response? .statusCode ?? 0 ,
82- 'data' : response? .data ?? 'null' ,
80+ 'status_code' : response.statusCode ?? 0 ,
81+ 'data' : response.data ?? 'null' ,
8382 };
8483 }
8584
@@ -89,32 +88,29 @@ class ApiService {
8988 Map <String , dynamic >? headers,
9089 Map <String , dynamic >? data,
9190 }) async {
91+ log ('[API] [PUT] >> $endpoint ' );
9292 Response ? response;
9393 try {
9494 response = await _channel.put (
9595 Environment .baseUrl + endpoint,
9696 data: data,
9797 options: Options (
98- validateStatus: (status) {
99- return status! < 500 ;
100- },
98+ validateStatus: _validate,
10199 headers: headers,
102100 ),
103101 );
104- return {
105- 'status_code' : response.statusCode ?? 0 ,
106- 'data' : response.data ?? 'null' ,
107- };
108102 } on Exception catch (exception, stacktrace) {
109103 debugPrint (
110104 'ERROR: Failed during a PUT request.\n '
111105 'Endpoint: $endpoint \n Data: $data \n '
112106 'Exception: $exception \n Stacktrace: $stacktrace ' ,
113107 );
108+ rethrow ;
114109 }
110+ log ('[API] [PUT] << received ${response .statusCode } from $endpoint ' );
115111 return {
116- 'status_code' : response? .statusCode ?? 0 ,
117- 'data' : response? .data ?? 'null' ,
112+ 'status_code' : response.statusCode ?? 0 ,
113+ 'data' : response.data ?? 'null' ,
118114 };
119115 }
120116
@@ -123,4 +119,12 @@ class ApiService {
123119 final token = StorageService .authToken;
124120 headers.addAll ({'authorization' : token});
125121 }
122+
123+ static bool _validate (int ? status) {
124+ if (status == 401 ) {
125+ StorageService .delete (AppStrings .authTokenKey);
126+ nav.Get .offAllNamed (AppRoutes .login);
127+ }
128+ return status! < 500 ;
129+ }
126130}
0 commit comments