mirror of
https://github.com/foss42/apidash.git
synced 2025-12-07 21:50:34 +08:00
map insomnia resources to HttpRequestModel
This commit is contained in:
81
packages/apidash_core/lib/import_export/insomia_io.dart
Normal file
81
packages/apidash_core/lib/import_export/insomia_io.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
import 'package:insomnia/insomnia.dart' as ins;
|
||||
import 'package:seed/seed.dart';
|
||||
import '../consts.dart';
|
||||
import '../models/models.dart';
|
||||
import '../utils/utils.dart';
|
||||
|
||||
class InsomiaIO {
|
||||
List<(String?, HttpRequestModel)>? getHttpRequestModelList(String content) {
|
||||
content = content.trim();
|
||||
try {
|
||||
final ic = ins.insomniaCollectionFromJsonStr(content);
|
||||
final requests = ins.getRequestsFromInsomniaCollection(ic);
|
||||
return requests
|
||||
.map((req) => (req.$1, insomniaRequestToHttpRequestModel(req.$2)))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
HttpRequestModel insomniaRequestToHttpRequestModel(ins.Resource request) {
|
||||
HTTPVerb method;
|
||||
|
||||
try {
|
||||
method = HTTPVerb.values.byName((request.method ?? "").toLowerCase());
|
||||
} catch (e) {
|
||||
method = kDefaultHttpMethod;
|
||||
}
|
||||
String url = stripUrlParams(request.url ?? "");
|
||||
List<NameValueModel> headers = [];
|
||||
List<bool> isHeaderEnabledList = [];
|
||||
|
||||
List<NameValueModel> params = [];
|
||||
List<bool> isParamEnabledList = [];
|
||||
|
||||
for (var header in request.headers ?? <ins.Header>[]) {
|
||||
var name = header.name ?? "";
|
||||
var value = header.value;
|
||||
var activeHeader = header.name?.isNotEmpty ?? false;
|
||||
headers.add(NameValueModel(name: name, value: value));
|
||||
isHeaderEnabledList.add(!activeHeader);
|
||||
}
|
||||
|
||||
for (var query in request.parameters ?? <ins.Parameter>[]) {
|
||||
var name = query.name ?? "";
|
||||
var value = query.value;
|
||||
var activeQuery = query.name?.isNotEmpty ?? false;
|
||||
params.add(NameValueModel(name: name, value: value));
|
||||
isParamEnabledList.add(!activeQuery);
|
||||
}
|
||||
|
||||
ContentType bodyContentType = kDefaultContentType;
|
||||
String? body;
|
||||
List<FormDataModel>? formData;
|
||||
if (request.body != null) {
|
||||
if (request.body?.mimeType != null) {
|
||||
try {
|
||||
bodyContentType = ContentType.values
|
||||
.byName(request.body?.mimeType ?? "");
|
||||
} catch (e) {
|
||||
bodyContentType = kDefaultContentType;
|
||||
}
|
||||
body = request.body?.text;
|
||||
}
|
||||
/// TODO: Handle formdata and text
|
||||
}
|
||||
|
||||
return HttpRequestModel(
|
||||
method: method,
|
||||
url: url,
|
||||
headers: headers,
|
||||
params: params,
|
||||
isHeaderEnabledList: isHeaderEnabledList,
|
||||
isParamEnabledList: isParamEnabledList,
|
||||
body: body,
|
||||
bodyContentType: bodyContentType,
|
||||
formData: formData,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user