mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 10:49:49 +08:00
64 lines
1.7 KiB
Dart
64 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);
|
|
}
|
|
}
|