70 lines
1.9 KiB
Dart
70 lines
1.9 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
class ApiService {
|
|
static const _baseUrl = 'https://freaccess.com.br/freaccess';
|
|
|
|
static final Dio _dio = Dio(BaseOptions(
|
|
baseUrl: _baseUrl,
|
|
// headers: {'Authorization': 'Bearer $_apiKey'},
|
|
));
|
|
|
|
static Future<Response> makeApiCall({
|
|
required String endpoint,
|
|
required Map<String, dynamic> body,
|
|
bool isMultipart = false,
|
|
Uint8List? file,
|
|
Uint8List? blob,
|
|
}) async {
|
|
try {
|
|
Response response;
|
|
if (isMultipart && file != null) {
|
|
FormData formData = FormData.fromMap(body);
|
|
formData.files.add(MapEntry('file', MultipartFile.fromBytes(file)));
|
|
response = await _dio.post(endpoint, data: formData);
|
|
} else if (blob != null) {
|
|
// Convert the blob to base64 string
|
|
String base64Image = base64Encode(blob);
|
|
|
|
// Set the request headers
|
|
Map<String, dynamic> headers = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
// Create the request body as JSON
|
|
Map<String, dynamic> requestBody = {
|
|
'image': base64Image,
|
|
};
|
|
|
|
response = await _dio.post(
|
|
endpoint,
|
|
data: jsonEncode(requestBody),
|
|
options: Options(headers: headers),
|
|
);
|
|
} else {
|
|
// Set the request headers
|
|
Map<String, dynamic> headers = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
response = await _dio.post(
|
|
endpoint,
|
|
data: jsonEncode(body),
|
|
options: Options(headers: headers),
|
|
);
|
|
}
|
|
debugPrint('API call to $endpoint successful');
|
|
debugPrint('Api call body: $body');
|
|
debugPrint('Response: ${response.data}');
|
|
debugPrint('Response headers: ${response.headers}');
|
|
debugPrint('Response status: ${response.statusCode}');
|
|
return response;
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
}
|