seperated the api service
This commit is contained in:
parent
afc9990180
commit
1ea8447d6c
14
lib/helpers/services/api_endpoints.dart
Normal file
14
lib/helpers/services/api_endpoints.dart
Normal file
@ -0,0 +1,14 @@
|
||||
class ApiEndpoints {
|
||||
static const String baseUrl = "https://stageapi.marcoaiot.com/api";
|
||||
|
||||
// Attendance Screen API Endpoints
|
||||
static const String getProjects = "/project/list";
|
||||
static const String getEmployeesByProject = "/attendance/project/team";
|
||||
static const String getAttendanceLogs = "/attendance/project/log";
|
||||
static const String getAttendanceLogView = "/attendance/log/attendance";
|
||||
static const String getRegularizationLogs = "/attendance/regularize";
|
||||
static const String uploadAttendanceImage = "/attendance/record-image";
|
||||
|
||||
// Employee Screen API Endpoints
|
||||
|
||||
}
|
@ -2,14 +2,14 @@ import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:marco/helpers/services/storage/local_storage.dart';
|
||||
import 'package:marco/helpers/services/auth_service.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:marco/helpers/services/api_endpoints.dart';
|
||||
|
||||
final Logger logger = Logger();
|
||||
|
||||
class ApiService {
|
||||
static const String baseUrl = "https://stageapi.marcoaiot.com/api";
|
||||
static const Duration timeout = Duration(seconds: 10);
|
||||
static const bool enableLogs = true;
|
||||
|
||||
@ -18,7 +18,7 @@ class ApiService {
|
||||
static Future<String?> _getToken() async {
|
||||
final token = await LocalStorage.getJwtToken();
|
||||
if (token == null && enableLogs) {
|
||||
logger.w("No JWT token found. Please log in."); // <-- Use logger
|
||||
logger.w("No JWT token found. Please log in.");
|
||||
}
|
||||
return token;
|
||||
}
|
||||
@ -29,7 +29,7 @@ class ApiService {
|
||||
};
|
||||
|
||||
static void _log(String message) {
|
||||
if (enableLogs) logger.i(message); // <-- Use logger
|
||||
if (enableLogs) logger.i(message);
|
||||
}
|
||||
|
||||
static dynamic _parseResponse(http.Response response, {String label = ''}) {
|
||||
@ -51,18 +51,22 @@ class ApiService {
|
||||
String? token = await _getToken();
|
||||
if (token == null) return null;
|
||||
|
||||
Uri uri = Uri.parse("$baseUrl$endpoint").replace(queryParameters: queryParams);
|
||||
Uri uri = Uri.parse("${ApiEndpoints.baseUrl}$endpoint")
|
||||
.replace(queryParameters: queryParams);
|
||||
_log('GET request: $uri');
|
||||
|
||||
try {
|
||||
http.Response response = await http.get(uri, headers: _headers(token)).timeout(timeout);
|
||||
http.Response response =
|
||||
await http.get(uri, headers: _headers(token)).timeout(timeout);
|
||||
|
||||
if (response.statusCode == 401 && !hasRetried) {
|
||||
_log("Unauthorized. Attempting token refresh...");
|
||||
bool refreshed = await AuthService.refreshToken();
|
||||
if (refreshed) {
|
||||
token = await _getToken();
|
||||
if (token != null) {
|
||||
return await _getRequest(endpoint, queryParams: queryParams, hasRetried: true);
|
||||
return await _getRequest(endpoint,
|
||||
queryParams: queryParams, hasRetried: true);
|
||||
}
|
||||
}
|
||||
_log("Refresh failed.");
|
||||
@ -78,7 +82,7 @@ class ApiService {
|
||||
String? token = await _getToken();
|
||||
if (token == null) return null;
|
||||
|
||||
final uri = Uri.parse("$baseUrl$endpoint");
|
||||
final uri = Uri.parse("${ApiEndpoints.baseUrl}$endpoint");
|
||||
_log("POST request to $uri with body: $body");
|
||||
|
||||
try {
|
||||
@ -95,12 +99,12 @@ class ApiService {
|
||||
// ===== API Calls =====
|
||||
|
||||
static Future<List<dynamic>?> getProjects() async {
|
||||
final response = await _getRequest("/project/list");
|
||||
final response = await _getRequest(ApiEndpoints.getProjects);
|
||||
return response != null ? _parseResponse(response, label: 'Projects') : null;
|
||||
}
|
||||
|
||||
static Future<List<dynamic>?> getEmployeesByProject(String projectId) async {
|
||||
final response = await _getRequest("/attendance/project/team",
|
||||
final response = await _getRequest(ApiEndpoints.getEmployeesByProject,
|
||||
queryParams: {"projectId": projectId});
|
||||
return response != null ? _parseResponse(response, label: 'Employees') : null;
|
||||
}
|
||||
@ -109,21 +113,24 @@ class ApiService {
|
||||
{DateTime? dateFrom, DateTime? dateTo}) async {
|
||||
final query = {
|
||||
"projectId": projectId,
|
||||
if (dateFrom != null) "dateFrom": DateFormat('yyyy-MM-dd').format(dateFrom),
|
||||
if (dateTo != null) "dateTo": DateFormat('yyyy-MM-dd').format(dateTo),
|
||||
if (dateFrom != null)
|
||||
"dateFrom": DateFormat('yyyy-MM-dd').format(dateFrom),
|
||||
if (dateTo != null)
|
||||
"dateTo": DateFormat('yyyy-MM-dd').format(dateTo),
|
||||
};
|
||||
|
||||
final response = await _getRequest("/attendance/project/log", queryParams: query);
|
||||
final response =
|
||||
await _getRequest(ApiEndpoints.getAttendanceLogs, queryParams: query);
|
||||
return response != null ? _parseResponse(response, label: 'Attendance Logs') : null;
|
||||
}
|
||||
|
||||
static Future<List<dynamic>?> getAttendanceLogView(String id) async {
|
||||
final response = await _getRequest("/attendance/log/attendance/$id");
|
||||
final response = await _getRequest("${ApiEndpoints.getAttendanceLogView}/$id");
|
||||
return response != null ? _parseResponse(response, label: 'Log Details') : null;
|
||||
}
|
||||
|
||||
static Future<List<dynamic>?> getRegularizationLogs(String projectId) async {
|
||||
final response = await _getRequest("/attendance/regularize",
|
||||
final response = await _getRequest(ApiEndpoints.getRegularizationLogs,
|
||||
queryParams: {"projectId": projectId});
|
||||
return response != null ? _parseResponse(response, label: 'Regularization Logs') : null;
|
||||
}
|
||||
@ -175,7 +182,8 @@ class ApiService {
|
||||
}
|
||||
}
|
||||
|
||||
final response = await _postRequest("/attendance/record-image", body);
|
||||
final response =
|
||||
await _postRequest(ApiEndpoints.uploadAttendanceImage, body);
|
||||
if (response == null) return false;
|
||||
|
||||
final json = jsonDecode(response.body);
|
||||
|
Loading…
x
Reference in New Issue
Block a user