172 lines
4.5 KiB
Dart
172 lines
4.5 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:yimaru_app/models/user_model.dart';
|
|
import 'package:yimaru_app/services/dio_service.dart';
|
|
import 'package:yimaru_app/ui/common/app_constants.dart';
|
|
|
|
import '../app/app.locator.dart';
|
|
import '../ui/common/enmus.dart';
|
|
|
|
class ApiService {
|
|
final _service = locator<DioService>();
|
|
|
|
// Http headers
|
|
Map<String, dynamic> _getHeaders({String? token}) => {
|
|
|
|
// if (token != null) 'Authorization': 'Bearer $token',
|
|
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
if (token != null) 'Authorization': 'Bearer $token'
|
|
};
|
|
|
|
// Dio options
|
|
Options? _getOptions({String? token}) {
|
|
return Options(
|
|
// followRedirects: false,
|
|
// validateStatus: (status) => true,
|
|
headers: _getHeaders(token: token),
|
|
);
|
|
}
|
|
|
|
// Register
|
|
Future<Map<String, dynamic>> register(Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$baseUrl/$userUrl/$kRegisterUrl',
|
|
data: data,
|
|
options: _getOptions(),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Otp sent successfully'
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': 'Unknown Error Occurred'
|
|
};
|
|
}
|
|
} catch (e) {
|
|
return {
|
|
'message': e.toString(),
|
|
'status': ResponseStatus.failure,
|
|
};
|
|
}
|
|
}
|
|
|
|
// Login
|
|
Future<Map<String, dynamic>> login(Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$baseUrl/$kLoginUrl',
|
|
data: data,
|
|
options: _getOptions(),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Logged in successfully',
|
|
'data': UserModel.fromJson(response.data['data']),
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': '${response.data['message']}, ${response.data['error']}'
|
|
};
|
|
}
|
|
} catch (e) {
|
|
return {
|
|
'message': e.toString(),
|
|
'status': ResponseStatus.failure,
|
|
};
|
|
}
|
|
}
|
|
|
|
// Verify otp
|
|
Future<Map<String, dynamic>> verifyOtp(Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$baseUrl/$userUrl/$kVerifyOtpUrl',
|
|
data: data,
|
|
options: _getOptions(),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Otp verified successfully',
|
|
//'data': UserModel.fromJson(response.data['data']),
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': '${response.data['message']}, ${response.data['error']}'
|
|
};
|
|
}
|
|
} catch (e) {
|
|
return {
|
|
'message': e.toString(),
|
|
'status': ResponseStatus.failure,
|
|
};
|
|
}
|
|
}
|
|
|
|
// Resend otp
|
|
Future<Map<String, dynamic>> resendOtp(Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$baseUrl/$userUrl/$kResendOtpUrl',
|
|
data: data,
|
|
options: _getOptions(),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Otp resend successfully'
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': 'Unknown Error Occurred'
|
|
};
|
|
}
|
|
} catch (e) {
|
|
return {
|
|
'message': e.toString(),
|
|
'status': ResponseStatus.failure,
|
|
};
|
|
}
|
|
}
|
|
|
|
// Profile completed
|
|
Future<Map<String, dynamic>> getProfileStatus(UserModel? user) async {
|
|
try {
|
|
Response response = await _service.dio.get(
|
|
'$baseUrl/$userUrl/${user?.userId}/$kProfileStatusUrl',
|
|
options: _getOptions(token: user?.accessToken),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Profile completion status fetched successfully',
|
|
'data': response.data['data']['is_profile_completed'] as bool,
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': '${response.data['message']}, ${response.data['error']}'
|
|
};
|
|
}
|
|
} catch (e) {
|
|
return {
|
|
'message': e.toString(),
|
|
'status': ResponseStatus.failure,
|
|
};
|
|
}
|
|
}
|
|
}
|