[ISSUE 375] - removed request body utility functions and added curlopt array setup

This commit is contained in:
adityamayukhsom
2024-04-03 12:43:01 +05:30
parent ad56099bbc
commit d341c56eef

View File

@ -11,10 +11,23 @@ class PHPcURLCodeGen {
"""; """;
final String kTemplateUri = """ String kTemplateBody = r'''
\$uri = "{{uri}}"; {%- 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 //defining query parameters
String kTemplateParams = """ String kTemplateParams = """
@ -34,134 +47,37 @@ if (count(\$queryParams) > 0) {
"""; """;
String kTemplateBody = """ String kTemplateRequestOptsInit = r'''
curl_setopt_array($request, [
\$request_body = <<<EOF CURLOPT_RETURNTRANSFER => 1,
{{body}} CURLOPT_CUSTOMREQUEST => '{{ method|upper }}',
EOF;
""";
//specifying headers
String kTemplateHeaders = """
\$headers = [{{headers}}];
curl_setopt(\$request, CURLOPT_HTTPHEADER, \$headers);
""";
String kTemplateFormHeaderContentType = '''
multipart/form-data; boundary={{boundary}}''';
String kTemplateRequest = """
curl_setopt(\$request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(\$request, {{method}}, 1);
""";
''';
String kStringHeaderOpt = r'''
CURLOPT_HTTPHEADER => $headers,
''';
//passing the request body //passing the request body
String kStringRequestBody = """ String kStringRequestBodyOpt = r'''
curl_setopt(\$request, CURLOPT_POSTFIELDS, \$request_body); CURLOPT_POSTFIELDS => $request_body,
''';
""";
//ending template //ending template
final String kStringRequestEnd = """ final String kStringRequestEnd = r'''
\$response = curl_exec(\$request); CURLOPT_SSL_VERIFYPEER => 0,
curl_close(\$request); CURLOPT_MAXREDIRS => 10,
var_dump(\$response); CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
]);
"""; $response = curl_exec($request);
//template for generating unique boundary curl_close($request);
String kBoundaryUniqueIdTemplate = """
\$boundary = "{{uuid}}";
"""; $httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE);
echo "Status Code: " . $httpCode . "\n";
// echo $response;
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);
""";
String? getCode(RequestModel requestModel) { String? getCode(RequestModel requestModel) {
String uuid = getNewUuid(); String uuid = getNewUuid();