From d1e27598bc3d4c9e622334c99ab2463a4ce4014a Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sun, 1 Oct 2023 22:10:17 +0530 Subject: [PATCH] feat: HAR codegen --- lib/codegen/codegen.dart | 3 ++ lib/codegen/others/har.dart | 68 +++++++++++++++++++++++++++++++++++++ lib/consts.dart | 1 + 3 files changed, 72 insertions(+) create mode 100644 lib/codegen/others/har.dart diff --git a/lib/codegen/codegen.dart b/lib/codegen/codegen.dart index f9dcb20f..8735b8d3 100644 --- a/lib/codegen/codegen.dart +++ b/lib/codegen/codegen.dart @@ -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: diff --git a/lib/codegen/others/har.dart b/lib/codegen/others/har.dart new file mode 100644 index 00000000..7487c110 --- /dev/null +++ b/lib/codegen/others/har.dart @@ -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 convertRequestModelToHARJson(RequestModel requestModel) { + Map 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; + } + } +} diff --git a/lib/consts.dart b/lib/consts.dart index 0f9c0ba3..e6a5808b 100644 --- a/lib/consts.dart +++ b/lib/consts.dart @@ -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"),