1
0
mirror of https://github.com/foss42/apidash.git synced 2025-07-03 23:05:32 +08:00

added starting request part for getCode

This commit is contained in:
Aditya Mayukh Som
2024-03-21 18:07:17 +05:30
parent 9c4f7ef56b
commit 6e86422285

@ -74,6 +74,72 @@ public class Main {
try {
String result = '';
bool hasBody = false;
var rec = getValidRequestUri(
requestModel.url,
requestModel.enabledRequestParams,
);
// uri is already generated based on url and enabled request params
Uri? uri = rec.$1;
if (uri == null) {
return "";
}
// this is the common import and will be imported for every generated code snippet
result += kTemplateUnirestImports;
// java file io packages are to be imported only when there is a form with file present
if (requestModel.hasBody &&
kMethodsWithBody.contains(requestModel.method) &&
requestModel.hasFormData &&
requestModel.hasFileInFormData) {
result += kTemplateFileIoImports;
}
// adding the main method under Main class
result += kTemplateStart;
var url = stripUriParams(uri);
// 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;
// generating the URL to which the request has to be submitted
var templateUrl = jj.Template(kTemplateUrl);
result += templateUrl.render({"url": url});
// creating request body if available
var rM = requestModel.copyWith(url: url);
var harJson = requestModelToHARJsonRequest(rM, useEnabled: true);
// if request type is not form data, the request method can include
// a body, and the body of the request is not null, in that case
// we need to parse the body as it is, and write it to the body
if (!requestModel.hasFormData &&
kMethodsWithBody.contains(method) &&
requestBody != null) {
// find out the content length for the request, i.e. request body's size
var contentLength = utf8.encode(requestBody).length;
if (contentLength > 0) {
var templateBodyContent = jj.Template(kTemplateRequestBodyContent);
hasBody = true;
if (harJson["postData"]?["text"] != null) {
result += templateBodyContent.render({
"body": kEncoder.convert(harJson["postData"]["text"]).substring(
1, kEncoder.convert(harJson["postData"]["text"]).length - 1)
});
}
}
}
var templateRequestCreation = jj.Template(kTemplateRequestCreation);
result +=
templateRequestCreation.render({"method": method.name.toLowerCase()});
return result;
} catch (e) {