Files
apidash/lib/codegen/php/curl.dart
Shruti Roy d253f62f01 added template for user defined class File
- a constructor which takes a human readable name and the file
      and internally stores the contents of the file
    - if the file does not exist, then file_get_contents function
      returns false, in that case the contents of the file is an empty
      string
2024-03-17 23:49:48 +05:30

119 lines
2.3 KiB
Dart

import 'dart:convert';
import 'package:jinja/jinja.dart' as jj;
import 'package:apidash/utils/utils.dart'
show getNewUuid, getValidRequestUri, requestModelToHARJsonRequest;
import 'package:apidash/models/models.dart' show RequestModel;
import 'package:apidash/consts.dart';
class PHPcURLCodeGen {
//starting template
final String kTemplateStart = """
<?php
""";
final String kTemplateUri = """
\$uri = "{{uri}}";
""";
//defining query parameters
String kTemplateParams = """
\$queryParams = [{{params}}];
\$queryString = "?" . http_build_query(\$queryParams);
if (count(\$queryParams) > 0) {
\$uri .= \$queryString;
}
""";
//initialising the request
String kTemplateRequestInit = """
\$request = curl_init(\$uri);
""";
//specifying headers
String kTemplateHeaders = """
\$headers = [{{headers}}];
curl_setopt(\$request, CURLOPT_HTTPHEADER, \$headers);
""";
//request template
String kTemplateRequest = """
curl_setopt(\$request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(\$request, {{method}}, 1);
""";
//ending template
final String kStringRequestEnd = """
\$response = curl_exec(\$request);
curl_close(\$request);
var_dump(\$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 : "";
}
}
""";
}
""";
//
String kMultiPartBodyWithFiles = """
\$request_body = build_data_files(\$boundary, \$fields, \$files);
""";
//
String kMultiPartBodyWithoutFiles = """
\$request_body = build_data(\$boundary, \$fields);
""";
//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 "";
}
}
}