mirror of
https://github.com/foss42/apidash.git
synced 2025-06-05 19:14:25 +08:00
feat: HAR codegen
This commit is contained in:
@ -4,6 +4,7 @@ import 'dart/pkg_http.dart';
|
||||
import 'kotlin/pkg_okhttp.dart';
|
||||
import 'python/pkg_http_client.dart';
|
||||
import 'python/pkg_requests.dart';
|
||||
import 'others/har.dart';
|
||||
|
||||
class Codegen {
|
||||
String? getCode(
|
||||
@ -12,6 +13,8 @@ class Codegen {
|
||||
String defaultUriScheme,
|
||||
) {
|
||||
switch (codegenLanguage) {
|
||||
case CodegenLanguage.har:
|
||||
return HARCodeGen().getCode(requestModel);
|
||||
case CodegenLanguage.dartHttp:
|
||||
return DartHttpCodeGen().getCode(requestModel, defaultUriScheme);
|
||||
case CodegenLanguage.kotlinOkHttp:
|
||||
|
68
lib/codegen/others/har.dart
Normal file
68
lib/codegen/others/har.dart
Normal file
@ -0,0 +1,68 @@
|
||||
import 'dart:convert';
|
||||
import 'package:apidash/consts.dart';
|
||||
import 'package:apidash/utils/utils.dart' show rowsToMap;
|
||||
import 'package:apidash/models/models.dart' show RequestModel;
|
||||
|
||||
Map<String, dynamic> convertRequestModelToHARJson(RequestModel requestModel) {
|
||||
Map<String, dynamic> json = {};
|
||||
bool hasBody = false;
|
||||
|
||||
json["method"] = requestModel.method.name.toUpperCase();
|
||||
json["url"] = requestModel.url;
|
||||
json["httpVersion"] = "HTTP/1.1";
|
||||
json["queryString"] = [];
|
||||
json["headers"] = [];
|
||||
|
||||
var paramsList = requestModel.requestParams;
|
||||
if (paramsList != null) {
|
||||
var params = rowsToMap(requestModel.requestParams) ?? {};
|
||||
if (params.isNotEmpty) {
|
||||
for (final k in params.keys) {
|
||||
json["queryString"].add({"name": k, "value": params[k]});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var method = requestModel.method;
|
||||
var requestBody = requestModel.requestBody;
|
||||
if (kMethodsWithBody.contains(method) && requestBody != null) {
|
||||
var contentLength = utf8.encode(requestBody).length;
|
||||
if (contentLength > 0) {
|
||||
hasBody = true;
|
||||
json["postData"] = {};
|
||||
json["postData"]["mimeType"] =
|
||||
kContentTypeMap[requestModel.requestBodyContentType] ?? "";
|
||||
json["postData"]["text"] = requestBody;
|
||||
}
|
||||
}
|
||||
|
||||
var headersList = requestModel.requestHeaders;
|
||||
if (headersList != null || hasBody) {
|
||||
var headers = rowsToMap(requestModel.requestHeaders) ?? {};
|
||||
if (headers.isNotEmpty || hasBody) {
|
||||
if (hasBody) {
|
||||
json["headers"].add({
|
||||
"name": "Content-Type",
|
||||
"value": kContentTypeMap[requestModel.requestBodyContentType] ?? ""
|
||||
});
|
||||
}
|
||||
for (final k in headers.keys) {
|
||||
json["headers"].add({"name": k, "value": headers[k]});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
class HARCodeGen {
|
||||
String? getCode(RequestModel requestModel) {
|
||||
try {
|
||||
var harString =
|
||||
kEncoder.convert(convertRequestModelToHARJson(requestModel));
|
||||
return harString;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -231,6 +231,7 @@ const kDefaultHttpMethod = HTTPVerb.get;
|
||||
const kDefaultContentType = ContentType.json;
|
||||
|
||||
enum CodegenLanguage {
|
||||
har("HAR", "json", "json"),
|
||||
dartHttp("Dart (http)", "dart", "dart"),
|
||||
kotlinOkHttp("Kotlin (okhttp3)", "java", "kt"),
|
||||
pythonHttpClient("Python (http.client)", "python", "py"),
|
||||
|
Reference in New Issue
Block a user