Prevent generation of unnecessary lines in Python codegen

This commit is contained in:
Mixel2004
2023-07-04 22:32:23 +05:30
parent e5ab9b84d0
commit ac4a2160bf

View File

@ -10,13 +10,7 @@ import requests
import json import json
def main(): def main():
url = '{{url}}' url = '{{url}}'{{params}}{{body}}{{headers}}
params = {{params}}
{{body}}
headers = {{headers}}
response = requests.{{method}}( response = requests.{{method}}(
url{{request_params}}{{request_headers}}{{request_body}} url{{request_params}}{{request_headers}}{{request_body}}
@ -28,7 +22,7 @@ def main():
print('Response Body:', response.text) print('Response Body:', response.text)
else: else:
print('Error Status Code:', status_code) print('Error Status Code:', status_code)
print('Error Response Body:', response.text) print('Error Response Body:', response.reason)
main() main()
'''; ''';
@ -47,9 +41,11 @@ main()
var paramsList = requestModel.requestParams; var paramsList = requestModel.requestParams;
String params = ''; String params = '';
if (paramsList != null) { if (paramsList != null) {
hasParams = true;
for (var param in paramsList) { for (var param in paramsList) {
params += '\n "${param.k}": "${param.v}",'; if (param.k.isNotEmpty) {
hasParams = true;
params += '\n "${param.k}": "${param.v}",';
}
} }
} }
@ -57,19 +53,23 @@ main()
var requestBody = requestModel.requestBody; var requestBody = requestModel.requestBody;
String requestBodyString = ''; String requestBodyString = '';
if (requestBody != null) { if (requestBody != null && requestBody.isNotEmpty) {
hasBody = true; hasBody = true;
var bodyType = requestModel.requestBodyContentType; var bodyType = requestModel.requestBodyContentType;
if (bodyType == ContentType.text) { if (bodyType == ContentType.text) {
requestBodyString = "data = '''$requestBody'''\n"; requestBodyString = "data = '''$requestBody'''\n";
} else if (bodyType == ContentType.json) { } else if (bodyType == ContentType.json) {
int index = requestBody.lastIndexOf("}"); int index = requestBody.lastIndexOf("}");
if (requestBody[index - 1] == ",") { index--;
requestBody = requestBody.substring(0, index - 1) + while (requestBody[index] == " " || requestBody[index] == "\n") {
requestBody.substring(index); index--;
}
if (requestBody[index] == ",") {
requestBody = requestBody.substring(0, index) +
requestBody.substring(index + 1);
} }
requestBodyString = requestBodyString =
"data = '''$requestBody''' \ndata = json.loads(data)\n"; "data = '''$requestBody''' \n data = json.loads(data)\n";
} }
} }
@ -91,9 +91,9 @@ main()
var template = jj.Template(kPythonTemplate); var template = jj.Template(kPythonTemplate);
var pythonCode = template.render({ var pythonCode = template.render({
'url': url, 'url': url,
'params': '{$params \n }', 'params': hasParams ? '\n\n params = {$params \n }' : '',
'body': requestBodyString, 'body': hasBody ? '\n\n $requestBodyString' : '',
'headers': headers, 'headers': hasHeaders ? '\n\n headers = $headers' : '',
'method': method, 'method': method,
'request_params': hasParams ? ', params=params' : '', 'request_params': hasParams ? ', params=params' : '',
'request_headers': hasHeaders ? ', headers=headers' : '', 'request_headers': hasHeaders ? ', headers=headers' : '',