// ignore_for_file: file_names import 'dart:convert'; import 'package:daily_ledger/config/Global.dart'; import 'package:http/http.dart' as http; class ApiService { late final String token; ApiService(this.token); String getApi(String apiName) { return Constants.baseUrl + Constants.apiEndpoints[apiName]!; } Future post(String api, Map payload) async { final uri = Uri.parse(getApi(api)); try { final response = await http.post( uri, headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, body: jsonEncode(payload), ); return response; } catch (err) { rethrow; } } Future put( String api, String id, Map payload) async { final uri = Uri.parse('${getApi(api)}$id/'); try { final response = await http.put( uri, headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, body: jsonEncode(payload), ); return response; } catch (err) { rethrow; } } Future patch( String api, String id, Map payload) async { final uri = Uri.parse('${getApi(api)}$id/'); try { final response = await http.patch( uri, headers: { 'Authorization': 'Bearer $token', 'Content-Type': 'application/json', }, body: jsonEncode(payload), ); return response; } catch (err) { rethrow; } } Future get(String api) async { final uri = Uri.parse(getApi(api)); try { final response = await http.get( uri, headers: { 'Authorization': 'Bearer $token', }, ); return jsonDecode(response.body); } catch (err) { rethrow; } } Future delete(String api, String id) async { final uri = Uri.parse('${getApi(api)}$id/'); try { final response = await http.delete( uri, headers: { 'Authorization': 'Bearer $token', }, ); return response; } catch (err) { rethrow; } } }