mirror of
https://github.com/foss42/apidash.git
synced 2025-12-05 04:18:56 +08:00
66 lines
1.7 KiB
Dart
66 lines
1.7 KiB
Dart
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);
|
|
}
|
|
}
|