Update insomnia_io.dart

This commit is contained in:
Ashita Prasad
2025-02-24 11:39:50 +05:30
parent c40704b0b1
commit 18c32dc4c7

View File

@ -1,42 +1,26 @@
import 'package:insomnia_collection/models/insomnia_environment.dart'; import 'package:insomnia_collection/insomnia_collection.dart';
import 'package:seed/seed.dart'; import 'package:seed/seed.dart';
import '../consts.dart'; import '../consts.dart';
import '../models/models.dart'; import '../models/models.dart';
import '../utils/utils.dart'; import '../utils/utils.dart';
import 'package:insomnia_collection/insomnia_collection.dart' as ins;
class InsomniaIO { class InsomniaIO {
List<(String?, HttpRequestModel)>? getHttpRequestModelList(String content) { List<(String?, HttpRequestModel)>? getHttpRequestModelList(String content) {
content = content.trim(); content = content.trim();
try { try {
final ic = ins.insomniaCollectionFromJsonStr(content); final ic = insomniaCollectionFromJsonStr(content);
final requests = ins.getRequestsFromInsomniaCollection(ic); final requests = getRequestsFromInsomniaCollection(ic);
/// TODO; Get env from the insomnia collection
// final environmentVariables = ins.getEnvironmentVariablesFromInsomniaEnvironment(env);
return requests return requests
.map((req) => (req.$1, insomniaRequestToHttpRequestModel(req.$2))) .map((req) => (req.$1, insomniaResourceToHttpRequestModel(req.$2)))
.toList(); .toList();
} catch (e) { } catch (e) {
return null; return null;
} }
} }
InsomniaEnvironment? getInsomiaEnvironment(String content) { HttpRequestModel insomniaResourceToHttpRequestModel(Resource request) {
content = content.trim();
try {
final env = ins.insomniaEnvironmentFromJsonStr(content);
return env;
} catch (e) {
return null;
}
}
HttpRequestModel insomniaRequestToHttpRequestModel(ins.Resource request) {
HTTPVerb method; HTTPVerb method;
try { try {
method = HTTPVerb.values.byName((request.method ?? "").toLowerCase()); method = HTTPVerb.values.byName((request.method ?? "").toLowerCase());
} catch (e) { } catch (e) {
@ -49,7 +33,7 @@ class InsomniaIO {
List<NameValueModel> params = []; List<NameValueModel> params = [];
List<bool> isParamEnabledList = []; List<bool> isParamEnabledList = [];
for (var header in request.headers ?? <ins.Header>[]) { for (var header in request.headers ?? <Header>[]) {
var name = header.name ?? ""; var name = header.name ?? "";
var value = header.value ?? ""; var value = header.value ?? "";
var activeHeader = header.disabled ?? false; var activeHeader = header.disabled ?? false;
@ -57,7 +41,7 @@ class InsomniaIO {
isHeaderEnabledList.add(!activeHeader); isHeaderEnabledList.add(!activeHeader);
} }
for (var query in request.parameters ?? <ins.Parameter>[]) { for (var query in request.parameters ?? <Parameter>[]) {
var name = query.name ?? ""; var name = query.name ?? "";
var value = query.value; var value = query.value;
var activeQuery = query.disabled ?? false; var activeQuery = query.disabled ?? false;
@ -65,44 +49,34 @@ class InsomniaIO {
isParamEnabledList.add(!activeQuery); isParamEnabledList.add(!activeQuery);
} }
ContentType bodyContentType = kDefaultContentType; ContentType bodyContentType =
getContentTypeFromContentTypeStr(request.body?.mimeType) ??
kDefaultContentType;
String? body; String? body;
List<FormDataModel>? formData; List<FormDataModel>? formData;
if (request.body != null) { if (request.body != null && request.body?.mimeType != null) {
if (request.body?.mimeType != null) { if (bodyContentType == ContentType.formdata) {
try { formData = [];
if (request.body?.mimeType == 'text/plain') { for (var fd in request.body?.params ?? <Formdatum>[]) {
bodyContentType = ContentType.text; var name = fd.name ?? "";
} else if (request.body?.mimeType == 'application/json') { FormDataType formDataType;
bodyContentType = ContentType.json; try {
} else if (request.body?.mimeType == 'multipart/form-data') { formDataType = FormDataType.values.byName(fd.type ?? "");
bodyContentType = ContentType.formdata; } catch (e) {
formData = []; formDataType = FormDataType.text;
for (var fd in request.body?.params ?? <ins.Formdatum>[]) {
var name = fd.name ?? "";
FormDataType formDataType;
try {
formDataType = FormDataType.values.byName(fd.type ?? "");
} catch (e) {
formDataType = FormDataType.text;
}
var value = switch (formDataType) {
FormDataType.text => fd.value ?? "",
FormDataType.file => fd.src ?? ""
};
formData.add(FormDataModel(
name: name,
value: value,
type: formDataType,
));
}
} else {
bodyContentType =
ContentType.values.byName(request.body?.mimeType ?? "");
} }
} catch (e) { var value = switch (formDataType) {
bodyContentType = kDefaultContentType; FormDataType.text => fd.value ?? "",
FormDataType.file => fd.src ?? ""
};
formData.add(FormDataModel(
name: name,
value: value,
type: formDataType,
));
} }
} else {
body = request.body?.text; body = request.body?.text;
} }
} }