1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import 'dart:async';
- import 'dart:convert';
- import '../data/session/session_storage.dart';
- import 'package:http/http.dart' as http;
- class BaseService {
- int currentPage = 0;
- int limit = 20;
- int total = 0;
- String base_url = 'https://dev.computosonora.com';
- String baseUrl = 'dev.computosonora.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);
- }
- Map<String, String> getHeader(String token) => {
- 'Authorization': 'Bearer ' + token,
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- };
- Future<http.Response?> getHttp(String url, String token) async {
- try {
- return await http
- .get(Uri.parse(base_url + url), headers: getHeader(token))
- .timeout(Duration(seconds: 120));
- } on TimeoutException catch (e) {
- print(e);
- return null;
- }
- }
- 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);
- }
- }
|