Skip to content

Commit 40322ad

Browse files
committed
complete changes for har importer #122
1 parent 2ccad02 commit 40322ad

File tree

17 files changed

+1955
-593
lines changed

17 files changed

+1955
-593
lines changed

lib/consts.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ enum CodegenLanguage {
140140
enum ImportFormat {
141141
curl("cURL"),
142142
postman("Postman Collection v2.1"),
143-
insomnia("Insomnia v4");
143+
insomnia("Insomnia v4"),
144+
har("Har v1.2");
144145

145146
const ImportFormat(this.label);
146147
final String label;

lib/importer/importer.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Importer {
1313
.toList(),
1414
ImportFormat.postman => PostmanIO().getHttpRequestModelList(content),
1515
ImportFormat.insomnia => InsomniaIO().getHttpRequestModelList(content),
16+
ImportFormat.har => HarParserIO().getHttpRequestModelList(content),
1617
};
1718
}
1819
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import 'package:har_parser/har_parser.dart' as hp;
2+
import 'package:seed/seed.dart';
3+
import '../consts.dart';
4+
import '../models/models.dart';
5+
import '../utils/utils.dart';
6+
7+
class HarParserIO {
8+
List<(String?, HttpRequestModel)>? getHttpRequestModelList(String content) {
9+
content = content.trim();
10+
try {
11+
final hl = hp.harLogFromJsonStr(content);
12+
final requests = hp.getRequestsFromHarLog(hl);
13+
return requests
14+
.map((req) => (req.$2.url, harRequestToHttpRequestModel(req.$2)))
15+
.toList();
16+
} catch (e) {
17+
return null;
18+
}
19+
}
20+
21+
HttpRequestModel harRequestToHttpRequestModel(hp.Request request) {
22+
HTTPVerb method;
23+
24+
try {
25+
method = HTTPVerb.values.byName((request.method ?? "").toLowerCase());
26+
} catch (e) {
27+
method = kDefaultHttpMethod;
28+
}
29+
String url = stripUrlParams(request.url ?? "");
30+
List<NameValueModel> headers = [];
31+
List<bool> isHeaderEnabledList = [];
32+
33+
List<NameValueModel> params = [];
34+
List<bool> isParamEnabledList = [];
35+
36+
for (var header in request.headers ?? <hp.Header>[]) {
37+
var name = header.name ?? "";
38+
var value = header.value;
39+
var activeHeader = header.disabled ?? false;
40+
headers.add(NameValueModel(name: name, value: value));
41+
isHeaderEnabledList.add(!activeHeader);
42+
}
43+
44+
for (var query in request.queryString ?? <hp.Query>[]) {
45+
var name = query.name ?? "";
46+
var value = query.value;
47+
var activeQuery = query.disabled ?? false;
48+
params.add(NameValueModel(name: name, value: value));
49+
isParamEnabledList.add(!activeQuery);
50+
}
51+
52+
ContentType bodyContentType = kDefaultContentType;
53+
String? body;
54+
List<FormDataModel>? formData = [];
55+
56+
if (request.postData?.mimeType == "application/json") {
57+
bodyContentType = ContentType.json;
58+
body = request.postData?.text;
59+
}
60+
FormDataType formDataType = FormDataType.text;
61+
if (request.postData?.mimeType == "application/x-www-form-urlencoded") {
62+
bodyContentType = ContentType.formdata;
63+
var formDataStr = request.postData?.text;
64+
Map<String, String> parsedData = parseFormData(formDataStr);
65+
parsedData.forEach((key, value) {
66+
formDataType = FormDataType.text;
67+
var name = key ?? "";
68+
var val = value ?? "";
69+
formData.add(FormDataModel(
70+
name: name,
71+
value: val,
72+
type: formDataType,
73+
));
74+
});
75+
}
76+
77+
if (request.postData?.mimeType == "multipart/form-data") {
78+
bodyContentType = ContentType.formdata;
79+
var name, val;
80+
for (var fd in request.postData?.params ?? <hp.Param>[]) {
81+
name = fd.name;
82+
if (fd.contentType == "text/plain") {
83+
formDataType = FormDataType.text;
84+
val = fd.value;
85+
} else {
86+
formDataType = FormDataType.file;
87+
val = fd.fileName;
88+
}
89+
formData.add(FormDataModel(
90+
name: name,
91+
value: val,
92+
type: formDataType,
93+
));
94+
}
95+
}
96+
97+
return HttpRequestModel(
98+
method: method,
99+
url: url,
100+
headers: headers,
101+
params: params,
102+
isHeaderEnabledList: isHeaderEnabledList,
103+
isParamEnabledList: isParamEnabledList,
104+
body: body,
105+
bodyContentType: bodyContentType,
106+
formData: formData);
107+
}
108+
109+
Map<String, String> parseFormData(String? data) {
110+
// Return an empty map if the input is null or empty
111+
if (data == null || data.isEmpty) {
112+
return {};
113+
}
114+
// Split the input string into individual key-value pairs
115+
var pairs = data.split('&');
116+
117+
// Create a Map to store key-value pairs
118+
Map<String, String> result = {};
119+
120+
// Loop through the pairs and split them into keys and values
121+
for (var pair in pairs) {
122+
var keyValue = pair.split('=');
123+
124+
// Ensure the pair contains both key and value
125+
if (keyValue.length == 2) {
126+
var key = Uri.decodeComponent(keyValue[0]);
127+
var value = Uri.decodeComponent(keyValue[1]);
128+
129+
result[key] = value;
130+
}
131+
}
132+
133+
return result;
134+
}
135+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export 'curl_io.dart';
22
export 'postman_io.dart';
33
export 'insomnia_io.dart';
4+
export 'har_io.dart';

packages/apidash_core/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ dependencies:
2121
path: ../insomnia_collection
2222
postman:
2323
path: ../postman
24+
har_parser:
25+
path: ../har_parser
2426
seed: ^0.0.3
2527
xml: ^6.3.0
2628

packages/apidash_core/pubspec_overrides.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
# melos_managed_dependency_overrides: har_parser
12
# melos_managed_dependency_overrides: curl_parser,insomnia_collection,postman,seed
23
dependency_overrides:
34
curl_parser:
45
path: ../curl_parser
6+
har_parser:
7+
path: ..\\har_parser
58
insomnia_collection:
69
path: ../insomnia_collection
710
postman:

0 commit comments

Comments
 (0)