Aidra Connect 10.0.2+16
Aidra Connect Mobile Application
Loading...
Searching...
No Matches
api_client.dart
Go to the documentation of this file.
1import 'package:connect/core/config/environment_config.dart';
2import 'package:dio/dio.dart';
3
4import '../router/auth_interceptor.dart';
5import 'api_endpoints.dart';
6import 'api_exceptions.dart';
7
8class ApiClient {
9 late Dio _dio;
10
12 Duration connectTimeout = const Duration(seconds: 30),
13 Duration receiveTimeout = const Duration(seconds: 30),
14 Duration sendTimeout = const Duration(seconds: 30),
15 }) {
16 _dio = Dio(
17 BaseOptions(
19 connectTimeout: Duration(milliseconds: EnvironmentConfig.apiTimeout),
20 receiveTimeout: Duration(milliseconds: EnvironmentConfig.apiTimeout),
21 sendTimeout: Duration(milliseconds: EnvironmentConfig.apiTimeout),
22 contentType: 'application/json',
23 headers: {'Accept': 'application/json'},
24 ),
25 );
26 _dio.interceptors.add(AuthInterceptor(dio: _dio));
27 }
28
29 Future<dynamic> get(
30 String path, [
31 Map<String, dynamic>? headers,
32 bool? isAuth,
33 String? url,
34 ]) async {
35 final response = await _dio.get(
36 url ??
38 path,
39 isAuth,
40 ),
41 options: Options(headers: headers),
42 );
43 return _handleResponse(response);
44 }
45
46 Future<Map<String, dynamic>> post({
47 required String path,
48 required Map<String, dynamic> data,
49 Map<String, dynamic>? headers,
50 bool? isAuth,
51 }) async {
52 print(
54 path,
55 isAuth,
56 ),
57 );
58 print(data);
59 final response = await _dio.post(
61 path,
62 isAuth,
63 ),
64 data: data,
65 options: Options(headers: headers),
66 );
67 return _handleResponse(response);
68 }
69
70 dynamic _handleResponse(Response response) {
71 if (response.statusCode == 204) {
72 return {};
73 }
74 if (response.statusCode == 200 ||
75 (response.data['statusCodeValue'] == 200 ||
76 !response.data.containsKey('statusCodeValue'))) {
77 return response.data;
78 } else if (response.statusCode == 200 &&
79 response.data['code'] == 400 &&
80 response.data['message'] == 'NO Data Found') {
81 return {};
82 } else {
83 throw ApiException(
84 response.data["statusCodeValue"],
85 {
86 "statusCode": response.data['statusCode'],
87 "message": response.data["body"]["error"],
88 },
89 );
90 }
91 }
92}
dynamic _handleResponse(Response response)
Future< dynamic > get(String path, [Map< String, dynamic >? headers, bool? isAuth, String? url,]) async
Future< Map< String, dynamic > > post({ required String path, required Map< String, dynamic > data, Map< String, dynamic >? headers, bool? isAuth, }) async
ApiClient({ Duration connectTimeout=const Duration(seconds:30), Duration receiveTimeout=const Duration(seconds:30), Duration sendTimeout=const Duration(seconds:30), })
late Dio _dio
static String buildUrl(String path, bool? isAuthUrl)
static String get apiBaseUrl
class EnergySavingsMeasure extends StatefulWidget options