remove boundary

This commit is contained in:
Ankit Mahato
2024-04-06 13:30:24 +05:30
parent 37eb188cc3
commit bc532ae593
2 changed files with 29 additions and 34 deletions

View File

@ -74,7 +74,7 @@ class Codegen {
case CodegenLanguage.pythonRequests: case CodegenLanguage.pythonRequests:
return PythonRequestsCodeGen().getCode(rM, boundary: boundary); return PythonRequestsCodeGen().getCode(rM, boundary: boundary);
case CodegenLanguage.rubyFaraday: case CodegenLanguage.rubyFaraday:
return RubyFaradayCodeGen().getCode(rM, boundary: boundary ?? getNewUuid()); return RubyFaradayCodeGen().getCode(rM);
case CodegenLanguage.rustActix: case CodegenLanguage.rustActix:
return RustActixCodeGen().getCode(rM, boundary: boundary); return RustActixCodeGen().getCode(rM, boundary: boundary);
case CodegenLanguage.rustCurl: case CodegenLanguage.rustCurl:

View File

@ -21,20 +21,27 @@ require 'faraday/multipart'
'''; ''';
final String kTemplateRequestUrl = """ final String kTemplateRequestUrl = """
\nREQUEST_URL = URI("{{ url }}")\n\n
REQUEST_URL = URI("{{ url }}")
"""; """;
final String kTemplateBody = """ final String kTemplateBody = """
PAYLOAD = <<-{{ boundary }} PAYLOAD = <<HEREDOC
{{ body }} {{ body }}
{{ boundary }}\n\n HEREDOC
"""; """;
final String kTemplateFormParamsWithFile = """ final String kTemplateFormParamsWithFile = """
PAYLOAD = { PAYLOAD = {
{% for param in params %}{% if param.type == "text" %} "{{ param.name }}" => Faraday::Multipart::ParamPart.new("{{ param.value }}", "text/plain"), {% for param in params %}{% if param.type == "text" %} "{{ param.name }}" => Faraday::Multipart::ParamPart.new("{{ param.value }}", "text/plain"),
{% elif param.type == "file" %} "{{ param.name }}" => Faraday::Multipart::FilePart.new("{{ param.value }}", "application/octet-stream"),{% endif %}{% endfor %} {% elif param.type == "file" %} "{{ param.name }}" => Faraday::Multipart::FilePart.new("{{ param.value }}", "application/octet-stream"),{% endif %}{% endfor %}
}\n\n }
"""; """;
final String kTemplateFormParamsWithoutFile = """ final String kTemplateFormParamsWithoutFile = """
@ -44,23 +51,26 @@ PAYLOAD = URI.encode_www_form({\n{% for param in params %} "{{ param.name }}" =
final String kTemplateConnection = """ final String kTemplateConnection = """
conn = Faraday.new do |faraday| conn = Faraday.new do |faraday|
faraday.adapter Faraday.default_adapter{% if hasFile %}\n faraday.request :multipart{% endif %} faraday.adapter Faraday.default_adapter{% if hasFile %}\n faraday.request :multipart{% endif %}
end\n\n end
"""; """;
final String kTemplateRequestStart = """ final String kTemplateRequestStart = """
response = conn.{{ method|lower }}(REQUEST_URL{% if doesMethodAcceptBody and containsBody %}, PAYLOAD{% endif %}) do |req|\n response = conn.{{ method|lower }}(REQUEST_URL{% if doesMethodAcceptBody and containsBody %}, PAYLOAD{% endif %}) do |req|
""";
final String kTemplateRequestOptionsBoundary = """
req.options.boundary = "{{ boundary }}"\n
"""; """;
final String kTemplateRequestParams = """ final String kTemplateRequestParams = """
req.params = {\n{% for key, val in params %} "{{ key }}" => "{{ val }}",\n{% endfor %} }\n req.params = {
{% for key, val in params %} "{{ key }}" => "{{ val }}",\n{% endfor %} }
"""; """;
final String kTemplateRequestHeaders = """ final String kTemplateRequestHeaders = """
req.headers = {\n{% for key, val in headers %} "{{ key }}" => "{{ val }}",\n{% endfor %} }\n req.headers = {
{% for key, val in headers %} "{{ key }}" => "{{ val }}",\n{% endfor %} }
"""; """;
final String kStringDeleteRequestBody = """ final String kStringDeleteRequestBody = """
@ -68,7 +78,8 @@ response = conn.{{ method|lower }}(REQUEST_URL{% if doesMethodAcceptBody and con
"""; """;
final String kStringRequestEnd = """ final String kStringRequestEnd = """
end\n end
"""; """;
final String kStringResponse = """ final String kStringResponse = """
@ -77,18 +88,11 @@ puts "Response Body: #{response.body}"
"""; """;
String? getCode( String? getCode(
RequestModel requestModel, { RequestModel requestModel,
String? boundary, ) {
}) {
try { try {
String result = ""; String result = "";
if (boundary != null) {
// boundary needs to start with a character, hence we append apidash
// and remove hyphen characters from the existing boundary
boundary = "apidash_${boundary.replaceAll(RegExp("-"), "")}";
}
var rec = getValidRequestUri( var rec = getValidRequestUri(
requestModel.url, requestModel.url,
requestModel.enabledRequestParams, requestModel.enabledRequestParams,
@ -121,8 +125,7 @@ puts "Response Body: #{response.body}"
} else if (requestModel.hasJsonData || requestModel.hasTextData) { } else if (requestModel.hasJsonData || requestModel.hasTextData) {
var templateBody = jj.Template(kTemplateBody); var templateBody = jj.Template(kTemplateBody);
result += templateBody.render({ result += templateBody.render({
"body": requestModel.requestBody, // "body": requestModel.requestBody,
"boundary": boundary,
}); });
} }
@ -135,24 +138,16 @@ puts "Response Body: #{response.body}"
// start of the request sending // start of the request sending
var templateRequestStart = jj.Template(kTemplateRequestStart); var templateRequestStart = jj.Template(kTemplateRequestStart);
result += templateRequestStart.render({ result += templateRequestStart.render({
"method": requestModel.method.name, // "method": requestModel.method.name,
"doesMethodAcceptBody": "doesMethodAcceptBody":
kMethodsWithBody.contains(requestModel.method) && requestModel.method != HTTPVerb.delete, // kMethodsWithBody.contains(requestModel.method) && requestModel.method != HTTPVerb.delete, //
"containsBody": requestModel.hasBody, // "containsBody": requestModel.hasBody,
}); });
if (requestModel.hasFormDataContentType && requestModel.hasFileInFormData) {
var templateRequestOptionsBoundary = jj.Template(kTemplateRequestOptionsBoundary);
result += templateRequestOptionsBoundary.render({"boundary": boundary});
}
var headers = requestModel.enabledHeadersMap; var headers = requestModel.enabledHeadersMap;
if (requestModel.hasBody && !requestModel.hasContentTypeHeader) { if (requestModel.hasBody && !requestModel.hasContentTypeHeader) {
if (requestModel.hasJsonData || requestModel.hasTextData) { if (requestModel.hasJsonData || requestModel.hasTextData) {
headers["Content-Type"] = requestModel.requestBodyContentType.header; headers["Content-Type"] = requestModel.requestBodyContentType.header;
} else if (requestModel.hasFormData) {
headers["Content-Type"] =
(requestModel.hasFileInFormData) ? "multipart/form-data" : "application/x-www-form-urlencoded";
} }
} }