|
| 1 | +package de.sonallux.spotify.generator.java.templates; |
| 2 | + |
| 3 | +import com.github.mustachejava.Mustache; |
| 4 | +import com.github.mustachejava.MustacheFactory; |
| 5 | +import com.google.common.base.CaseFormat; |
| 6 | +import de.sonallux.spotify.core.model.SpotifyWebApiCategory; |
| 7 | +import de.sonallux.spotify.core.model.SpotifyWebApiEndpoint; |
| 8 | +import de.sonallux.spotify.generator.java.EndpointHelper; |
| 9 | +import de.sonallux.spotify.generator.java.util.JavaPackage; |
| 10 | +import de.sonallux.spotify.generator.java.util.JavaUtils; |
| 11 | +import de.sonallux.spotify.generator.java.util.Markdown2Html; |
| 12 | +import lombok.Getter; |
| 13 | +import lombok.Setter; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import static java.nio.file.StandardOpenOption.*; |
| 26 | +import static java.util.stream.Collectors.joining; |
| 27 | +import static java.util.stream.Collectors.toList; |
| 28 | + |
| 29 | +public class ApiTemplate { |
| 30 | + private Mustache apiTemplate; |
| 31 | + private Mustache requestTemplate; |
| 32 | + |
| 33 | + private Path outputFolder; |
| 34 | + |
| 35 | + public ApiTemplate loadTemplate(MustacheFactory mustacheFactory) { |
| 36 | + this.apiTemplate = mustacheFactory.compile("templates/api.mustache"); |
| 37 | + this.requestTemplate = mustacheFactory.compile("templates/request.mustache"); |
| 38 | + return this; |
| 39 | + } |
| 40 | + |
| 41 | + public void generate(SpotifyWebApiCategory category, Path outputFolder, JavaPackage basePackage) throws IOException { |
| 42 | + this.outputFolder = outputFolder; |
| 43 | + |
| 44 | + var apisJavaPackage = basePackage.child("apis"); |
| 45 | + var className = JavaUtils.getClassName(category); |
| 46 | + var requestsJavaPackage = apisJavaPackage.child(className.replace("Api", "").toLowerCase()); |
| 47 | + |
| 48 | + var context = new HashMap<String, Object>(); |
| 49 | + context.put("package", apisJavaPackage.getName()); |
| 50 | + context.put("requestsPackage", requestsJavaPackage.getName()); |
| 51 | + context.put("name", category.getName()); |
| 52 | + context.put("className", className); |
| 53 | + context.put("documentationLink", category.getLink()); |
| 54 | + var endpointContexts = new ArrayList<>(); |
| 55 | + for (var endpoint : category.getEndpointList()) { |
| 56 | + endpointContexts.add(buildEndpointContext(endpoint, requestsJavaPackage)); |
| 57 | + } |
| 58 | + context.put("endpoints", endpointContexts); |
| 59 | + |
| 60 | + var packageFolder = getPackageFolder(outputFolder, apisJavaPackage); |
| 61 | + var outputFile = packageFolder.resolve(JavaUtils.getFileName(className)); |
| 62 | + generateFile(outputFile, apiTemplate, context); |
| 63 | + } |
| 64 | + |
| 65 | + private Path getPackageFolder(Path baseFolder, JavaPackage javaPackage) throws IOException { |
| 66 | + var packageFolder = baseFolder.resolve(javaPackage.getPath()); |
| 67 | + Files.createDirectories(packageFolder); |
| 68 | + return packageFolder; |
| 69 | + } |
| 70 | + |
| 71 | + private void generateFile(Path outputFile, Mustache mustacheTemplate, Object scope) throws IOException { |
| 72 | + try (var writer = Files.newBufferedWriter(outputFile, CREATE, TRUNCATE_EXISTING, WRITE)) { |
| 73 | + mustacheTemplate.execute(writer, scope); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private Map<String, Object> buildEndpointContext(SpotifyWebApiEndpoint endpoint, JavaPackage javaPackage) throws IOException { |
| 78 | + var requiredParameters = generateEndpointRequest(endpoint, javaPackage); |
| 79 | + |
| 80 | + var context = new HashMap<String, Object>(); |
| 81 | + var methodName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, JavaUtils.shrinkEndpointId(endpoint)); |
| 82 | + context.put("methodName", methodName); |
| 83 | + context.put("name", endpoint.getName()); |
| 84 | + context.put("description", Markdown2Html.convertToSingleLine(endpoint.getDescription())); |
| 85 | + context.put("requestBuilder", JavaUtils.getEndpointRequestBuilderName(endpoint)); |
| 86 | + |
| 87 | + context.put("requiredParameters", requiredParameters.stream() |
| 88 | + .map(Parameter::asMethodParameter) |
| 89 | + .collect(joining(", "))); |
| 90 | + context.put("javaDocParameters", requiredParameters.stream() |
| 91 | + .map(Parameter::asJavaDoc) |
| 92 | + .collect(toList())); |
| 93 | + |
| 94 | + context.put("arguments", Stream.concat(Stream.of(new Parameter("apiClient", "ApiClient", "")), requiredParameters.stream()) |
| 95 | + .map(Parameter::getJavaName) |
| 96 | + .collect(joining(", "))); |
| 97 | + |
| 98 | + return context; |
| 99 | + } |
| 100 | + |
| 101 | + private List<Parameter> generateEndpointRequest(SpotifyWebApiEndpoint endpoint, JavaPackage javaPackage) throws IOException { |
| 102 | + EndpointHelper.fixDuplicateEndpointParameters(endpoint); |
| 103 | + |
| 104 | + var context = new HashMap<String, Object>(); |
| 105 | + context.put("package", javaPackage.getName()); |
| 106 | + context.put("name", endpoint.getName() + " request"); |
| 107 | + context.put("documentationLink", endpoint.getLink()); |
| 108 | + context.put("className", JavaUtils.getEndpointRequestBuilderName(endpoint)); |
| 109 | + context.put("httpMethod", endpoint.getHttpMethod()); |
| 110 | + context.put("path", endpoint.getPath()); |
| 111 | + context.put("responseType", getResponseType(endpoint)); |
| 112 | + context.put("responseDescription", Markdown2Html.convertToLines(endpoint.getResponseDescription())); |
| 113 | + if (endpoint.getNotes() != null) { |
| 114 | + context.put("hasNotes", true); |
| 115 | + context.put("notes", Markdown2Html.convertToLines(endpoint.getNotes())); |
| 116 | + } |
| 117 | + if (endpoint.getScopes().size() > 0) { |
| 118 | + context.put("scopes", String.join(", ", endpoint.getScopes())); |
| 119 | + } |
| 120 | + |
| 121 | + var requiredPathParameters = new ArrayList<Parameter>(); |
| 122 | + var requiredQueryParameters = new ArrayList<Parameter>(); |
| 123 | + var requiredBodyParameters = new ArrayList<Parameter>(); |
| 124 | + var optionalPathParameters = new ArrayList<Parameter>(); |
| 125 | + var optionalQueryParameters = new ArrayList<Parameter>(); |
| 126 | + var optionalBodyParameters = new ArrayList<Parameter>(); |
| 127 | + |
| 128 | + for (var parameter : endpoint.getParameters()) { |
| 129 | + switch (parameter.getLocation()) { |
| 130 | + case PATH: { |
| 131 | + addParameter(parameter, requiredPathParameters, optionalPathParameters); |
| 132 | + break; |
| 133 | + } |
| 134 | + case QUERY: { |
| 135 | + addParameter(parameter, requiredQueryParameters, optionalQueryParameters); |
| 136 | + break; |
| 137 | + } |
| 138 | + case BODY: { |
| 139 | + addParameter(parameter, requiredBodyParameters, optionalBodyParameters); |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + context.put("requiredPathParameters", requiredPathParameters); |
| 146 | + context.put("requiredQueryParameters", requiredQueryParameters); |
| 147 | + context.put("requiredBodyParameters", requiredBodyParameters); |
| 148 | + context.put("optionalPathParameters", optionalPathParameters); |
| 149 | + context.put("optionalQueryParameters", optionalQueryParameters); |
| 150 | + context.put("optionalBodyParameters", optionalBodyParameters); |
| 151 | + |
| 152 | + List<Parameter> requiredParameterList = new ArrayList<>(); |
| 153 | + requiredParameterList.add(new Parameter("apiClient", "ApiClient", "The API client")); |
| 154 | + requiredParameterList.addAll(requiredPathParameters); |
| 155 | + requiredParameterList.addAll(requiredQueryParameters); |
| 156 | + requiredParameterList.addAll(requiredBodyParameters); |
| 157 | + context.put("requiredParameters", requiredParameterList.stream() |
| 158 | + .map(Parameter::asMethodParameter) |
| 159 | + .collect(joining(", "))); |
| 160 | + |
| 161 | + context.put("requiredJavaDocParameters", requiredParameterList.stream().map(Parameter::asJavaDoc).collect(toList())); |
| 162 | + |
| 163 | + var packageFolder = getPackageFolder(outputFolder, javaPackage); |
| 164 | + var outputFile = packageFolder.resolve(JavaUtils.getFileName(JavaUtils.getEndpointRequestBuilderName(endpoint))); |
| 165 | + generateFile(outputFile, requestTemplate, context); |
| 166 | + |
| 167 | + return requiredParameterList.subList(1, requiredParameterList.size()); |
| 168 | + } |
| 169 | + |
| 170 | + private void addParameter(SpotifyWebApiEndpoint.Parameter parameter, List<Parameter> requiredParams, List<Parameter> optionalParams) { |
| 171 | + var mappedParameter = new Parameter(parameter.getName(), parameter.getType(), parameter.getDescription()); |
| 172 | + if (parameter.isRequired()) { |
| 173 | + requiredParams.add(mappedParameter); |
| 174 | + } else { |
| 175 | + optionalParams.add(mappedParameter); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private String getResponseType(SpotifyWebApiEndpoint endpoint) { |
| 180 | + if (endpoint.getResponseTypes().stream() |
| 181 | + .map(SpotifyWebApiEndpoint.ResponseType::getType) |
| 182 | + .distinct().count() == 1) { |
| 183 | + return JavaUtils.mapToPrimitiveJavaType(endpoint.getResponseTypes().get(0).getType()); |
| 184 | + } |
| 185 | + var nonVoidResponseTypes = endpoint.getResponseTypes().stream() |
| 186 | + .map(SpotifyWebApiEndpoint.ResponseType::getType).filter(t -> !"Void".equals(t)).distinct().collect(toList()); |
| 187 | + if (nonVoidResponseTypes.size() == 1) { |
| 188 | + return JavaUtils.mapToPrimitiveJavaType(endpoint.getResponseTypes().get(0).getType()); |
| 189 | + } |
| 190 | + return ""; |
| 191 | + } |
| 192 | + |
| 193 | + @Getter |
| 194 | + @Setter |
| 195 | + private static class Parameter { |
| 196 | + private String javaName; |
| 197 | + private String jsonName; |
| 198 | + private String type; |
| 199 | + private String description; |
| 200 | + |
| 201 | + public Parameter(String jsonName, String type, String description) { |
| 202 | + this.javaName = JavaUtils.escapeFieldName(jsonName); |
| 203 | + this.jsonName = jsonName; |
| 204 | + this.type = JavaUtils.mapToPrimitiveJavaType(type); |
| 205 | + this.description = Markdown2Html.convertToSingleLine(description); |
| 206 | + } |
| 207 | + |
| 208 | + public String asMethodParameter() { |
| 209 | + return type + " " + javaName; |
| 210 | + } |
| 211 | + public String asJavaDoc() { |
| 212 | + return "@param " + javaName + " " + description; |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments