Code updated
This commit is contained in:
@@ -4,7 +4,42 @@ import '../../resources/api_constants.dart';
|
||||
|
||||
class SignUpApiService {
|
||||
final String baseUrl = ApiConstants.baseUrl;
|
||||
final Dio dio = Dio();
|
||||
late final Dio dio;
|
||||
|
||||
SignUpApiService() {
|
||||
dio = Dio(BaseOptions(
|
||||
connectTimeout: const Duration(seconds: 10),
|
||||
receiveTimeout: const Duration(seconds: 10),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
String _formatError(dynamic e, String defaultMessage) {
|
||||
if (e is DioException) {
|
||||
if (e.response != null && e.response?.data != null) {
|
||||
final data = e.response!.data;
|
||||
if (data is Map) {
|
||||
if (data['message'] != null) return data['message'].toString();
|
||||
if (data['operationMessage'] != null) return data['operationMessage'].toString();
|
||||
if (data['error'] != null) return data['error'].toString();
|
||||
} else if (data is String && data.isNotEmpty) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
if (e.type == DioExceptionType.connectionTimeout ||
|
||||
e.type == DioExceptionType.sendTimeout ||
|
||||
e.type == DioExceptionType.receiveTimeout) {
|
||||
return 'Connection timed out. Please check your network and backend.';
|
||||
}
|
||||
if (e.type == DioExceptionType.connectionError) {
|
||||
return 'Cannot connect to server at $baseUrl. Please verify the backend is running.';
|
||||
}
|
||||
return e.message ?? defaultMessage;
|
||||
}
|
||||
return defaultMessage.isNotEmpty ? '$defaultMessage: $e' : e.toString();
|
||||
}
|
||||
|
||||
// get all account
|
||||
Future<List<Map<String, dynamic>>> getallAccount(String token) async {
|
||||
@@ -17,18 +52,15 @@ class SignUpApiService {
|
||||
print('response data is ... $responseData');
|
||||
|
||||
if (responseData is List) {
|
||||
// If the response is a list, cast it to the expected type
|
||||
final entities = responseData.cast<Map<String, dynamic>>();
|
||||
return entities;
|
||||
} else if (responseData is Map<String, dynamic>) {
|
||||
// If the response is a single object, wrap it in a list
|
||||
return [responseData];
|
||||
} else {
|
||||
// Handle other unexpected response types here
|
||||
throw Exception('Unexpected response type');
|
||||
}
|
||||
} catch (e) {
|
||||
throw Exception('Failed to Account: $e');
|
||||
throw Exception(_formatError(e, 'Failed to fetch Account'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,14 +68,18 @@ class SignUpApiService {
|
||||
Future<Map<String, dynamic>> createAccount(
|
||||
Map<String, dynamic> entity) async {
|
||||
try {
|
||||
// dio.options.headers['Authorization'] = 'Bearer $token';
|
||||
final response = await dio
|
||||
.post('$baseUrl/token/users/sysaccount/savesysaccount', data: entity);
|
||||
|
||||
print(' created account is $response');
|
||||
return response.data;
|
||||
print('created account is $response');
|
||||
if (response.data is Map<String, dynamic>) {
|
||||
return response.data;
|
||||
} else if (response.data is Map) {
|
||||
return Map<String, dynamic>.from(response.data);
|
||||
}
|
||||
return {'account_id': response.data.toString()};
|
||||
} catch (e) {
|
||||
throw Exception('Failed To Create Account: $e');
|
||||
throw Exception(_formatError(e, 'Failed to Create Account'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,41 +87,39 @@ class SignUpApiService {
|
||||
Future<void> sendEmail(Map<String, dynamic> entity) async {
|
||||
try {
|
||||
print("in post api...$entity");
|
||||
// dio.options.headers['Authorization'] = 'Bearer $token';
|
||||
await dio.post('$baseUrl/token/user/send_email', data: entity);
|
||||
print(entity);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to Send Email: $e');
|
||||
throw Exception(_formatError(e, 'Failed to Send Email'));
|
||||
}
|
||||
}
|
||||
|
||||
// RESEND EMAIL FOR OTP
|
||||
Future<void> resendEmail(String email) async {
|
||||
try {
|
||||
// dio.options.headers['Authorization'] = 'Bearer $token';
|
||||
await dio.post('$baseUrl/token/user/resend_otp?email=$email');
|
||||
} catch (e) {
|
||||
throw Exception('Failed to ReSend Email: $e');
|
||||
throw Exception(_formatError(e, 'Failed to Resend OTP'));
|
||||
}
|
||||
}
|
||||
|
||||
// OTP VEFICATION
|
||||
// OTP VERIFICATION
|
||||
Future<void> otpverification(String email, String otp) async {
|
||||
try {
|
||||
// dio.options.headers['Authorization'] = 'Bearer $token';
|
||||
await dio
|
||||
.post('$baseUrl/token/user/otp_verification?email=$email&otp=$otp');
|
||||
} catch (e) {
|
||||
throw Exception('Failed to Verify Otp: $e');
|
||||
throw Exception(_formatError(e, 'Failed to Verify OTP'));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> createuser(Map<String, dynamic> entity) async {
|
||||
try {
|
||||
print("in post api...$entity");
|
||||
await dio.post('$baseUrl/token/addOneAppUser', data: entity);
|
||||
print(entity);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to create User: $e');
|
||||
throw Exception(_formatError(e, 'Failed to Create User'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +130,7 @@ class SignUpApiService {
|
||||
await dio.put('$baseUrl/api/updateAppUserDto/$entityId', data: entity);
|
||||
print(entity);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to update Backend: $e');
|
||||
throw Exception(_formatError(e, 'Failed to update User'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +139,7 @@ class SignUpApiService {
|
||||
dio.options.headers['Authorization'] = 'Bearer $token';
|
||||
await dio.delete('$baseUrl/api/delete_usr/$entityId');
|
||||
} catch (e) {
|
||||
throw Exception('Failed to delete User: $e');
|
||||
throw Exception(_formatError(e, 'Failed to delete User'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user