Advertisement
kwasinski

Untitled

Feb 5th, 2025
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.37 KB | None | 0 0
  1. import 'dart:convert';
  2. import 'package:http/http.dart' as http;
  3.  
  4. import '/App/Configs/Config.dart';
  5. import '/App/Services/Common/Interfaces/HttpClientInterface.dart';
  6.  
  7. import '/App/Support/Logs/Log.dart';
  8.  
  9. abstract class BaseHttpClient implements HttpClientInterface
  10. {
  11.     final String baseUrl = Config.BASE_API_URL;
  12.  
  13.     final defaultHeaders = <String, String> {
  14.         'Content-Type': 'application/json',
  15.     };
  16.  
  17.     late dynamic headers;
  18.  
  19.  
  20.     BaseHttpClient(
  21.         {
  22.             this.headers,
  23.         }
  24.     );
  25.  
  26.     @override
  27.     Future<http.Response> post(String endpoint, Map<String, dynamic> data) async
  28.     {
  29.         final url = Uri.parse('$baseUrl$endpoint');
  30.    
  31.         Log.debug('[BaseHttpClient] :: [post] :: [headers]: $headers');
  32.        
  33.         headers = {
  34.             ...defaultHeaders,
  35.         };
  36.        
  37.         Log.debug('[BaseHttpClient] :: [post] :: [headers]: $headers');
  38.  
  39.         return await http.post(
  40.             url,
  41.             headers: headers,
  42.             body: jsonEncode(data),
  43.         );
  44.     }
  45.  
  46.     @override
  47.     Future<http.Response> get(String endpoint) async
  48.     {
  49.         final url = Uri.parse('$baseUrl$endpoint');
  50.  
  51.         headers = {
  52.             ...defaultHeaders,
  53.             ...headers,
  54.         };
  55.  
  56.         return await http.get(
  57.             url,
  58.             headers: headers,
  59.  
  60.         );
  61.     }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement