mirror of
https://github.com/foss42/apidash.git
synced 2025-12-03 03:17:00 +08:00
94 lines
2.3 KiB
Dart
94 lines
2.3 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 _JsonAcceptClient extends http.BaseClient {
|
|
final http.Client _inner;
|
|
|
|
_JsonAcceptClient(this._inner);
|
|
|
|
@override
|
|
Future<http.StreamedResponse> send(http.BaseRequest request) {
|
|
request.headers['Accept'] = 'application/json';
|
|
return _inner.send(request);
|
|
}
|
|
|
|
@override
|
|
void close() {
|
|
_inner.close();
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
http.Client createClientWithJsonAccept(
|
|
String requestId, {
|
|
bool noSSL = false,
|
|
}) {
|
|
final baseClient = (noSSL && !kIsWeb)
|
|
? createHttpClientWithNoSSL()
|
|
: http.Client();
|
|
|
|
final client = _JsonAcceptClient(baseClient);
|
|
_clients[requestId] = client;
|
|
return client;
|
|
}
|
|
}
|