From ad56099bbc03704b868cbdd65eb2675d5a0ec8cd Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:35:19 +0530 Subject: [PATCH 01/19] changed library name --- lib/consts.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/consts.dart b/lib/consts.dart index 668bdcb9..0a03311f 100644 --- a/lib/consts.dart +++ b/lib/consts.dart @@ -291,7 +291,7 @@ enum CodegenLanguage { javaHttpClient("Java (HttpClient)", "java", "java"), juliaHttp("Julia (HTTP)", "julia", "jl"), phpGuzzle("PHP (guzzle)", "php", "php"), - phpCurl("PHP (curl)", "php", "php"); + phpCurl("PHP (cURL)", "php", "php"); const CodegenLanguage(this.label, this.codeHighlightLang, this.ext); final String label; From d341c56eefa032ecc1c4721b55e1a83fbf5a9bf7 Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:43:01 +0530 Subject: [PATCH 02/19] [ISSUE 375] - removed request body utility functions and added curlopt array setup --- lib/codegen/php/curl.dart | 164 ++++++++++---------------------------- 1 file changed, 40 insertions(+), 124 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 0b9fc2f9..68dbc547 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -11,10 +11,23 @@ class PHPcURLCodeGen { """; - final String kTemplateUri = """ -\$uri = "{{uri}}"; + String kTemplateBody = r''' +{%- if body is iterable -%} +$request_body = [ +{%- for data in body %} +{%- if data.type == 'text' %} + '{{ data.name }}' => '{{ data.value }}', +{%- elif data.type == 'file' %} + '{{ data.name }}' => new CURLFILE('{{ data.value }}'), +{%- endif %} +{%- endfor %} +]; +{%- else -%} +$request_body = '{{body}}'; +{%- endif %} -"""; + +'''; //defining query parameters String kTemplateParams = """ @@ -34,134 +47,37 @@ if (count(\$queryParams) > 0) { """; - String kTemplateBody = """ - -\$request_body = << 1, + CURLOPT_CUSTOMREQUEST => '{{ method|upper }}', +'''; + String kStringHeaderOpt = r''' + CURLOPT_HTTPHEADER => $headers, +'''; //passing the request body - String kStringRequestBody = """ -curl_setopt(\$request, CURLOPT_POSTFIELDS, \$request_body); - -"""; + String kStringRequestBodyOpt = r''' + CURLOPT_POSTFIELDS => $request_body, +'''; //ending template - final String kStringRequestEnd = """ -\$response = curl_exec(\$request); -curl_close(\$request); -var_dump(\$response); + final String kStringRequestEnd = r''' + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); -"""; +$response = curl_exec($request); - //template for generating unique boundary - String kBoundaryUniqueIdTemplate = """ -\$boundary = "{{uuid}}"; +curl_close($request); -"""; - - // - String kFileClassString = """ -class File -{ - public string \$name; - public string \$filename; - public string \$content; - - function __construct(\$name, \$filename) - { - \$this->name = \$name; - \$this->filename = \$filename; - \$available_content = file_get_contents(\$this->filename); - \$this->content = \$available_content ? \$available_content : ""; - } -} - - -"""; - - //function to build formdata without 'file' type - String kBuildFormDataFunctionWithoutFilesString = """ -function build_data(\$boundary, \$fields) -{ - \$data = ''; - \$eol = "\\r\\n"; - - \$delimiter = '-------------' . \$boundary; - - foreach (\$fields as \$name => \$content) { - \$data .= "--" . \$delimiter . \$eol - . 'Content-Disposition: form-data; name="' . \$name . "\\"" . \$eol . \$eol - . \$content . \$eol; - } - \$data .= "--" . \$delimiter . "--" . \$eol; - return \$data; -} -"""; - - //function to build formdata with 'file' type - String kBuildFormDataFunctionWithFilesString = """ -function build_data_files(\$boundary, \$fields, \$files) -{ - \$data = ''; - \$eol = "\\r\\n"; - - \$delimiter = '-------------' . \$boundary; - - foreach (\$fields as \$name => \$content) { - \$data .= "--" . \$delimiter . \$eol - . 'Content-Disposition: form-data; name="' . \$name . "\\"" . \$eol . \$eol - . \$content . \$eol; - } - - foreach (\$files as \$uploaded_file) { - if (\$uploaded_file instanceof File) { - \$data .= "--" . \$delimiter . \$eol - . 'Content-Disposition: form-data; name="' . \$uploaded_file->name . '"; filename="' . \$uploaded_file->filename . '"' . \$eol - . 'Content-Transfer-Encoding: binary' . \$eol; - - \$data .= \$eol; - \$data .= \$uploaded_file->content . \$eol; - } - } - \$data .= "--" . \$delimiter . "--" . \$eol; - - - return \$data; -} - -"""; - - // - String kMultiPartBodyWithFiles = """ -\$request_body = build_data_files(\$boundary, \$fields, \$files); - -"""; - - // - String kMultiPartBodyWithoutFiles = """ -\$request_body = build_data(\$boundary, \$fields); - -"""; +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; String? getCode(RequestModel requestModel) { String uuid = getNewUuid(); From 21e1355d32a3cbc31962fbd6833e2f1d312d4e32 Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:43:49 +0530 Subject: [PATCH 03/19] [ISSUE 375] - reformatted query params and header rendering --- lib/codegen/php/curl.dart | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 68dbc547..ec921a2d 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -30,15 +30,27 @@ $request_body = '{{body}}'; '''; //defining query parameters - String kTemplateParams = """ + String kTemplateParams = r''' +$queryParams = [ +{%- for name, value in params %} + '{{ name }}' => '{{ value }}', +{%- endfor %} +]; +$uri .= '?' . http_build_query($queryParams); -\$queryParams = [{{params}}]; -\$queryString = "?" . http_build_query(\$queryParams); -if (count(\$queryParams) > 0) { - \$uri .= \$queryString; -} -"""; +'''; + + //specifying headers + String kTemplateHeaders = r''' +$headers = [ +{%- for name, value in headers %} + '{{ name }}: {{ value }}', +{%- endfor %} +]; + + +'''; //initialising the request String kTemplateRequestInit = """ From 8ac737b013782b8202bfd034937ea95023e41512 Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:44:39 +0530 Subject: [PATCH 04/19] [ISSUE 375] - changed string style to single quotes --- lib/codegen/php/curl.dart | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index ec921a2d..671c90f2 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -6,10 +6,17 @@ import 'package:apidash/models/models.dart' show RequestModel; import 'package:apidash/consts.dart'; class PHPcURLCodeGen { - final String kTemplateStart = """ + final String kTemplateStart = r''' Date: Wed, 3 Apr 2024 12:45:42 +0530 Subject: [PATCH 05/19] [ISSUE 375] - removed uuid boundary --- lib/codegen/php/curl.dart | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 671c90f2..1a700340 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -98,9 +98,6 @@ echo $response; '''; String? getCode(RequestModel requestModel) { - String uuid = getNewUuid(); - uuid = uuid.replaceAll(RegExp(r'-'), ""); - try { String result = ""; bool hasHeaders = false; From c04ae9ec304a1a2adb713bff892b5ec5f4e82089 Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:46:27 +0530 Subject: [PATCH 06/19] [ISSUE 375] - header rendering through iteration in jinja template --- lib/codegen/php/curl.dart | 71 +++++++-------------------------------- 1 file changed, 12 insertions(+), 59 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 1a700340..673ec4dc 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -151,67 +151,20 @@ echo $response; } } - // renders the initial request init function call - var templateRequestInit = jj.Template(kTemplateRequestInit); - result += templateRequestInit.render(); + var headers = requestModel.enabledHeadersMap; + if (requestModel.hasBody) { + if (!headers.containsKey('Content-Type')) { + if (requestModel.hasJsonData) { + headers['Content-Type'] = 'application/json'; + } else if (requestModel.hasTextData) { + headers['Content-Type'] = 'text/plain'; + } + } + } - var harJson = - requestModelToHARJsonRequest(requestModel, useEnabled: true); - - var headers = harJson["headers"]; - - //parses and adds the headers - if (headers.isNotEmpty || requestModel.hasFormData) { + if (headers.isNotEmpty) { var templateHeader = jj.Template(kTemplateHeaders); - var m = {}; - for (var i in headers) { - m[i["name"]] = i["value"]; - } - - if (requestModel.hasFormData) { - // we will override any existing boundary and use our own boundary - m['Content-Type'] = - "multipart/form-data; boundary=-------------$uuid"; - - var boundaryUniqueIdTemplate = - jj.Template(kBoundaryUniqueIdTemplate); - result += boundaryUniqueIdTemplate.render({"uuid": uuid}); - - var fieldsString = '\$fields = [\n'; - var filesString = '\$files = [\n'; - - for (var formData in requestModel.formDataMapList) { - if (formData['type'] == 'text') { - // the four spaces on the left hand side are for indentation, hence do not remove - fieldsString += - ' "${formData['name']}" => "${formData['value']}",\n'; - } else if (formData['type'] == 'file') { - filesString += - ' new File("${formData['name']}", "${formData['value']}"),\n'; - } - } - - fieldsString += '];\n'; - filesString += '];\n'; - - result += fieldsString; - if (requestModel.hasFileInFormData) { - result += filesString; - - result += kMultiPartBodyWithFiles; - } else { - result += kMultiPartBodyWithoutFiles; - } - } - - var headersString = '\n'; - m.forEach((key, value) { - headersString += "\t\t\t\t'$key: $value', \n"; - }); - - result += templateHeader.render({ - "headers": headersString, - }); + result += templateHeader.render({'headers': headers}); } // contains the HTTP method associated with the request From 623abe3611b3e7e6efc3746286afa9acb4115bb4 Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:47:17 +0530 Subject: [PATCH 07/19] [ISSUE 375] - request body is rendered based on whether formdata or text/json type --- lib/codegen/php/curl.dart | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 673ec4dc..1728d371 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -130,7 +130,16 @@ echo $response; } var templateUri = jj.Template(kTemplateUri); - result += templateUri.render({"uri": requestModel.url}); + + //renders the request body contains the HTTP method associated with the request + if (kMethodsWithBody.contains(requestModel.method) && requestModel.hasBody) { + hasBody = true; + // contains the entire request body as a string if body is present + var templateBody = jj.Template(kTemplateBody); + result += templateBody.render({ + 'body': requestModel.hasFormData ? requestModel.formDataMapList : requestModel.requestBody, + }); + } //checking and adding query params if (uri.hasQuery) { From 02b10c979c173bbb297bd0b1df72f45369d2346c Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:47:55 +0530 Subject: [PATCH 08/19] [ISSUE 375] - removed utility class rendering for file and formdata handling --- lib/codegen/php/curl.dart | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 1728d371..b2f54e42 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -116,19 +116,6 @@ echo $response; var templateStart = jj.Template(kTemplateStart); result += templateStart.render(); - // if the request does not contain any file uploads, we do not need - // to add the class for File in the request - if (requestModel.hasFileInFormData) { - result += kFileClassString; - } - - //adds the function to build formdata with or without 'file' type - if (requestModel.hasFormData) { - result += requestModel.hasFileInFormData - ? kBuildFormDataFunctionWithFilesString - : kBuildFormDataFunctionWithoutFilesString; - } - var templateUri = jj.Template(kTemplateUri); //renders the request body contains the HTTP method associated with the request From d87ac4b46fdf280b97c4ae745de9914793d2ef2d Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:48:20 +0530 Subject: [PATCH 09/19] [ISSUE 375] - strip parameters from url before rendering --- lib/codegen/php/curl.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index b2f54e42..3f64d4bc 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -117,6 +117,7 @@ echo $response; result += templateStart.render(); var templateUri = jj.Template(kTemplateUri); + result += templateUri.render({'uri': stripUriParams(uri)}); //renders the request body contains the HTTP method associated with the request if (kMethodsWithBody.contains(requestModel.method) && requestModel.hasBody) { From 9103f8d5af40ded56f2d5674081f038a49d3e056 Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:49:08 +0530 Subject: [PATCH 10/19] [ISSUE 375] - query params rendered through iteration in jinja template --- lib/codegen/php/curl.dart | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 3f64d4bc..8f1a4f7a 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -131,20 +131,10 @@ echo $response; //checking and adding query params if (uri.hasQuery) { - var params = uri.queryParameters; - if (params.isNotEmpty) { + if (requestModel.enabledParamsMap.isNotEmpty) { hasQuery = true; var templateParams = jj.Template(kTemplateParams); - - // generating the map of key and value for the query parameters - List queryList = []; - for (MapEntry entry in params.entries) { - String entryStr = "\"${entry.key}\" => \"${entry.value}\""; - queryList.add(entryStr); - } - String paramsString = "\n ${queryList.join(",\n ")}\n"; - - result += templateParams.render({"params": paramsString}); + result += templateParams.render({"params": requestModel.enabledParamsMap}); } } From 2ce5727c01f5f00f5f8ad9c792faa2f66761057c Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:50:32 +0530 Subject: [PATCH 11/19] [ISSUE 375] - request initialization and body setup --- lib/codegen/php/curl.dart | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 8f1a4f7a..8e6f915c 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -154,21 +154,7 @@ echo $response; result += templateHeader.render({'headers': headers}); } - // contains the HTTP method associated with the request - var method = requestModel.method; - - // contains the entire request body as a string if body is present - var requestBody = requestModel.requestBody; - - //renders the request body - if (kMethodsWithBody.contains(method) && requestBody != null) { - var contentLength = utf8.encode(requestBody).length; - if (contentLength > 0) { - hasBody = true; - var templateBody = jj.Template(kTemplateBody); - result += templateBody.render({"body": requestBody}); - } - } + result += kStringRequestInit; //renders the request temlate var templateRequest = jj.Template(kTemplateRequest); @@ -180,7 +166,7 @@ echo $response; }); if (hasBody || requestModel.hasFormData) { - result += kStringRequestBody; + result += kStringRequestBodyOpt; } //and of the request From c2d25768b382210d301fcdb4e8de0eef8c32da71 Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:51:07 +0530 Subject: [PATCH 12/19] [ISSUE 375] - removed utility function to convert http verb into curl option --- lib/codegen/php/curl.dart | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 8e6f915c..9ec0ce36 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -177,24 +177,4 @@ echo $response; return null; } } - - //function for http verb to curl mapping - String httpMethod(String methodName) { - switch (methodName) { - case "POST": - return "CURLOPT_POST"; - case "GET": - return "CURLOPT_HTTPGET"; - case "PUT": - return "CURLOPT_PUT"; - case "DELETE": - return "CURLOPT_CUSTOMREQUEST"; - case "PATCH": - return "CURLOPT_CUSTOMREQUEST"; - case "HEAD": - return "CURLOPT_NOBODY"; - default: - return ""; - } - } } From 388e82c3c3b9dc518ee499f8df195df577ec94bc Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:51:35 +0530 Subject: [PATCH 13/19] [ISSUE 375] - php curl options setup --- lib/codegen/php/curl.dart | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 9ec0ce36..fc88f14d 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -154,17 +154,15 @@ echo $response; result += templateHeader.render({'headers': headers}); } + // renders the initial request init function call result += kStringRequestInit; //renders the request temlate - var templateRequest = jj.Template(kTemplateRequest); - result += templateRequest.render({ - "authority": uri.authority, - "method": httpMethod(method.name.toUpperCase()), - "path": uri.path, - "queryParamsStr": hasQuery ? "queryParamsStr" : "", - }); - + var templateRequestOptsInit = jj.Template(kTemplateRequestOptsInit); + result += templateRequestOptsInit.render({'method': requestModel.method.name}); + if (headers.isNotEmpty) { + result += kStringHeaderOpt; + } if (hasBody || requestModel.hasFormData) { result += kStringRequestBodyOpt; } From 5e5eb747b486dcf47fc2834d4bdbe55f7c11771b Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:52:01 +0530 Subject: [PATCH 14/19] [ISSUE 375] - fixed imports during code generation --- lib/codegen/php/curl.dart | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index fc88f14d..d40dc82a 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -1,7 +1,5 @@ -import 'dart:convert'; import 'package:jinja/jinja.dart' as jj; -import 'package:apidash/utils/utils.dart' - show getNewUuid, getValidRequestUri, requestModelToHARJsonRequest; +import 'package:apidash/utils/utils.dart' show getValidRequestUri, stripUriParams; import 'package:apidash/models/models.dart' show RequestModel; import 'package:apidash/consts.dart'; From 8b17c90265050b0bebd693b96a952947e7a7d541 Mon Sep 17 00:00:00 2001 From: adityamayukhsom Date: Wed, 3 Apr 2024 12:52:25 +0530 Subject: [PATCH 15/19] [ISSUE 375] - added test for php curl code generation --- test/codegen/php_curl_codegen_test.dart | 961 ++++++++++++++++++++++++ 1 file changed, 961 insertions(+) create mode 100644 test/codegen/php_curl_codegen_test.dart diff --git a/test/codegen/php_curl_codegen_test.dart b/test/codegen/php_curl_codegen_test.dart new file mode 100644 index 00000000..53b5d075 --- /dev/null +++ b/test/codegen/php_curl_codegen_test.dart @@ -0,0 +1,961 @@ +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''' 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet1, 'https'), expectedCode); + }); + test('GET 2', () { + const expectedCode = r''' 'US', +]; +$uri .= '?' . http_build_query($queryParams); + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet2, 'https'), expectedCode); + }); + test('GET 3', () { + const expectedCode = r''' 'IND', +]; +$uri .= '?' . http_build_query($queryParams); + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet3, "https"), expectedCode); + }); + test('GET 4', () { + const expectedCode = r''' '8700000', + 'digits' => '3', + 'system' => 'SS', + 'add_space' => 'true', + 'trailing_zeros' => 'true', +]; +$uri .= '?' . http_build_query($queryParams); + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet4, "https"), expectedCode); + }); + + test('GET 5', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet5, 'https'), expectedCode); + }); + + test('GET 6', () { + const expectedCode = r''' 'true', +]; +$uri .= '?' . http_build_query($queryParams); + +$headers = [ + 'User-Agent: Test Agent', +]; + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet6, "https"), expectedCode); + }); + + test('GET 7', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet7, "https"), expectedCode); + }); + + test('GET 8', () { + const expectedCode = r''' 'true', +]; +$uri .= '?' . http_build_query($queryParams); + +$headers = [ + 'User-Agent: Test Agent', +]; + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet8, "https"), expectedCode); + }); + + test('GET 9', () { + const expectedCode = r''' '8700000', + 'add_space' => 'true', +]; +$uri .= '?' . http_build_query($queryParams); + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet9, "https"), expectedCode); + }); + + test('GET 10', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet10, "https"), expectedCode); + }); + + test('GET 11', () { + const expectedCode = r''' '8700000', + 'digits' => '3', +]; +$uri .= '?' . http_build_query($queryParams); + +$headers = [ + 'User-Agent: Test Agent', +]; + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet11, "https"), expectedCode); + }); + + test('GET 12', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet12, "https"), expectedCode); + }); + }); + + group('HEAD Request', () { + test('HEAD 1', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'HEAD', + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelHead1, "https"), expectedCode); + }); + + test('HEAD 2', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'HEAD', + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelHead2, "http"), expectedCode); + }); + }); + + group('POST Request', () { + test('POST 1', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPost1, "https"), expectedCode); + }); + + test('POST 2', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPost2, "https"), expectedCode); + }); + test('POST 3', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPost3, "https"), expectedCode); + }); + + test('POST 4', () { + const expectedCode = r''' 'API', + 'sep' => '|', + 'times' => '3', +]; + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPost4, "https"), expectedCode); + }); + + test('POST 5', () { + const expectedCode = r''' 'API', + 'sep' => '|', + 'times' => '3', +]; + +$headers = [ + 'User-Agent: Test Agent', +]; + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPost5, "https"), expectedCode); + }); + test('POST 6', () { + const expectedCode = r''' 'xyz', + 'imfile' => new CURLFILE('/Documents/up/1.png'), +]; + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPost6, "https"), expectedCode); + }); + test('POST 7', () { + const expectedCode = r''' 'xyz', + 'imfile' => new CURLFILE('/Documents/up/1.png'), +]; + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPost7, "https"), expectedCode); + }); + test('POST 8', () { + const expectedCode = r''' 'API', + 'sep' => '|', + 'times' => '3', +]; + +$queryParams = [ + 'size' => '2', + 'len' => '3', +]; +$uri .= '?' . http_build_query($queryParams); + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPost8, "https"), expectedCode); + }); + test('POST 9', () { + const expectedCode = r''' 'xyz', + 'imfile' => new CURLFILE('/Documents/up/1.png'), +]; + +$queryParams = [ + 'size' => '2', + 'len' => '3', +]; +$uri .= '?' . http_build_query($queryParams); + +$headers = [ + 'User-Agent: Test Agent', + 'Keep-Alive: true', +]; + +$request = curl_init($uri); + +curl_setopt_array($request, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPost9, "https"), expectedCode); + }); + }); + group('PUT Request', () { + test('PUT 1', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'PUT', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPut1, "https"), expectedCode); + }); + }); + group('PATCH Request', () { + test('PATCH 1', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'PATCH', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelPatch1, "https"), expectedCode); + }); + }); + group('DELETE Request', () { + test('DELETE 1', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'DELETE', + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelDelete1, "https"), expectedCode); + }); + test('DELETE 2', () { + const expectedCode = r''' 1, + CURLOPT_CUSTOMREQUEST => 'DELETE', + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => $request_body, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, +]); + +$response = curl_exec($request); + +curl_close($request); + +$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); +echo "Status Code: " . $httpCode . "\n"; +echo $response; +'''; + expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelDelete2, "https"), expectedCode); + }); + }); +} From 6d9cbcc04ab843d0231658f5df790e5ffb0126a6 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Sat, 6 Apr 2024 23:25:36 +0530 Subject: [PATCH 16/19] add newline --- lib/codegen/php/curl.dart | 18 ++- test/codegen/php_curl_codegen_test.dart | 164 ++++++++++++++++-------- 2 files changed, 122 insertions(+), 60 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 9f28b3b5..84964d95 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -1,5 +1,6 @@ import 'package:jinja/jinja.dart' as jj; -import 'package:apidash/utils/utils.dart' show getValidRequestUri, stripUriParams; +import 'package:apidash/utils/utils.dart' + show getValidRequestUri, stripUriParams; import 'package:apidash/models/models.dart' show RequestModel; import 'package:apidash/consts.dart'; @@ -92,7 +93,7 @@ curl_close($request); $httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); echo "Status Code: " . $httpCode . "\n"; -echo $response; +echo $response . "\n"; '''; String? getCode(RequestModel requestModel) { @@ -117,12 +118,15 @@ echo $response; result += templateUri.render({'uri': stripUriParams(uri)}); //renders the request body contains the HTTP method associated with the request - if (kMethodsWithBody.contains(requestModel.method) && requestModel.hasBody) { + if (kMethodsWithBody.contains(requestModel.method) && + requestModel.hasBody) { hasBody = true; // contains the entire request body as a string if body is present var templateBody = jj.Template(kTemplateBody); result += templateBody.render({ - 'body': requestModel.hasFormData ? requestModel.formDataMapList : requestModel.requestBody, + 'body': requestModel.hasFormData + ? requestModel.formDataMapList + : requestModel.requestBody, }); } @@ -131,7 +135,8 @@ echo $response; if (requestModel.enabledParamsMap.isNotEmpty) { hasQuery = true; var templateParams = jj.Template(kTemplateParams); - result += templateParams.render({"params": requestModel.enabledParamsMap}); + result += templateParams + .render({"params": requestModel.enabledParamsMap}); } } @@ -156,7 +161,8 @@ echo $response; //renders the request temlate var templateRequestOptsInit = jj.Template(kTemplateRequestOptsInit); - result += templateRequestOptsInit.render({'method': requestModel.method.name}); + result += templateRequestOptsInit + .render({'method': requestModel.method.name}); if (headers.isNotEmpty) { result += kStringHeaderOpt; } diff --git a/test/codegen/php_curl_codegen_test.dart b/test/codegen/php_curl_codegen_test.dart index 53b5d075..aee8b43c 100644 --- a/test/codegen/php_curl_codegen_test.dart +++ b/test/codegen/php_curl_codegen_test.dart @@ -30,9 +30,11 @@ curl_close($request); $httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE); echo "Status Code: " . $httpCode . "\n"; -echo $response; +echo $response . "\n"; '''; - expect(codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet1, 'https'), expectedCode); + expect( + codeGen.getCode(CodegenLanguage.phpCurl, requestModelGet1, 'https'), + expectedCode); }); test('GET 2', () { const expectedCode = r''' Date: Sat, 6 Apr 2024 23:52:27 +0530 Subject: [PATCH 17/19] Update options --- lib/codegen/php/curl.dart | 4 - test/codegen/php_curl_codegen_test.dart | 108 ------------------------ 2 files changed, 112 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 84964d95..40706843 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -80,11 +80,7 @@ curl_setopt_array($request, [ //ending template final String kStringRequestEnd = r''' - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); diff --git a/test/codegen/php_curl_codegen_test.dart b/test/codegen/php_curl_codegen_test.dart index aee8b43c..afcb8e3e 100644 --- a/test/codegen/php_curl_codegen_test.dart +++ b/test/codegen/php_curl_codegen_test.dart @@ -17,11 +17,7 @@ $request = curl_init($uri); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -51,11 +47,7 @@ $request = curl_init($uri); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -85,11 +77,7 @@ $request = curl_init($uri); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -123,11 +111,7 @@ $request = curl_init($uri); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -158,11 +142,7 @@ curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => $headers, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -198,11 +178,7 @@ curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => $headers, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -228,11 +204,7 @@ $request = curl_init($uri); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -268,11 +240,7 @@ curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => $headers, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -304,11 +272,7 @@ $request = curl_init($uri); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -339,11 +303,7 @@ curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => $headers, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -380,11 +340,7 @@ curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => $headers, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -410,11 +366,7 @@ $request = curl_init($uri); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'GET', - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -442,11 +394,7 @@ $request = curl_init($uri); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'HEAD', - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -472,11 +420,7 @@ $request = curl_init($uri); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'HEAD', - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -514,11 +458,7 @@ curl_setopt_array($request, [ CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -559,11 +499,7 @@ curl_setopt_array($request, [ CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -599,11 +535,7 @@ curl_setopt_array($request, [ CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -636,11 +568,7 @@ curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -678,11 +606,7 @@ curl_setopt_array($request, [ CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -713,11 +637,7 @@ curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -748,11 +668,7 @@ curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -790,11 +706,7 @@ curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -837,11 +749,7 @@ curl_setopt_array($request, [ CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -879,11 +787,7 @@ curl_setopt_array($request, [ CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -921,11 +825,7 @@ curl_setopt_array($request, [ CURLOPT_CUSTOMREQUEST => 'PATCH', CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -952,11 +852,7 @@ $request = curl_init($uri); curl_setopt_array($request, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'DELETE', - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); @@ -993,11 +889,7 @@ curl_setopt_array($request, [ CURLOPT_CUSTOMREQUEST => 'DELETE', CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $request_body, - CURLOPT_SSL_VERIFYPEER => 0, - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, ]); $response = curl_exec($request); From fc8e7176089b015fce8825464e889b9693b27461 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Sun, 7 Apr 2024 00:16:18 +0530 Subject: [PATCH 18/19] Update consts.dart --- lib/consts.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/consts.dart b/lib/consts.dart index 9c056d20..4f0bb509 100644 --- a/lib/consts.dart +++ b/lib/consts.dart @@ -293,8 +293,8 @@ enum CodegenLanguage { javaAsyncHttpClient("Java (asynchttpclient)", "java", "java"), javaHttpClient("Java (HttpClient)", "java", "java"), juliaHttp("Julia (HTTP)", "julia", "jl"), - phpGuzzle("PHP (guzzle)", "php", "php"), - phpCurl("PHP (cURL)", "php", "php"); + phpCurl("PHP (cURL)", "php", "php"), + phpGuzzle("PHP (guzzle)", "php", "php"); const CodegenLanguage(this.label, this.codeHighlightLang, this.ext); final String label; From 8bc24721f05fbdcc06a23e0f9ea1184fce1ece54 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Sun, 7 Apr 2024 00:30:45 +0530 Subject: [PATCH 19/19] Fixes --- lib/codegen/php/curl.dart | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/codegen/php/curl.dart b/lib/codegen/php/curl.dart index 40706843..974159ee 100644 --- a/lib/codegen/php/curl.dart +++ b/lib/codegen/php/curl.dart @@ -95,7 +95,6 @@ echo $response . "\n"; String? getCode(RequestModel requestModel) { try { String result = ""; - bool hasQuery = false; bool hasBody = false; var rec = getValidRequestUri( @@ -114,8 +113,7 @@ echo $response . "\n"; result += templateUri.render({'uri': stripUriParams(uri)}); //renders the request body contains the HTTP method associated with the request - if (kMethodsWithBody.contains(requestModel.method) && - requestModel.hasBody) { + if (requestModel.hasBody) { hasBody = true; // contains the entire request body as a string if body is present var templateBody = jj.Template(kTemplateBody); @@ -129,7 +127,6 @@ echo $response . "\n"; //checking and adding query params if (uri.hasQuery) { if (requestModel.enabledParamsMap.isNotEmpty) { - hasQuery = true; var templateParams = jj.Template(kTemplateParams); result += templateParams .render({"params": requestModel.enabledParamsMap}); @@ -137,13 +134,10 @@ echo $response . "\n"; } var headers = requestModel.enabledHeadersMap; - if (requestModel.hasBody) { - if (!headers.containsKey('Content-Type')) { - if (requestModel.hasJsonData) { - headers['Content-Type'] = 'application/json'; - } else if (requestModel.hasTextData) { - headers['Content-Type'] = 'text/plain'; - } + if (requestModel.hasBody && !requestModel.hasContentTypeHeader) { + if (requestModel.hasJsonData || requestModel.hasTextData) { + headers[kHeaderContentType] = + requestModel.requestBodyContentType.header; } }