Skip to content

JetBrains HTTP Client - Adds support for query and header params and env file #18844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 5, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.io.IOException;
import java.io.Writer;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
Expand Down Expand Up @@ -66,6 +68,9 @@ public class JetbrainsHttpClientClientCodegen extends DefaultCodegen implements

public List<String> customHeaders = new ArrayList<>();

// A map is nice, because that way I easily override variables across APIs, for pagination for example. This should add nice defaults
private final Map<String, Object> customVariables = new HashMap<>();


public CodegenType getTag() {
return CodegenType.CLIENT;
Expand Down Expand Up @@ -96,6 +101,7 @@ public JetbrainsHttpClientClientCodegen() {
embeddedTemplateDir = templateDir = "jetbrains-http-client";
apiPackage = "Apis";
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("http-client.template.env.mustache", "Apis", "http-client.template.env.json"));


cliOptions.clear();
Expand All @@ -116,6 +122,14 @@ public void processOpts() {
if (additionalProperties.containsKey(CUSTOM_HEADERS)) {
customHeaders = Arrays.asList(additionalProperties.get(CUSTOM_HEADERS).toString().split("&"));
}

bodyVariables.forEach(variable -> customVariables.put(variable, ""));
for(String header: customHeaders) {
List<String> variables = extractDoubleCurlyBraces(header);
if(!variables.isEmpty()) {
variables.forEach(v -> customVariables.put(v, ""));
}
}
}

@Override
Expand Down Expand Up @@ -152,9 +166,17 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
codegenOperation.vendorExtensions.put("customHeaders", customHeaders);
}
}

return results;
}

@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
var variables = new ArrayList<>(customVariables.keySet());
objs.put("vendorExtensionsVariables", variables);
return objs;
}

List<RequestItem> getRequests(CodegenOperation codegenOperation) {
List<RequestItem> items = new ArrayList<>();

Expand Down Expand Up @@ -195,10 +217,42 @@ List<RequestItem> getRequests(CodegenOperation codegenOperation) {
items.add(new RequestItem(codegenOperation.summary, null));
}

codegenOperation.headerParams.forEach(param -> customVariables.put(param.baseName, ""));
codegenOperation.queryParams.forEach(param -> customVariables.put(param.paramName, ""));

// I also need to grab the parameters from the path
List<String> pathVariables = extractSingleCurlyBraces(codegenOperation.path);
pathVariables.forEach(pv -> customVariables.put(pv, ""));

// Handling custom variables now
return handleCustomVariablesInRequests(items);
}

public static List<String> extractDoubleCurlyBraces(String input) {
List<String> result = new ArrayList<>();
Pattern pattern = Pattern.compile("\\{\\{([^}]+)\\}\\}");
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
result.add(matcher.group(1));
}

return result;
}

public static List<String> extractSingleCurlyBraces(String input) {
List<String> result = new ArrayList<>();
Pattern pattern = Pattern.compile("\\{([^}]+)\\}");
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
result.add(matcher.group(1));
}

return result;
}


private List<RequestItem> handleCustomVariablesInRequests(List<RequestItem> items) {
if (!bodyVariables.isEmpty()) {
for (var item : items) {
Expand All @@ -220,7 +274,7 @@ private List<RequestItem> handleCustomVariablesInRequests(List<RequestItem> item
public void postProcess() {
System.out.println("##########################################################################################");
System.out.println("# Thanks for using OpenAPI Generator. #");
System.out.println("# Please consider donation to help us maintain this project \uD83D\uDE4F #");
System.out.println("# Please consider donation to help us maintain this project \uD83D\uDE4F #");
System.out.println("# https://opencollective.com/openapi_generator/donate #");
System.out.println("# #");
System.out.println("# This generator was written by Julien Lengrand-Lambert (https://github.com/jlengrand) #");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
{{#vendorExtensions.requests}}
### {{#summary}}{{summary}}{{/summary}}
## {{name}}
{{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}{{#authMethods}}{{#isKeyInQuery}}?{{keyParamName}}={{#lambda.doubleMustache}}{queryKey}{{/lambda.doubleMustache}}{{/isKeyInQuery}}{{/authMethods}}
{{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}{{>queryParams}}
{{#consumes}}
Content-Type: {{{mediaType}}}
{{/consumes}}
{{#produces}}
Accept: {{{mediaType}}}
{{/produces}}
{{#headerParams}}
{{baseName}}: {{=<% %>=}}{{<%paramName%>}}<%={{ }}=%>
{{/headerParams}}
{{#vendorExtensions.customHeaders}}
{{{.}}}
{{/vendorExtensions.customHeaders}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dev": {
{{#vendorExtensionsVariables}}
"{{.}}" : ""{{^-last}},{{/-last}}
{{/vendorExtensionsVariables}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{! Case where there is no query params, auth does everything}}{{^queryParams}}{{#authMethods}}{{#isKeyInQuery}}?{{keyParamName}}={{#lambda.doubleMustache}}{queryKey}{{/lambda.doubleMustache}}{{/isKeyInQuery}}{{/authMethods}}{{/queryParams}}{{#queryParams}}{{! If there are query params, auth has no logic}}{{#-first}}?{{/-first}}{{paramName}}={{=<% %>=}}{{<%paramName%>}}<%={{ }}=%>{{^-last}}&{{/-last}}{{#-last}}{{#authMethods}}{{#isKeyInQuery}}&{{keyParamName}}={{#lambda.doubleMustache}}{queryKey}{{/lambda.doubleMustache}}{{/isKeyInQuery}}{{/authMethods}}{{/-last}}{{/queryParams}}
Loading
Loading