Skip to content

Fix issue #630: Unable to override Content-Type header charset (Using Dio) #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/providers/collection_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,20 +267,16 @@ class CollectionStateNotifier
ref.read(codePaneVisibleStateProvider.notifier).state = false;
final defaultUriScheme = ref.read(settingsProvider).defaultUriScheme;

if (requestId == null || state == null) {
return;
}
RequestModel? requestModel = state![requestId];
if (requestId == null || state == null) return;

if (requestModel?.httpRequestModel == null) {
return;
}
RequestModel? requestModel = state![requestId];
if (requestModel?.httpRequestModel == null) return;

APIType apiType = requestModel!.apiType;
HttpRequestModel substitutedHttpRequestModel =
getSubstitutedHttpRequestModel(requestModel.httpRequestModel!);

// set current model's isWorking to true and update state
// Set current model's isWorking to true and update state
var map = {...state!};
map[requestId] = requestModel.copyWith(
isWorking: true,
Expand All @@ -298,24 +294,28 @@ class CollectionStateNotifier
);

late final RequestModel newRequestModel;

if (responseRec.$1 == null) {
newRequestModel = requestModel.copyWith(
responseStatus: -1,
message: responseRec.$3,
isWorking: false,
);
} else {
final httpResponseModel = baseHttpResponseModel.fromResponse(
final httpResponseModel = baseHttpResponseModel.fromDioResponse(
response: responseRec.$1!,
time: responseRec.$2!,
);
int statusCode = responseRec.$1!.statusCode;

final int statusCode = responseRec.$1?.statusCode ?? -1;

newRequestModel = requestModel.copyWith(
responseStatus: statusCode,
message: kResponseCodeReasons[statusCode],
httpResponseModel: httpResponseModel,
isWorking: false,
);

String newHistoryId = getNewUuid();
HistoryRequestModel model = HistoryRequestModel(
historyId: newHistoryId,
Expand All @@ -335,7 +335,7 @@ class CollectionStateNotifier
ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(model);
}

// update state with response data
// Update state with response data
map = {...state!};
map[requestId] = newRequestModel;
state = map;
Expand Down
28 changes: 28 additions & 0 deletions packages/apidash_core/lib/models/http_response_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:collection/collection.dart' show mergeMaps;
import 'package:http/http.dart';
import 'package:http_parser/http_parser.dart';
import '../extensions/extensions.dart';
import 'package:dio/dio.dart' as dio;
import '../utils/utils.dart';
import '../consts.dart';

Expand Down Expand Up @@ -85,4 +86,31 @@ class HttpResponseModel with _$HttpResponseModel {
time: time,
);
}

HttpResponseModel fromDioResponse({
required dio.Response response,
Duration? time,
}) {
final headers = <String, String>{};
response.headers.forEach((k, v) {
headers[k] = v.join(',');
});

MediaType? mediaType = getMediaTypeFromHeaders(headers);
final body = (mediaType?.subtype == kSubTypeJson)
? jsonEncode(response.data)
: response.data.toString();

return HttpResponseModel(
statusCode: response.statusCode,
headers: headers,
requestHeaders: response.requestOptions.headers.map((k, v) => MapEntry(k, v.toString())),
body: body,
formattedBody: formatBody(body, mediaType),
bodyBytes: response.data is List<int>
? Uint8List.fromList(response.data)
: utf8.encode(body),
time: time,
);
}
}
68 changes: 34 additions & 34 deletions packages/apidash_core/lib/services/http_client_manager.dart
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
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 Map<String, Dio> _clients = {};
final Map<String, CancelToken> _cancelTokens = {};
final Set<String> _cancelledRequests = {};

factory HttpClientManager() {
return _instance;
}
factory HttpClientManager() => _instance;

HttpClientManager._internal();

http.Client createClient(
String requestId, {
bool noSSL = false,
}) {
final client =
(noSSL && !kIsWeb) ? createHttpClientWithNoSSL() : http.Client();
_clients[requestId] = client;
return client;
Dio createClient(String requestId, {bool noSSL = false}) {
final dio = Dio();

if (noSSL && !kIsWeb) {
(dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
final client = HttpClient();
client.badCertificateCallback = (cert, host, port) => true;
return client;
};
}

final cancelToken = CancelToken();
_clients[requestId] = dio;
_cancelTokens[requestId] = cancelToken;

return dio;
}

CancelToken? getCancelToken(String requestId) => _cancelTokens[requestId];

void cancelRequest(String? requestId) {
if (requestId != null && _clients.containsKey(requestId)) {
_clients[requestId]?.close();
if (requestId != null) {
_cancelTokens[requestId]?.cancel("Request cancelled");
_clients.remove(requestId);

_cancelTokens.remove(requestId);
_cancelledRequests.add(requestId);

if (_cancelledRequests.length > _maxCancelledRequests) {
_cancelledRequests.remove(_cancelledRequests.first);
}
}
}

bool wasRequestCancelled(String requestId) {
return _cancelledRequests.contains(requestId);
}
bool wasRequestCancelled(String requestId) =>
_cancelledRequests.contains(requestId);

void removeCancelledRequest(String requestId) {
_cancelledRequests.remove(requestId);
}

void closeClient(String requestId) {
if (_clients.containsKey(requestId)) {
_clients[requestId]?.close();
_clients.remove(requestId);
}
_clients.remove(requestId);
_cancelTokens.remove(requestId);
}

bool hasActiveClient(String requestId) {
return _clients.containsKey(requestId);
}
bool hasActiveClient(String requestId) =>
_clients.containsKey(requestId);
}
Loading