apidash_core: contents moved into better_networking package

This commit is contained in:
Manas Hejmadi
2025-06-17 23:55:16 +05:30
parent 330e1b82e6
commit 6558a4028e
32 changed files with 2347 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart';
http.Client createHttpClientWithNoSSL() {
var ioClient = HttpClient()
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return IOClient(ioClient);
}
class HttpClientManager {
static final HttpClientManager _instance = HttpClientManager._internal();
static const int _maxCancelledRequests = 100;
final Map<String, http.Client> _clients = {};
final Set<String> _cancelledRequests = {};
factory HttpClientManager() {
return _instance;
}
HttpClientManager._internal();
http.Client createClient(
String requestId, {
bool noSSL = false,
}) {
final client =
(noSSL && !kIsWeb) ? createHttpClientWithNoSSL() : http.Client();
_clients[requestId] = client;
return client;
}
void cancelRequest(String? requestId) {
if (requestId != null && _clients.containsKey(requestId)) {
_clients[requestId]?.close();
_clients.remove(requestId);
_cancelledRequests.add(requestId);
if (_cancelledRequests.length > _maxCancelledRequests) {
_cancelledRequests.remove(_cancelledRequests.first);
}
}
}
bool wasRequestCancelled(String requestId) {
return _cancelledRequests.contains(requestId);
}
void removeCancelledRequest(String requestId) {
_cancelledRequests.remove(requestId);
}
void closeClient(String requestId) {
if (_clients.containsKey(requestId)) {
_clients[requestId]?.close();
_clients.remove(requestId);
}
}
bool hasActiveClient(String requestId) {
return _clients.containsKey(requestId);
}
}