From e6b4ef071a07f725c0a6cf2a033fddb8f25994eb Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Sun, 31 Mar 2024 19:27:37 +0530 Subject: [PATCH 1/8] added ruby faraday as constant --- lib/consts.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/consts.dart b/lib/consts.dart index b0ca77bd5..017e5f103 100644 --- a/lib/consts.dart +++ b/lib/consts.dart @@ -282,6 +282,7 @@ enum CodegenLanguage { kotlinOkHttp("Kotlin (okhttp3)", "java", "kt"), pythonRequests("Python (requests)", "python", "py"), pythonHttpClient("Python (http.client)", "python", "py"), + rubyFaraday("Ruby (Faraday)", "ruby", "rb"), rustActix("Rust (Actix Client)", "rust", "rs"), rustReqwest("Rust (reqwest)", "rust", "rs"), rustCurl("Rust (curl-rust)", "rust", "rs"), From aa535a4d8d16fb109ca9c9f0bd7313bcc7bc1254 Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Sun, 31 Mar 2024 19:28:17 +0530 Subject: [PATCH 2/8] added code generator support ruby faraday --- lib/codegen/codegen.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/codegen/codegen.dart b/lib/codegen/codegen.dart index f3f606ae9..90fea8faf 100644 --- a/lib/codegen/codegen.dart +++ b/lib/codegen/codegen.dart @@ -8,6 +8,7 @@ import 'kotlin/okhttp.dart'; import 'php/guzzle.dart'; import 'python/http_client.dart'; import 'python/requests.dart'; +import 'ruby/faraday.dart'; import 'rust/actix.dart'; import 'rust/curl_rust.dart'; import 'rust/reqwest.dart'; @@ -72,6 +73,8 @@ class Codegen { .getCode(rM, boundary: boundary ?? getNewUuid()); case CodegenLanguage.pythonRequests: return PythonRequestsCodeGen().getCode(rM, boundary: boundary); + case CodegenLanguage.rubyFaraday: + return RubyFaradayCodeGen().getCode(rM, boundary: boundary ?? getNewUuid()); case CodegenLanguage.rustActix: return RustActixCodeGen().getCode(rM, boundary: boundary); case CodegenLanguage.rustCurl: From b86adacffcfce660b81b3f6c4ea0ab3e858a18fa Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Sun, 31 Mar 2024 19:29:00 +0530 Subject: [PATCH 3/8] addec class for generating faraday code --- lib/codegen/ruby/faraday.dart | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 lib/codegen/ruby/faraday.dart diff --git a/lib/codegen/ruby/faraday.dart b/lib/codegen/ruby/faraday.dart new file mode 100644 index 000000000..a01c07197 --- /dev/null +++ b/lib/codegen/ruby/faraday.dart @@ -0,0 +1,14 @@ +import 'package:apidash/consts.dart'; +import 'package:jinja/jinja.dart' as jj; +import 'package:apidash/utils/utils.dart' show getValidRequestUri; +import 'package:apidash/utils/http_utils.dart' show stripUriParams; + +import 'package:apidash/models/models.dart' show RequestModel; + +// Note that delete is a special case in Faraday as API Dash supports request +// body inside delete reqest, but Faraday does not. Hence we need to manually +// setup request body for delete request and add that to request. +// +// Refer https://lostisland.github.io/faraday/#/getting-started/quick-start?id=get-head-delete-trace +class RubyFaradayCodeGen { +} From 2671f61eb344f0c426ecb7e82aa4fdb33a751fac Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Sun, 31 Mar 2024 19:29:18 +0530 Subject: [PATCH 4/8] added templates for faraday code --- lib/codegen/ruby/faraday.dart | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/lib/codegen/ruby/faraday.dart b/lib/codegen/ruby/faraday.dart index a01c07197..043f13876 100644 --- a/lib/codegen/ruby/faraday.dart +++ b/lib/codegen/ruby/faraday.dart @@ -11,4 +11,69 @@ import 'package:apidash/models/models.dart' show RequestModel; // // Refer https://lostisland.github.io/faraday/#/getting-started/quick-start?id=get-head-delete-trace class RubyFaradayCodeGen { + final String kStringFaradayRequireStatement = """ +require 'uri' +require 'faraday' +"""; + + final String kStringFaradayMultipartRequireStatement = ''' +require 'faraday/multipart' +'''; + + final String kTemplateRequestUrl = """ +\nREQUEST_URL = URI("{{ url }}")\n\n +"""; + + final String kTemplateBody = """ +PAYLOAD = <<-{{ boundary }} +{{ body }} +{{ boundary }}\n\n +"""; + + final String kTemplateFormParamsWithFile = """ +PAYLOAD = { +{% 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 %} +}\n\n +"""; + + final String kTemplateFormParamsWithoutFile = """ +PAYLOAD = URI.encode_www_form({\n{% for param in params %} "{{ param.name }}" => "{{ param.value }}",\n{% endfor %}})\n\n +"""; + + final String kTemplateConnection = """ +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter{% if hasFile %}\n faraday.request :multipart{% endif %} +end\n\n +"""; + + final String kTemplateRequestStart = """ +response = conn.{{ method|lower }}(REQUEST_URL{% if doesMethodAcceptBody and containsBody %}, PAYLOAD{% endif %}) do |req|\n +"""; + + final String kTemplateRequestOptionsBoundary = """ + req.options.boundary = "{{ boundary }}"\n +"""; + + final String kTemplateRequestParams = """ + req.params = {\n{% for key, val in params %} "{{ key }}" => "{{ val }}",\n{% endfor %} }\n +"""; + + final String kTemplateRequestHeaders = """ + req.headers = {\n{% for key, val in headers %} "{{ key }}" => "{{ val }}",\n{% endfor %} }\n +"""; + + final String kStringDeleteRequestBody = """ + req.body = PAYLOAD +"""; + + final String kStringRequestEnd = """ +end\n +"""; + + final String kStringResponse = """ +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + } From 67155a81298cb518d827319245bef10f7bac5d91 Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Sun, 31 Mar 2024 19:29:38 +0530 Subject: [PATCH 5/8] added code generator for faraday --- lib/codegen/ruby/faraday.dart | 104 ++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/lib/codegen/ruby/faraday.dart b/lib/codegen/ruby/faraday.dart index 043f13876..04c166f02 100644 --- a/lib/codegen/ruby/faraday.dart +++ b/lib/codegen/ruby/faraday.dart @@ -76,4 +76,108 @@ puts "Status Code: #{response.status}" puts "Response Body: #{response.body}" """; + String? getCode( + RequestModel requestModel, { + String? boundary, + }) { + try { + 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( + requestModel.url, + requestModel.enabledRequestParams, + ); + + Uri? uri = rec.$1; + + if (uri == null) { + return ""; + } + + var url = stripUriParams(uri); + + result += kStringFaradayRequireStatement; + if (requestModel.hasFormDataContentType && requestModel.hasFileInFormData) { + result += kStringFaradayMultipartRequireStatement; + } + + var templateRequestUrl = jj.Template(kTemplateRequestUrl); + result += templateRequestUrl.render({"url": url}); + + if (requestModel.hasFormData) { + jj.Template payload; + if (requestModel.hasFileInFormData) { + payload = jj.Template(kTemplateFormParamsWithFile); + } else { + payload = jj.Template(kTemplateFormParamsWithoutFile); + } + result += payload.render({"params": requestModel.formDataMapList}); + } else if (requestModel.hasJsonData || requestModel.hasTextData) { + var templateBody = jj.Template(kTemplateBody); + result += templateBody.render({ + "body": requestModel.requestBody, // + "boundary": boundary, + }); + } + + // crreating faraday connection for request + var templateConnection = jj.Template(kTemplateConnection); + result += templateConnection.render({ + "hasFile": requestModel.hasFormDataContentType && requestModel.hasFileInFormData // + }); + + // start of the request sending + var templateRequestStart = jj.Template(kTemplateRequestStart); + result += templateRequestStart.render({ + "method": requestModel.method.name, // + "doesMethodAcceptBody": + kMethodsWithBody.contains(requestModel.method) && requestModel.method != HTTPVerb.delete, // + "containsBody": requestModel.hasBody, // + }); + + if (requestModel.hasFormDataContentType && requestModel.hasFileInFormData) { + var templateRequestOptionsBoundary = jj.Template(kTemplateRequestOptionsBoundary); + result += templateRequestOptionsBoundary.render({"boundary": boundary}); + } + + var headers = requestModel.enabledHeadersMap; + if (requestModel.hasBody && !requestModel.hasContentTypeHeader) { + if (requestModel.hasJsonData || requestModel.hasTextData) { + headers["Content-Type"] = requestModel.requestBodyContentType.header; + } else if (requestModel.hasFormData) { + headers["Content-Type"] = + (requestModel.hasFileInFormData) ? "multipart/form-data" : "application/x-www-form-urlencoded"; + } + } + + if (headers.isNotEmpty) { + var templateRequestHeaders = jj.Template(kTemplateRequestHeaders); + result += templateRequestHeaders.render({"headers": headers}); + } + + if (uri.hasQuery) { + var params = uri.queryParameters; + if (params.isNotEmpty) { + var templateRequestParams = jj.Template(kTemplateRequestParams); + result += templateRequestParams.render({"params": params}); + } + } + + if (requestModel.hasBody && requestModel.method == HTTPVerb.delete) { + result += kStringDeleteRequestBody; + } + + result += kStringRequestEnd; + result += kStringResponse; + return result; + } catch (e) { + return null; + } + } } From 37eb188cc3ed53b944d58d0b07539d43fd601d7d Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Sun, 31 Mar 2024 19:29:50 +0530 Subject: [PATCH 6/8] added tests for faraday code generator --- test/codegen/ruby_faraday_codegen_test.dart | 744 ++++++++++++++++++++ 1 file changed, 744 insertions(+) create mode 100644 test/codegen/ruby_faraday_codegen_test.dart diff --git a/test/codegen/ruby_faraday_codegen_test.dart b/test/codegen/ruby_faraday_codegen_test.dart new file mode 100644 index 000000000..5f94080ca --- /dev/null +++ b/test/codegen/ruby_faraday_codegen_test.dart @@ -0,0 +1,744 @@ +import 'package:apidash/codegen/codegen.dart'; +import 'package:apidash/consts.dart'; +import 'package:test/test.dart'; +import '../request_models.dart'; + +void main() { + final codeGen = Codegen(); + + group('GET Request', () { + test('GET 1', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet1, "https"), expectedCode); + }); + + test('GET 2', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/country/data") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| + req.params = { + "code" => "US", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet2, "https"), expectedCode); + }); + + test('GET 3', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/country/data") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| + req.params = { + "code" => "IND", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet3, "https"), expectedCode); + }); + + test('GET 4', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/humanize/social") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| + req.params = { + "num" => "8700000", + "digits" => "3", + "system" => "SS", + "add_space" => "true", + "trailing_zeros" => "true", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet4, "https"), expectedCode); + }); + + test('GET 5', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.github.com/repos/foss42/apidash") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| + req.headers = { + "User-Agent" => "Test Agent", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet5, "https"), expectedCode); + }); + + test('GET 6', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.github.com/repos/foss42/apidash") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| + req.headers = { + "User-Agent" => "Test Agent", + } + req.params = { + "raw" => "true", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet6, "https"), expectedCode); + }); + + test('GET 7', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet7, "https"), expectedCode); + }); + + test('GET 8', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.github.com/repos/foss42/apidash") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| + req.headers = { + "User-Agent" => "Test Agent", + } + req.params = { + "raw" => "true", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet8, "https"), expectedCode); + }); + + test('GET 9', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/humanize/social") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| + req.params = { + "num" => "8700000", + "add_space" => "true", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet9, "https"), expectedCode); + }); + + test('GET 10', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/humanize/social") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| + req.headers = { + "User-Agent" => "Test Agent", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode( + CodegenLanguage.rubyFaraday, + requestModelGet10, + "https", + ), + expectedCode); + }); + + test('GET 11', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/humanize/social") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| + req.headers = { + "User-Agent" => "Test Agent", + } + req.params = { + "num" => "8700000", + "digits" => "3", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet11, "https"), expectedCode); + }); + + test('GET 12', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/humanize/social") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.get(REQUEST_URL) do |req| +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelGet12, "https"), expectedCode); + }); + }); + + group('HEAD Request', () { + test('HEAD 1', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.head(REQUEST_URL) do |req| +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelHead1, "https"), expectedCode); + }); + + test('HEAD 2', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("http://api.apidash.dev") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.head(REQUEST_URL) do |req| +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelHead2, "http"), expectedCode); + }); + }); + + group('POST Request', () { + test('POST 1', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/case/lower") + +PAYLOAD = <<-apidash_a5d7a5d09d721f398905c39274c7fa33 +{ +"text": "I LOVE Flutter" +} +apidash_a5d7a5d09d721f398905c39274c7fa33 + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.post(REQUEST_URL, PAYLOAD) do |req| + req.headers = { + "Content-Type" => "text/plain", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost1, "https", + boundary: "a5d7a5d09d721f398905c39274c7fa33"), + expectedCode); + }); + + test('POST 2', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/case/lower") + +PAYLOAD = <<-apidash_8b9b12a09dc81f398905c39274c7fa33 +{ +"text": "I LOVE Flutter", +"flag": null, +"male": true, +"female": false, +"no": 1.2, +"arr": ["null", "true", "false", null] +} +apidash_8b9b12a09dc81f398905c39274c7fa33 + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.post(REQUEST_URL, PAYLOAD) do |req| + req.headers = { + "Content-Type" => "application/json", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost2, "https", + boundary: "8b9b12a09dc81f398905c39274c7fa33"), + expectedCode); + }); + + test('POST 3', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/case/lower") + +PAYLOAD = <<-apidash_5c3b98809e3c1f398905c39274c7fa33 +{ +"text": "I LOVE Flutter" +} +apidash_5c3b98809e3c1f398905c39274c7fa33 + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.post(REQUEST_URL, PAYLOAD) do |req| + req.headers = { + "User-Agent" => "Test Agent", + "Content-Type" => "application/json", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost3, "https", + boundary: "5c3b98809e3c1f398905c39274c7fa33"), + expectedCode); + }); + + test('POST 4', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/io/form") + +PAYLOAD = URI.encode_www_form({ + "text" => "API", + "sep" => "|", + "times" => "3", +}) + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.post(REQUEST_URL, PAYLOAD) do |req| + req.headers = { + "Content-Type" => "application/x-www-form-urlencoded", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode( + CodegenLanguage.rubyFaraday, + requestModelPost4, + "https", + ), + expectedCode); + }); + test('POST 5', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/io/form") + +PAYLOAD = URI.encode_www_form({ + "text" => "API", + "sep" => "|", + "times" => "3", +}) + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.post(REQUEST_URL, PAYLOAD) do |req| + req.headers = { + "User-Agent" => "Test Agent", + "Content-Type" => "application/x-www-form-urlencoded", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost5, "https", boundary: "test"), expectedCode); + }); + test('POST 6', () { + const expectedCode = r"""require 'uri' +require 'faraday' +require 'faraday/multipart' + +REQUEST_URL = URI("https://api.apidash.dev/io/img") + +PAYLOAD = { + "token" => Faraday::Multipart::ParamPart.new("xyz", "text/plain"), + "imfile" => Faraday::Multipart::FilePart.new("/Documents/up/1.png", "application/octet-stream"), +} + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter + faraday.request :multipart +end + +response = conn.post(REQUEST_URL, PAYLOAD) do |req| + req.options.boundary = "apidash_6f6629609fb41f398905c39274c7fa33" + req.headers = { + "Content-Type" => "multipart/form-data", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost6, "https", + boundary: "6f6629609fb41f398905c39274c7fa33"), + expectedCode); + }); + test('POST 7', () { + const expectedCode = r"""require 'uri' +require 'faraday' +require 'faraday/multipart' + +REQUEST_URL = URI("https://api.apidash.dev/io/img") + +PAYLOAD = { + "token" => Faraday::Multipart::ParamPart.new("xyz", "text/plain"), + "imfile" => Faraday::Multipart::FilePart.new("/Documents/up/1.png", "application/octet-stream"), +} + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter + faraday.request :multipart +end + +response = conn.post(REQUEST_URL, PAYLOAD) do |req| + req.options.boundary = "apidash_3955fcc0a0b71f398905c39274c7fa33" + req.headers = { + "Content-Type" => "multipart/form-data", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost7, "https", + boundary: "3955fcc0a0b71f398905c39274c7fa33"), + expectedCode); + }); + test('POST 8', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://api.apidash.dev/io/form") + +PAYLOAD = URI.encode_www_form({ + "text" => "API", + "sep" => "|", + "times" => "3", +}) + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.post(REQUEST_URL, PAYLOAD) do |req| + req.headers = { + "Content-Type" => "application/x-www-form-urlencoded", + } + req.params = { + "size" => "2", + "len" => "3", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost8, "https", boundary: "test"), expectedCode); + }); + test('POST 9', () { + const expectedCode = r"""require 'uri' +require 'faraday' +require 'faraday/multipart' + +REQUEST_URL = URI("https://api.apidash.dev/io/img") + +PAYLOAD = { + "token" => Faraday::Multipart::ParamPart.new("xyz", "text/plain"), + "imfile" => Faraday::Multipart::FilePart.new("/Documents/up/1.png", "application/octet-stream"), +} + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter + faraday.request :multipart +end + +response = conn.post(REQUEST_URL, PAYLOAD) do |req| + req.options.boundary = "apidash_599e5a20a1361f398905c39274c7fa33" + req.headers = { + "User-Agent" => "Test Agent", + "Keep-Alive" => "true", + "Content-Type" => "multipart/form-data", + } + req.params = { + "size" => "2", + "len" => "3", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost9, "https", + boundary: "599e5a20a1361f398905c39274c7fa33"), + expectedCode); + }); + }); + + group('PUT Request', () { + test('PUT 1', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://reqres.in/api/users/2") + +PAYLOAD = <<-apidash_b4e90990a34b1f398905c39274c7fa33 +{ +"name": "morpheus", +"job": "zion resident" +} +apidash_b4e90990a34b1f398905c39274c7fa33 + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.put(REQUEST_URL, PAYLOAD) do |req| + req.headers = { + "Content-Type" => "application/json", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPut1, "https", + boundary: "b4e90990a34b1f398905c39274c7fa33"), + expectedCode); + }); + }); + + group('PATCH Request', () { + test('PATCH 1', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://reqres.in/api/users/2") + +PAYLOAD = <<-apidash_b2e9a260a3931f398905c39274c7fa33 +{ +"name": "marfeus", +"job": "accountant" +} +apidash_b2e9a260a3931f398905c39274c7fa33 + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.patch(REQUEST_URL, PAYLOAD) do |req| + req.headers = { + "Content-Type" => "application/json", + } +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPatch1, "https", + boundary: "b2e9a260a3931f398905c39274c7fa33"), + expectedCode); + }); + }); + + group('DELETE Request', () { + test('DELETE 1', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://reqres.in/api/users/2") + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.delete(REQUEST_URL) do |req| +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelDelete1, "https"), expectedCode); + }); + + test('DELETE 2', () { + const expectedCode = r"""require 'uri' +require 'faraday' + +REQUEST_URL = URI("https://reqres.in/api/users/2") + +PAYLOAD = <<-apidash_f2291be0a40b1f398905c39274c7fa33 +{ +"name": "marfeus", +"job": "accountant" +} +apidash_f2291be0a40b1f398905c39274c7fa33 + +conn = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter +end + +response = conn.delete(REQUEST_URL) do |req| + req.headers = { + "Content-Type" => "application/json", + } + req.body = PAYLOAD +end + +puts "Status Code: #{response.status}" +puts "Response Body: #{response.body}" +"""; + expect( + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelDelete2, "https", + boundary: "f2291be0a40b1f398905c39274c7fa33"), + expectedCode); + }); + }); +} From bc532ae5933f7b38c2cf7d34ec62966010628ac9 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Sat, 6 Apr 2024 13:30:24 +0530 Subject: [PATCH 7/8] remove boundary --- lib/codegen/codegen.dart | 2 +- lib/codegen/ruby/faraday.dart | 61 ++++++++++++++++------------------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/lib/codegen/codegen.dart b/lib/codegen/codegen.dart index 90fea8faf..b293d67fc 100644 --- a/lib/codegen/codegen.dart +++ b/lib/codegen/codegen.dart @@ -74,7 +74,7 @@ class Codegen { case CodegenLanguage.pythonRequests: return PythonRequestsCodeGen().getCode(rM, boundary: boundary); case CodegenLanguage.rubyFaraday: - return RubyFaradayCodeGen().getCode(rM, boundary: boundary ?? getNewUuid()); + return RubyFaradayCodeGen().getCode(rM); case CodegenLanguage.rustActix: return RustActixCodeGen().getCode(rM, boundary: boundary); case CodegenLanguage.rustCurl: diff --git a/lib/codegen/ruby/faraday.dart b/lib/codegen/ruby/faraday.dart index 04c166f02..852f0dfb4 100644 --- a/lib/codegen/ruby/faraday.dart +++ b/lib/codegen/ruby/faraday.dart @@ -21,20 +21,27 @@ require 'faraday/multipart' '''; final String kTemplateRequestUrl = """ -\nREQUEST_URL = URI("{{ url }}")\n\n + +REQUEST_URL = URI("{{ url }}") + + """; final String kTemplateBody = """ -PAYLOAD = <<-{{ boundary }} +PAYLOAD = < 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 %} -}\n\n +} + + """; final String kTemplateFormParamsWithoutFile = """ @@ -44,23 +51,26 @@ PAYLOAD = URI.encode_www_form({\n{% for param in params %} "{{ param.name }}" = final String kTemplateConnection = """ conn = Faraday.new do |faraday| faraday.adapter Faraday.default_adapter{% if hasFile %}\n faraday.request :multipart{% endif %} -end\n\n +end + + """; 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 = """ - 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 = """ - 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 = """ @@ -68,7 +78,8 @@ response = conn.{{ method|lower }}(REQUEST_URL{% if doesMethodAcceptBody and con """; final String kStringRequestEnd = """ -end\n +end + """; final String kStringResponse = """ @@ -77,18 +88,11 @@ puts "Response Body: #{response.body}" """; String? getCode( - RequestModel requestModel, { - String? boundary, - }) { + RequestModel requestModel, + ) { try { 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( requestModel.url, requestModel.enabledRequestParams, @@ -121,8 +125,7 @@ puts "Response Body: #{response.body}" } else if (requestModel.hasJsonData || requestModel.hasTextData) { var templateBody = jj.Template(kTemplateBody); result += templateBody.render({ - "body": requestModel.requestBody, // - "boundary": boundary, + "body": requestModel.requestBody, }); } @@ -135,24 +138,16 @@ puts "Response Body: #{response.body}" // start of the request sending var templateRequestStart = jj.Template(kTemplateRequestStart); result += templateRequestStart.render({ - "method": requestModel.method.name, // + "method": requestModel.method.name, "doesMethodAcceptBody": 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; if (requestModel.hasBody && !requestModel.hasContentTypeHeader) { if (requestModel.hasJsonData || requestModel.hasTextData) { headers["Content-Type"] = requestModel.requestBodyContentType.header; - } else if (requestModel.hasFormData) { - headers["Content-Type"] = - (requestModel.hasFileInFormData) ? "multipart/form-data" : "application/x-www-form-urlencoded"; } } From 2c6b23cb751546b144f8e9d8b03e4bd598c1f1f6 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Sat, 6 Apr 2024 13:32:03 +0530 Subject: [PATCH 8/8] Update ruby_faraday_codegen_test.dart --- test/codegen/ruby_faraday_codegen_test.dart | 72 +++++++-------------- 1 file changed, 23 insertions(+), 49 deletions(-) diff --git a/test/codegen/ruby_faraday_codegen_test.dart b/test/codegen/ruby_faraday_codegen_test.dart index 5f94080ca..c3d8fe521 100644 --- a/test/codegen/ruby_faraday_codegen_test.dart +++ b/test/codegen/ruby_faraday_codegen_test.dart @@ -331,11 +331,11 @@ require 'faraday' REQUEST_URL = URI("https://api.apidash.dev/case/lower") -PAYLOAD = <<-apidash_a5d7a5d09d721f398905c39274c7fa33 +PAYLOAD = < "application/x-www-form-urlencoded", - } end puts "Status Code: #{response.status}" @@ -476,14 +470,13 @@ end response = conn.post(REQUEST_URL, PAYLOAD) do |req| req.headers = { "User-Agent" => "Test Agent", - "Content-Type" => "application/x-www-form-urlencoded", } end puts "Status Code: #{response.status}" puts "Response Body: #{response.body}" """; - expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost5, "https", boundary: "test"), expectedCode); + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost5, "https"), expectedCode); }); test('POST 6', () { const expectedCode = r"""require 'uri' @@ -503,18 +496,13 @@ conn = Faraday.new do |faraday| end response = conn.post(REQUEST_URL, PAYLOAD) do |req| - req.options.boundary = "apidash_6f6629609fb41f398905c39274c7fa33" - req.headers = { - "Content-Type" => "multipart/form-data", - } end puts "Status Code: #{response.status}" puts "Response Body: #{response.body}" """; expect( - codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost6, "https", - boundary: "6f6629609fb41f398905c39274c7fa33"), + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost6, "https"), expectedCode); }); test('POST 7', () { @@ -535,18 +523,13 @@ conn = Faraday.new do |faraday| end response = conn.post(REQUEST_URL, PAYLOAD) do |req| - req.options.boundary = "apidash_3955fcc0a0b71f398905c39274c7fa33" - req.headers = { - "Content-Type" => "multipart/form-data", - } end puts "Status Code: #{response.status}" puts "Response Body: #{response.body}" """; expect( - codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost7, "https", - boundary: "3955fcc0a0b71f398905c39274c7fa33"), + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost7, "https"), expectedCode); }); test('POST 8', () { @@ -566,9 +549,6 @@ conn = Faraday.new do |faraday| end response = conn.post(REQUEST_URL, PAYLOAD) do |req| - req.headers = { - "Content-Type" => "application/x-www-form-urlencoded", - } req.params = { "size" => "2", "len" => "3", @@ -578,7 +558,7 @@ end puts "Status Code: #{response.status}" puts "Response Body: #{response.body}" """; - expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost8, "https", boundary: "test"), expectedCode); + expect(codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost8, "https"), expectedCode); }); test('POST 9', () { const expectedCode = r"""require 'uri' @@ -598,11 +578,9 @@ conn = Faraday.new do |faraday| end response = conn.post(REQUEST_URL, PAYLOAD) do |req| - req.options.boundary = "apidash_599e5a20a1361f398905c39274c7fa33" req.headers = { "User-Agent" => "Test Agent", "Keep-Alive" => "true", - "Content-Type" => "multipart/form-data", } req.params = { "size" => "2", @@ -614,8 +592,7 @@ puts "Status Code: #{response.status}" puts "Response Body: #{response.body}" """; expect( - codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost9, "https", - boundary: "599e5a20a1361f398905c39274c7fa33"), + codeGen.getCode(CodegenLanguage.rubyFaraday, requestModelPost9, "https"), expectedCode); }); }); @@ -627,12 +604,12 @@ require 'faraday' REQUEST_URL = URI("https://reqres.in/api/users/2") -PAYLOAD = <<-apidash_b4e90990a34b1f398905c39274c7fa33 +PAYLOAD = <