1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import 'dart:convert';
- import 'session_storage.dart';
- import 'package:http/http.dart' as http;
- class BaseService {
- int currentPage = 0;
- int limit = 20;
- int total = 0;
- //String baseUrl = 'hermogas.est.api.rdsistemas.app';
- //produccion: joshipapas.api.edesarrollos.info
- //prueba: pos.test.turquessacoffee.com
- String base_url = 'https://pos.api.turquessacoffee.com/';
- String baseUrl = 'pos.api.turquessacoffee.com';
- Future<Map<String, String>> getDefaultHeaders({withAuth = true}) async {
- Map<String, String> defaultHeaders = {'Content-Type': 'application/json'};
- if (withAuth) {
- String? token = await SessionStorage().getToken();
- defaultHeaders['Authorization'] = 'Bearer ${token ?? ''}';
- }
- return defaultHeaders;
- }
- Future<http.Response> get(String endpoint,
- {bool withAuth = true,
- Map<String, String>? queryParameters,
- Map<String, String>? headers}) async {
- final uri = Uri.https(baseUrl, endpoint, queryParameters);
- var defaultHeaders = await getDefaultHeaders(withAuth: withAuth);
- var head = {...?headers, ...defaultHeaders};
- return await http.get(uri, headers: head);
- }
- Future<http.Response> post(String endpoint,
- {bool withAuth = true,
- Map<String, String>? queryParameters,
- Map<String, dynamic>? body,
- Map<String, String>? headers}) async {
- final uri = Uri.https(baseUrl, endpoint, queryParameters);
- var defaultHeaders = await getDefaultHeaders(withAuth: withAuth);
- var head = {...?headers, ...defaultHeaders};
- return await http.post(uri, body: json.encode(body), headers: head);
- }
- Future<http.Response> delete(String endpoint,
- {bool withAuth = true,
- Map<String, String>? queryParameters,
- Map<String, dynamic>? body,
- Map<String, String>? headers}) async {
- final uri = Uri.https(baseUrl, endpoint, queryParameters);
- var defaultHeaders = await getDefaultHeaders(withAuth: withAuth);
- var head = {...?headers, ...defaultHeaders};
- return await http.delete(uri, body: json.encode(body), headers: head);
- }
- String prefijoVersion() {
- if (base_url.contains('api')) {
- return 'vP';
- } else if (base_url.contains('test')) {
- return 'vT';
- }
- return 'v'; // Versión genérica por si no coincide con api ni test
- }
- }
|