mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 02:39:19 +08:00
Merge branch 'foss42:main' into har_importer
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import 'dart:io';
|
||||
import 'dart:collection';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/io_client.dart';
|
||||
@@ -15,7 +14,7 @@ class HttpClientManager {
|
||||
static final HttpClientManager _instance = HttpClientManager._internal();
|
||||
static const int _maxCancelledRequests = 100;
|
||||
final Map<String, http.Client> _clients = {};
|
||||
final Queue<String> _cancelledRequests = Queue();
|
||||
final Set<String> _cancelledRequests = {};
|
||||
|
||||
factory HttpClientManager() {
|
||||
return _instance;
|
||||
@@ -38,9 +37,9 @@ class HttpClientManager {
|
||||
_clients[requestId]?.close();
|
||||
_clients.remove(requestId);
|
||||
|
||||
_cancelledRequests.addLast(requestId);
|
||||
while (_cancelledRequests.length > _maxCancelledRequests) {
|
||||
_cancelledRequests.removeFirst();
|
||||
_cancelledRequests.add(requestId);
|
||||
if (_cancelledRequests.length > _maxCancelledRequests) {
|
||||
_cancelledRequests.remove(_cancelledRequests.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,6 +48,10 @@ class HttpClientManager {
|
||||
return _cancelledRequests.contains(requestId);
|
||||
}
|
||||
|
||||
void removeCancelledRequest(String requestId) {
|
||||
_cancelledRequests.remove(requestId);
|
||||
}
|
||||
|
||||
void closeClient(String requestId) {
|
||||
if (_clients.containsKey(requestId)) {
|
||||
_clients[requestId]?.close();
|
||||
|
||||
@@ -19,6 +19,9 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
|
||||
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
|
||||
bool noSSL = false,
|
||||
}) async {
|
||||
if (httpClientManager.wasRequestCancelled(requestId)) {
|
||||
httpClientManager.removeCancelledRequest(requestId);
|
||||
}
|
||||
final client = httpClientManager.createClient(requestId, noSSL: noSSL);
|
||||
|
||||
(Uri?, String?) uriRec = getValidRequestUri(
|
||||
@@ -71,37 +74,27 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
|
||||
}
|
||||
}
|
||||
http.StreamedResponse multiPartResponse =
|
||||
await multiPartRequest.send();
|
||||
await client.send(multiPartRequest);
|
||||
|
||||
stopwatch.stop();
|
||||
http.Response convertedMultiPartResponse =
|
||||
await convertStreamedResponse(multiPartResponse);
|
||||
return (convertedMultiPartResponse, stopwatch.elapsed, null);
|
||||
}
|
||||
}
|
||||
switch (requestModel.method) {
|
||||
case HTTPVerb.get:
|
||||
response = await client.get(requestUrl, headers: headers);
|
||||
break;
|
||||
case HTTPVerb.head:
|
||||
response = await client.head(requestUrl, headers: headers);
|
||||
break;
|
||||
case HTTPVerb.post:
|
||||
response =
|
||||
await client.post(requestUrl, headers: headers, body: body);
|
||||
break;
|
||||
case HTTPVerb.put:
|
||||
response =
|
||||
await client.put(requestUrl, headers: headers, body: body);
|
||||
break;
|
||||
case HTTPVerb.patch:
|
||||
response =
|
||||
await client.patch(requestUrl, headers: headers, body: body);
|
||||
break;
|
||||
case HTTPVerb.delete:
|
||||
response =
|
||||
await client.delete(requestUrl, headers: headers, body: body);
|
||||
break;
|
||||
}
|
||||
response = switch (requestModel.method) {
|
||||
HTTPVerb.get => await client.get(requestUrl, headers: headers),
|
||||
HTTPVerb.head => response =
|
||||
await client.head(requestUrl, headers: headers),
|
||||
HTTPVerb.post => response =
|
||||
await client.post(requestUrl, headers: headers, body: body),
|
||||
HTTPVerb.put => response =
|
||||
await client.put(requestUrl, headers: headers, body: body),
|
||||
HTTPVerb.patch => response =
|
||||
await client.patch(requestUrl, headers: headers, body: body),
|
||||
HTTPVerb.delete => response =
|
||||
await client.delete(requestUrl, headers: headers, body: body),
|
||||
};
|
||||
}
|
||||
if (apiType == APIType.graphql) {
|
||||
var requestBody = getGraphQLBody(requestModel);
|
||||
|
||||
Reference in New Issue
Block a user