Skip to content

Commit d2d2c55

Browse files
committed
bumped to version 1.0.1+31
1 parent 1635f24 commit d2d2c55

22 files changed

+1445
-1458
lines changed

lib/apis/providers/api_provider.dart

Lines changed: 135 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/// ----------------------------------------------------------------------------

lib/apis/providers/socket_api_provider.dart

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@ import 'package:social_media_app/constants/urls.dart';
77
import 'package:social_media_app/utils/utility.dart';
88

99
class SocketApiProvider {
10-
/// A static private instance to access _socketApi from inside class only
11-
static final SocketApiProvider _socketApi = SocketApiProvider._internal();
12-
13-
/// An internal private constructor to access it for only once for static
14-
/// instance of class.
15-
SocketApiProvider._internal();
16-
1710
/// Factory constructor to return same static instance everytime you
1811
/// create any object.
1912
factory SocketApiProvider() {
2013
return _socketApi;
2114
}
2215

16+
/// An internal private constructor to access it for only once for static
17+
/// instance of class.
18+
SocketApiProvider._internal();
19+
2320
/// All the private and public variables
2421
static WebSocket? _socket;
25-
StreamController<dynamic>? _socketEventStream;
22+
23+
/// A static private instance to access _socketApi from inside class only
24+
static final SocketApiProvider _socketApi = SocketApiProvider._internal();
25+
2626
var _isDisposed = false;
27+
StreamController<dynamic>? _socketEventStream;
2728

2829
Stream<dynamic>? get socketEventStream =>
2930
_socketEventStream?.stream.asBroadcastStream();
@@ -92,11 +93,6 @@ class SocketApiProvider {
9293
AppUtility.log("Socket initialized");
9394
}
9495

95-
void _socketEventHandler(dynamic event) {
96-
AppUtility.log("Socket event received");
97-
_socketApi._socketEventStream?.add(event);
98-
}
99-
10096
void dispose() {
10197
AppUtility.log("Socket disposing...");
10298
_socket?.close();
@@ -124,4 +120,9 @@ class SocketApiProvider {
124120
void sendJson(Map<String, dynamic> json) {
125121
_socket?.add(jsonEncode(json));
126122
}
123+
124+
void _socketEventHandler(dynamic event) {
125+
AppUtility.log("Socket event received");
126+
_socketApi._socketEventStream?.add(event);
127+
}
127128
}

0 commit comments

Comments
 (0)