Files
recipe-app/flutter/lib/core/api/api_client.dart
T

38 lines
1.4 KiB
Dart

import 'dart:io';
import 'package:http/http.dart' as http;
/// Platform-neutral HTTP client wrapping the internal API base URL.
/// Base URL is read from the FLUTTER_API_URL_INTERNAL environment variable
/// (set by Docker) or falls back to localhost for local development.
class ApiClient {
final String baseUrl;
final http.Client _client;
ApiClient({http.Client? client})
: baseUrl = const String.fromEnvironment(
'API_BASE_URL',
defaultValue: 'http://localhost:8080',
),
_client = client ?? http.Client();
Map<String, String> _headers({String? token}) => {
'Content-Type': 'application/json',
if (token != null) 'Authorization': 'Bearer $token',
};
Future<http.Response> get(String path, {String? token}) =>
_client.get(Uri.parse('$baseUrl$path'), headers: _headers(token: token));
Future<http.Response> post(String path, String body, {String? token}) =>
_client.post(Uri.parse('$baseUrl$path'),
headers: _headers(token: token), body: body);
Future<http.Response> put(String path, String body, {String? token}) =>
_client.put(Uri.parse('$baseUrl$path'),
headers: _headers(token: token), body: body);
Future<http.Response> delete(String path, {String? token}) =>
_client.delete(Uri.parse('$baseUrl$path'),
headers: _headers(token: token));
}