@@ -25,146 +25,11 @@ class ApiProvider {
2525 baseUrl ?? = AppUrls .baseUrl;
2626 }
2727
28- final http.Client _client;
29-
3028 String ? baseUrl;
3129
30+ final http.Client _client;
3231 final _networkService = NetworkController .instance;
3332
34- /// This is the method that is called from the service class.
35- Future <dynamic > _catchAsyncApiError ({
36- String ? baseUrl,
37- required String endPoint,
38- required String method,
39- required String feature,
40- Map <String , dynamic >? body,
41- Map <String , String >? headers,
42- Map <String , dynamic >? queryParams,
43- }) async {
44- AppUtility .log ('$feature Request' );
45-
46- if (_networkService.isConnected == false ) {
47- AppUtility .log ('Error: No network connection' , tag: 'error' );
48- RouteService .set (RouteStatus .noNetwork);
49- return ;
50- }
51-
52- var url = Uri .parse ((baseUrl ?? this .baseUrl! ) + endPoint);
53-
54- if (queryParams != null && queryParams.isNotEmpty) {
55- url = url.replace (queryParameters: queryParams);
56- }
57-
58- AppUtility .log ('URL: $url ' );
59-
60- var headersWithContentType = {
61- "content-type" : "application/json" ,
62- };
63-
64- if (headers != null ) {
65- headersWithContentType.addAll (headers);
66- }
67-
68- try {
69- switch (method) {
70-
71- /// GET request
72- case "GET" :
73- var response = await _client.get (
74- url,
75- headers: headersWithContentType,
76- );
77-
78- var decodedData = jsonDecode (utf8.decode (response.bodyBytes));
79-
80- if (response.statusCode == 200 || response.statusCode == 201 ) {
81- AppUtility .log ('$feature Request Success' );
82- return ResponseData (data: decodedData, isSuccessful: true );
83- } else {
84- AppUtility .log ('$feature Request Error' , tag: 'error' );
85- AppUtility .log (
86- 'Error: ${response .statusCode } ${response .reasonPhrase } ${response .body }' );
87- return ResponseData (data: decodedData, isSuccessful: false );
88- }
89-
90- /// POST request
91- case "POST" :
92- var response = await _client.post (
93- url,
94- body: jsonEncode (body),
95- headers: headersWithContentType,
96- );
97-
98- var decodedData = jsonDecode (utf8.decode (response.bodyBytes));
99-
100- if (response.statusCode == 200 || response.statusCode == 201 ) {
101- AppUtility .log ('$feature Request Success' );
102- return ResponseData (data: decodedData, isSuccessful: true );
103- } else {
104- AppUtility .log ('$feature Request Error' , tag: 'error' );
105- AppUtility .log (
106- 'Error: ${response .statusCode } ${response .reasonPhrase } ${response .body }' );
107- return ResponseData (data: decodedData, isSuccessful: false );
108- }
109-
110- /// PUT request
111- case "PUT" :
112- var response = await _client.put (
113- url,
114- body: jsonEncode (body),
115- headers: headersWithContentType,
116- );
117-
118- var decodedData = jsonDecode (utf8.decode (response.bodyBytes));
119-
120- if (response.statusCode == 200 || response.statusCode == 201 ) {
121- AppUtility .log ('$feature Request Success' );
122- return ResponseData (data: decodedData, isSuccessful: true );
123- } else {
124- AppUtility .log ('$feature Request Error' , tag: 'error' );
125- AppUtility .log (
126- 'Error: ${response .statusCode } ${response .reasonPhrase } ${response .body }' );
127- return ResponseData (data: decodedData, isSuccessful: false );
128- }
129-
130- /// DELETE request
131- case "DELETE" :
132- var response = await _client.delete (
133- url,
134- headers: headersWithContentType,
135- );
136-
137- var decodedData = jsonDecode (utf8.decode (response.bodyBytes));
138-
139- if (response.statusCode == 200 || response.statusCode == 201 ) {
140- AppUtility .log ('$feature Request Success' );
141- return ResponseData (data: decodedData, isSuccessful: true );
142- } else {
143- AppUtility .log ('$feature Request Error' , tag: 'error' );
144- AppUtility .log (
145- 'Error: ${response .statusCode } ${response .reasonPhrase } $decodedData ' );
146- return ResponseData (data: decodedData, isSuccessful: false );
147- }
148- }
149- } on SocketException {
150- AppUtility .log ('$feature Request Error' , tag: 'error' );
151- AppUtility .log ('Error: No Internet Connection' , tag: 'error' );
152- throw AppException ('No Internet Connection' );
153- } on TimeoutException {
154- AppUtility .log ('$feature Request Error' , tag: 'error' );
155- AppUtility .log ('Error: Request Timeout' , tag: 'error' );
156- throw AppException ('Request Timeout' );
157- } on FormatException catch (e) {
158- AppUtility .log ('$feature Request Error' , tag: 'error' );
159- AppUtility .log ('Format Exception: $e ' , tag: 'error' );
160- throw AppException ('Format Exception: $e ' );
161- } catch (exc) {
162- AppUtility .log ('$feature Request Error' , tag: 'error' );
163- AppUtility .log ('Error: $exc ' , tag: 'error' );
164- throw AppException (exc.toString ());
165- }
166- }
167-
16833 /// --------------------------------------------------------------------------
16934
17035 /// Server -------------------------------------------------------------------
@@ -1639,6 +1504,140 @@ class ApiProvider {
16391504
16401505 return response;
16411506 }
1507+
1508+ /// This is the method that is called from the service class.
1509+ Future <dynamic > _catchAsyncApiError ({
1510+ String ? baseUrl,
1511+ required String endPoint,
1512+ required String method,
1513+ required String feature,
1514+ Map <String , dynamic >? body,
1515+ Map <String , String >? headers,
1516+ Map <String , dynamic >? queryParams,
1517+ }) async {
1518+ AppUtility .log ('$feature Request' );
1519+
1520+ if (_networkService.isConnected == false ) {
1521+ AppUtility .log ('Error: No network connection' , tag: 'error' );
1522+ RouteService .set (RouteStatus .noNetwork);
1523+ return ;
1524+ }
1525+
1526+ var url = Uri .parse ((baseUrl ?? this .baseUrl! ) + endPoint);
1527+
1528+ if (queryParams != null && queryParams.isNotEmpty) {
1529+ url = url.replace (queryParameters: queryParams);
1530+ }
1531+
1532+ AppUtility .log ('URL: $url ' );
1533+
1534+ var headersWithContentType = {
1535+ "content-type" : "application/json" ,
1536+ };
1537+
1538+ if (headers != null ) {
1539+ headersWithContentType.addAll (headers);
1540+ }
1541+
1542+ try {
1543+ switch (method) {
1544+
1545+ /// GET request
1546+ case "GET" :
1547+ var response = await _client.get (
1548+ url,
1549+ headers: headersWithContentType,
1550+ );
1551+
1552+ var decodedData = jsonDecode (utf8.decode (response.bodyBytes));
1553+
1554+ if (response.statusCode == 200 || response.statusCode == 201 ) {
1555+ AppUtility .log ('$feature Request Success' );
1556+ return ResponseData (data: decodedData, isSuccessful: true );
1557+ } else {
1558+ AppUtility .log ('$feature Request Error' , tag: 'error' );
1559+ AppUtility .log (
1560+ 'Error: ${response .statusCode } ${response .reasonPhrase } ${response .body }' );
1561+ return ResponseData (data: decodedData, isSuccessful: false );
1562+ }
1563+
1564+ /// POST request
1565+ case "POST" :
1566+ var response = await _client.post (
1567+ url,
1568+ body: jsonEncode (body),
1569+ headers: headersWithContentType,
1570+ );
1571+
1572+ var decodedData = jsonDecode (utf8.decode (response.bodyBytes));
1573+
1574+ if (response.statusCode == 200 || response.statusCode == 201 ) {
1575+ AppUtility .log ('$feature Request Success' );
1576+ return ResponseData (data: decodedData, isSuccessful: true );
1577+ } else {
1578+ AppUtility .log ('$feature Request Error' , tag: 'error' );
1579+ AppUtility .log (
1580+ 'Error: ${response .statusCode } ${response .reasonPhrase } ${response .body }' );
1581+ return ResponseData (data: decodedData, isSuccessful: false );
1582+ }
1583+
1584+ /// PUT request
1585+ case "PUT" :
1586+ var response = await _client.put (
1587+ url,
1588+ body: jsonEncode (body),
1589+ headers: headersWithContentType,
1590+ );
1591+
1592+ var decodedData = jsonDecode (utf8.decode (response.bodyBytes));
1593+
1594+ if (response.statusCode == 200 || response.statusCode == 201 ) {
1595+ AppUtility .log ('$feature Request Success' );
1596+ return ResponseData (data: decodedData, isSuccessful: true );
1597+ } else {
1598+ AppUtility .log ('$feature Request Error' , tag: 'error' );
1599+ AppUtility .log (
1600+ 'Error: ${response .statusCode } ${response .reasonPhrase } ${response .body }' );
1601+ return ResponseData (data: decodedData, isSuccessful: false );
1602+ }
1603+
1604+ /// DELETE request
1605+ case "DELETE" :
1606+ var response = await _client.delete (
1607+ url,
1608+ headers: headersWithContentType,
1609+ );
1610+
1611+ var decodedData = jsonDecode (utf8.decode (response.bodyBytes));
1612+
1613+ if (response.statusCode == 200 || response.statusCode == 201 ) {
1614+ AppUtility .log ('$feature Request Success' );
1615+ return ResponseData (data: decodedData, isSuccessful: true );
1616+ } else {
1617+ AppUtility .log ('$feature Request Error' , tag: 'error' );
1618+ AppUtility .log (
1619+ 'Error: ${response .statusCode } ${response .reasonPhrase } $decodedData ' );
1620+ return ResponseData (data: decodedData, isSuccessful: false );
1621+ }
1622+ }
1623+ } on SocketException {
1624+ AppUtility .log ('$feature Request Error' , tag: 'error' );
1625+ AppUtility .log ('Error: No Internet Connection' , tag: 'error' );
1626+ throw AppException ('No Internet Connection' );
1627+ } on TimeoutException {
1628+ AppUtility .log ('$feature Request Error' , tag: 'error' );
1629+ AppUtility .log ('Error: Request Timeout' , tag: 'error' );
1630+ throw AppException ('Request Timeout' );
1631+ } on FormatException catch (e) {
1632+ AppUtility .log ('$feature Request Error' , tag: 'error' );
1633+ AppUtility .log ('Format Exception: $e ' , tag: 'error' );
1634+ throw AppException ('Format Exception: $e ' );
1635+ } catch (exc) {
1636+ AppUtility .log ('$feature Request Error' , tag: 'error' );
1637+ AppUtility .log ('Error: $exc ' , tag: 'error' );
1638+ throw AppException (exc.toString ());
1639+ }
1640+ }
16421641}
16431642
16441643/// ----------------------------------------------------------------------------
0 commit comments