From d836c427bea5258375d85771d75b36f6572a79f4 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Fri, 6 Sep 2024 14:33:29 +0800 Subject: [PATCH] http-client-java, sync code from autorest.java (#4345) sync https://github.com/Azure/autorest.java/pull/2936 change to JavaSettings,java is already included in prior sync PR. This should be the last PR we need to sync. --- .../http/client/generator/core/Javagen.java | 102 ++++++++- .../android/AndroidClientMethodMapper.java | 116 ++++++++++ .../android/AndroidExceptionMapper.java | 24 ++ .../mapper/android/AndroidMapperFactory.java | 55 +++++ .../android/AndroidMethodGroupMapper.java | 31 +++ .../mapper/android/AndroidModelMapper.java | 24 ++ .../android/AndroidPrimitiveMapper.java | 55 +++++ .../android/AndroidProxyMethodMapper.java | 88 +++++++ .../android/AndroidProxyParameterMapper.java | 24 ++ .../android/AndroidServiceClientMapper.java | 75 ++++++ .../core/mapper/android/package-info.java | 4 + .../android/AndroidClientException.java | 25 ++ .../android/AndroidClientMethod.java | 102 +++++++++ .../android/AndroidClientModel.java | 63 +++++ .../android/AndroidMethodGroupClient.java | 36 +++ .../clientmodel/android/AndroidProxy.java | 36 +++ .../android/AndroidProxyMethod.java | 140 ++++++++++++ .../android/AndroidProxyMethodParameter.java | 72 ++++++ .../android/AndroidServiceClient.java | 59 +++++ .../clientmodel/android/package-info.java | 3 + .../android/AndroidClientMethodTemplate.java | 216 ++++++++++++++++++ .../template/android/AndroidEnumTemplate.java | 39 ++++ .../android/AndroidExceptionTemplate.java | 22 ++ .../android/AndroidMethodGroupTemplate.java | 66 ++++++ .../android/AndroidProxyTemplate.java | 39 ++++ .../android/AndroidResponseTemplate.java | 31 +++ .../AndroidServiceAsyncClientTemplate.java | 27 +++ .../AndroidServiceClientBuilderTemplate.java | 130 +++++++++++ .../android/AndroidServiceClientTemplate.java | 93 ++++++++ .../AndroidServiceSyncClientTemplate.java | 26 +++ .../android/AndroidTemplateFactory.java | 75 ++++++ .../AndroidWrapperClientMethodTemplate.java | 20 ++ .../core/template/android/package-info.java | 4 + 33 files changed, 1921 insertions(+), 1 deletion(-) create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidClientMethodMapper.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidExceptionMapper.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidMapperFactory.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidMethodGroupMapper.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidModelMapper.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidPrimitiveMapper.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidProxyMethodMapper.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidProxyParameterMapper.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidServiceClientMapper.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/package-info.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientException.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientMethod.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientModel.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidMethodGroupClient.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxy.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxyMethod.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxyMethodParameter.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidServiceClient.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/package-info.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidClientMethodTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidEnumTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidExceptionTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidMethodGroupTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidProxyTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidResponseTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceAsyncClientTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceClientBuilderTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceClientTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceSyncClientTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidTemplateFactory.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidWrapperClientMethodTemplate.java create mode 100644 packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/package-info.java diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/Javagen.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/Javagen.java index 79388efa6..3d4b89605 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/Javagen.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/Javagen.java @@ -11,6 +11,7 @@ import com.microsoft.typespec.http.client.generator.core.extension.plugin.NewPlu import com.microsoft.typespec.http.client.generator.core.extension.plugin.PluginLogger; import com.microsoft.typespec.http.client.generator.core.mapper.Mappers; import com.microsoft.typespec.http.client.generator.core.mapper.PomMapper; +import com.microsoft.typespec.http.client.generator.core.mapper.android.AndroidMapperFactory; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.AsyncSyncClient; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Client; import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientBuilder; @@ -36,6 +37,8 @@ import com.microsoft.typespec.http.client.generator.core.model.projectmodel.Text import com.microsoft.typespec.http.client.generator.core.model.xmlmodel.XmlFile; import com.microsoft.typespec.http.client.generator.core.postprocessor.Postprocessor; import com.microsoft.typespec.http.client.generator.core.preprocessor.Preprocessor; +import com.microsoft.typespec.http.client.generator.core.template.Templates; +import com.microsoft.typespec.http.client.generator.core.template.android.AndroidTemplateFactory; import com.microsoft.typespec.http.client.generator.core.util.ClientModelUtil; import com.microsoft.typespec.http.client.generator.core.util.SchemaUtil; import com.azure.core.util.CoreUtils; @@ -73,7 +76,10 @@ public class Javagen extends NewPlugin { this.clear(); JavaSettings settings = JavaSettings.getInstance(); + return settings.isAndroid() ? generateAndroid(settings) : generateJava(settings); + } + private boolean generateJava(JavaSettings settings) { try { // Step 1: Parse input yaml as CodeModel CodeModel codeModel = new Preprocessor(this, connection, pluginName, sessionId) @@ -110,6 +116,100 @@ public class Javagen extends NewPlugin { return true; } + private boolean generateAndroid(JavaSettings settings) { + try { + // Step 1: Parse input yaml as CodeModel + CodeModel codeModel = new Preprocessor(this, connection, pluginName, sessionId) + .processCodeModel(); + + // Step 2: Map + Mappers.setFactory(new AndroidMapperFactory()); + Client client = Mappers.getClientMapper().map(codeModel); + + // Step 3: Write to templates + Templates.setFactory(new AndroidTemplateFactory()); + JavaPackage javaPackage = new JavaPackage(this); + // Service client + javaPackage + .addServiceClient(client.getServiceClient().getPackage(), client.getServiceClient().getClassName(), + client.getServiceClient()); + + if (JavaSettings.getInstance().isGenerateClientInterfaces()) { + javaPackage + .addServiceClientInterface(client.getServiceClient().getInterfaceName(), client.getServiceClient()); + } + + // Async/sync service clients + for (AsyncSyncClient asyncClient : client.getAsyncClients()) { + javaPackage.addAsyncServiceClient(asyncClient.getPackageName(), asyncClient); + } + + for (AsyncSyncClient syncClient : client.getSyncClients()) { + javaPackage.addSyncServiceClient(syncClient.getPackageName(), syncClient); + } + + // Service client builder + for (ClientBuilder clientBuilder : client.getClientBuilders()) { + javaPackage.addServiceClientBuilder(clientBuilder); + } + + // Method group + for (MethodGroupClient methodGroupClient : client.getServiceClient().getMethodGroupClients()) { + javaPackage.addMethodGroup(methodGroupClient.getPackage(), methodGroupClient.getClassName(), + methodGroupClient); + if (JavaSettings.getInstance().isGenerateClientInterfaces()) { + javaPackage.addMethodGroupInterface(methodGroupClient.getInterfaceName(), methodGroupClient); + } + } + + // Response + for (ClientResponse response : client.getResponseModels()) { + javaPackage.addClientResponse(response.getPackage(), response.getName(), response); + } + + // Client model + for (ClientModel model : client.getModels()) { + javaPackage.addModel(model.getPackage(), model.getName(), model); + } + + // Enum + for (EnumType enumType : client.getEnums()) { + javaPackage.addEnum(enumType.getPackage(), enumType.getName(), enumType); + } + + // Exception + for (ClientException exception : client.getExceptions()) { + javaPackage.addException(exception.getPackage(), exception.getName(), exception); + } + + // XML sequence wrapper + for (XmlSequenceWrapper xmlSequenceWrapper : client.getXmlSequenceWrappers()) { + javaPackage.addXmlSequenceWrapper(xmlSequenceWrapper.getPackage(), + xmlSequenceWrapper.getWrapperClassName(), xmlSequenceWrapper); + } + + // Package-info + for (PackageInfo packageInfo : client.getPackageInfos()) { + javaPackage.addPackageInfo(packageInfo.getPackage(), "package-info", packageInfo); + } + + // TODO: POM, Manager + //Step 4: Print to files + new Postprocessor(this).postProcess(javaPackage.getJavaFiles().stream() + .collect(Collectors.toMap(JavaFile::getFilePath, file -> file.getContents().toString()))); + + String artifactId = JavaSettings.getInstance().getArtifactId(); + if (!(artifactId == null || artifactId.isEmpty())) { + writeFile("src/main/resources/" + artifactId + ".properties", + "name=${project.artifactId}\nversion=${project.version}\n", null); + } + } catch (Exception ex) { + logger.error("Failed to generate code.", ex); + return false; + } + return true; + } + CodeModel parseCodeModel(String fileName) { String file = readFile(fileName); Representer representer = new Representer(new DumperOptions()) { @@ -199,7 +299,7 @@ public class Javagen extends NewPlugin { if (CoreUtils.isNullOrEmpty(serviceClients)) { serviceClients = Collections.singletonList(client.getServiceClient()); } - TestContext testContext = new TestContext(serviceClients, client.getSyncClients()); + TestContext testContext = new TestContext<>(serviceClients, client.getSyncClients()); // base test class javaPackage.addProtocolTestBase(testContext); diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidClientMethodMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidClientMethodMapper.java new file mode 100644 index 000000000..278eb6feb --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidClientMethodMapper.java @@ -0,0 +1,116 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.mapper.android; + +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Operation; +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.mapper.ClientMethodMapper; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodParameter; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.GenericType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ReturnValue; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.android.AndroidClientMethod; + +public class AndroidClientMethodMapper extends ClientMethodMapper { + private static final ClientMethodMapper INSTANCE = new AndroidClientMethodMapper(); + + private static final ClientMethodParameter ANDROID_CONTEXT_PARAM = new ClientMethodParameter.Builder() + .description("The context to associate with this operation.") + .wireType(ClassType.ANDROID_CONTEXT) + .name("context") + .annotations(new java.util.ArrayList<>()) + .constant(false) + .defaultValue(null) + .fromClient(false) + .finalParameter(false) + .required(false) + .build(); + + protected AndroidClientMethodMapper() { + } + + public static ClientMethodMapper getInstance() { + return INSTANCE; + } + + @Override + protected ClientMethod.Builder getClientMethodBuilder() { + return new AndroidClientMethod.Builder(); + } + + @Override + protected ClientMethodParameter getContextParameter(boolean isProtocolMethod) { + return ANDROID_CONTEXT_PARAM; + } + + @Override + protected IType getContextType() { + return ClassType.ANDROID_CONTEXT; + } + + @Override + protected IType createPagedRestResponseReturnType(IType elementType) { + return GenericType.AndroidCompletableFuture(GenericType.AndroidPagedResponse(elementType)); + } + + @Override + protected IType createPagedAsyncReturnType(IType elementType) { + return GenericType.AndroidPagedResponse(elementType); + } + + @Override + protected IType createPagedSyncReturnType(IType elementType) { + return GenericType.AndroidPagedResponse(elementType); + } + + @Override + protected IType createAsyncBinaryReturnType() { + return null; + } + + @Override + protected IType createAsyncBodyReturnType(IType restAPIMethodReturnBodyClientType) { + return GenericType.AndroidCompletableFuture(restAPIMethodReturnBodyClientType); + } + + @Override + protected IType createAsyncVoidReturnType() { + return GenericType.AndroidCompletableFuture(ClassType.VOID); + } + + @Override + protected IType createSyncReturnWithResponseType(IType syncReturnType, Operation operation, boolean isProtocolMethod, JavaSettings settings) { + return GenericType.AndroidResponse(syncReturnType); + } + + @Override + protected boolean shouldGeneratePagingMethods() { + return true; + } + + @Override + protected ReturnValue createSimpleSyncRestResponseReturnValue(Operation operation, IType syncReturnWithResponse, IType syncReturnType) { + return new ReturnValue(returnTypeDescription(operation, syncReturnWithResponse, syncReturnType), syncReturnWithResponse); + } + + @Override + protected ReturnValue createSimpleAsyncRestResponseReturnValue(Operation operation, IType asyncRestResponseReturnType, IType syncReturnType) { + IType asyncWithResponseType = GenericType.AndroidCompletableFuture(GenericType.AndroidResponse(syncReturnType)); + return new ReturnValue(returnTypeDescription(operation, asyncWithResponseType, syncReturnType), + asyncWithResponseType); + } + + @Override + protected ReturnValue createSimpleSyncReturnValue(Operation operation, IType syncReturnType) { + return new ReturnValue(returnTypeDescription(operation, syncReturnType, syncReturnType), syncReturnType); + } + + @Override + protected ReturnValue createSimpleAsyncReturnValue(Operation operation, IType asyncReturnType, IType syncReturnType) { + return new ReturnValue(returnTypeDescription(operation, asyncReturnType, syncReturnType), asyncReturnType); + } + +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidExceptionMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidExceptionMapper.java new file mode 100644 index 000000000..ea4cfc2cd --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidExceptionMapper.java @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.mapper.android; + +import com.microsoft.typespec.http.client.generator.core.mapper.ExceptionMapper; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientException; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.android.AndroidClientException; + +public class AndroidExceptionMapper extends ExceptionMapper { + private static final ExceptionMapper INSTANCE = new AndroidExceptionMapper(); + + protected AndroidExceptionMapper() { + } + + public static ExceptionMapper getInstance() { + return INSTANCE; + } + + @Override + protected ClientException.Builder createClientExceptionBuilder() { + return new AndroidClientException.Builder(); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidMapperFactory.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidMapperFactory.java new file mode 100644 index 000000000..c8004655b --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidMapperFactory.java @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.mapper.android; + +import com.microsoft.typespec.http.client.generator.core.mapper.ClientMethodMapper; +import com.microsoft.typespec.http.client.generator.core.mapper.DefaultMapperFactory; +import com.microsoft.typespec.http.client.generator.core.mapper.ExceptionMapper; +import com.microsoft.typespec.http.client.generator.core.mapper.MethodGroupMapper; +import com.microsoft.typespec.http.client.generator.core.mapper.ModelMapper; +import com.microsoft.typespec.http.client.generator.core.mapper.PrimitiveMapper; +import com.microsoft.typespec.http.client.generator.core.mapper.ProxyMethodMapper; +import com.microsoft.typespec.http.client.generator.core.mapper.ProxyParameterMapper; +import com.microsoft.typespec.http.client.generator.core.mapper.ServiceClientMapper; + +public class AndroidMapperFactory extends DefaultMapperFactory { + + @Override + public PrimitiveMapper getPrimitiveMapper() { return AndroidPrimitiveMapper.getInstance(); } + + @Override + public ClientMethodMapper getClientMethodMapper() { + return AndroidClientMethodMapper.getInstance(); + } + + @Override + public ProxyMethodMapper getProxyMethodMapper() { + return AndroidProxyMethodMapper.getInstance(); + } + + @Override + public ProxyParameterMapper getProxyParameterMapper() { + return AndroidProxyParameterMapper.getInstance(); + } + + @Override + public MethodGroupMapper getMethodGroupMapper() { + return AndroidMethodGroupMapper.getInstance(); + } + + @Override + public ServiceClientMapper getServiceClientMapper() { + return AndroidServiceClientMapper.getInstance(); + } + + @Override + public ModelMapper getModelMapper() { + return AndroidModelMapper.getInstance(); + } + + @Override + public ExceptionMapper getExceptionMapper() { + return AndroidExceptionMapper.getInstance(); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidMethodGroupMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidMethodGroupMapper.java new file mode 100644 index 000000000..739f4610b --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidMethodGroupMapper.java @@ -0,0 +1,31 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.mapper.android; + +import com.microsoft.typespec.http.client.generator.core.mapper.MethodGroupMapper; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodGroupClient; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Proxy; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.android.AndroidMethodGroupClient; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.android.AndroidProxy; + +public class AndroidMethodGroupMapper extends MethodGroupMapper { + private static final MethodGroupMapper INSTANCE = new AndroidMethodGroupMapper(); + + protected AndroidMethodGroupMapper() { + } + + public static MethodGroupMapper getInstance() { + return INSTANCE; + } + + @Override + protected Proxy.Builder createProxyBuilder() { + return new AndroidProxy.Builder(); + } + + @Override + protected MethodGroupClient.Builder createMethodGroupClientBuilder() { + return new AndroidMethodGroupClient.Builder(); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidModelMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidModelMapper.java new file mode 100644 index 000000000..e75cc9fdc --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidModelMapper.java @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.mapper.android; + +import com.microsoft.typespec.http.client.generator.core.mapper.ModelMapper; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientModel; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.android.AndroidClientModel; + +public class AndroidModelMapper extends ModelMapper { + private static final ModelMapper INSTANCE = new AndroidModelMapper(); + + protected AndroidModelMapper() { + } + + public static ModelMapper getInstance() { + return INSTANCE; + } + + @Override + protected ClientModel.Builder createModelBuilder() { + return new AndroidClientModel.Builder(); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidPrimitiveMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidPrimitiveMapper.java new file mode 100644 index 000000000..de309593e --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidPrimitiveMapper.java @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.mapper.android; + +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ByteArraySchema; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.DateTimeSchema; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.PrimitiveSchema; +import com.microsoft.typespec.http.client.generator.core.mapper.PrimitiveMapper; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ArrayType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; + +public class AndroidPrimitiveMapper extends PrimitiveMapper { + private static final AndroidPrimitiveMapper INSTANCE = new AndroidPrimitiveMapper(); + + protected AndroidPrimitiveMapper() { + } + + public static PrimitiveMapper getInstance() { + return INSTANCE; + } + + @Override + public IType map(PrimitiveSchema primaryType) { + IType baseResolved = super.map(primaryType); + if (primaryType == null || primaryType.getType() == null) { + return baseResolved; + } + switch (primaryType.getType()) { + case BYTE_ARRAY: + ByteArraySchema byteArraySchema = (ByteArraySchema) primaryType; + if (byteArraySchema.getFormat() == ByteArraySchema.Format.BASE_64_URL) { + return ClassType.ANDROID_BASE_64_URL; + } else { + return ArrayType.BYTE_ARRAY; + } + case DATE_TIME: + DateTimeSchema dateTimeSchema = (DateTimeSchema) primaryType; + if (dateTimeSchema.getFormat() == DateTimeSchema.Format.DATE_TIME_RFC_1123) { + return ClassType.ANDROID_DATE_TIME_RFC_1123; + } else { + return ClassType.ANDROID_DATE_TIME; + } + case DURATION: + return ClassType.ANDROID_DURATION; + case DATE: + return ClassType.ANDROID_LOCAL_DATE; + case UNIXTIME: + return ClassType.ANDROID_DATE_TIME; + default: + return baseResolved; + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidProxyMethodMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidProxyMethodMapper.java new file mode 100644 index 000000000..51df2a280 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidProxyMethodMapper.java @@ -0,0 +1,88 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.mapper.android; + +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.RequestParameterLocation; +import com.microsoft.typespec.http.client.generator.core.mapper.ProxyMethodMapper; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.GenericType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.PrimitiveType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethodParameter; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.android.AndroidProxyMethod; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class AndroidProxyMethodMapper extends ProxyMethodMapper { + private static final ProxyMethodMapper INSTANCE = new AndroidProxyMethodMapper(); + + protected AndroidProxyMethodMapper() { + } + + public static ProxyMethodMapper getInstance() { + return INSTANCE; + } + + @Override + protected ProxyMethod.Builder createProxyMethodBuilder() { + return new AndroidProxyMethod.Builder(); + } + + @Override + protected IType createBinaryContentAsyncReturnType() { + return PrimitiveType.VOID; + } + + @Override + protected IType createStreamContentAsyncReturnType() { + return PrimitiveType.VOID; + } + + @Override + protected IType createClientResponseAsyncReturnType(ClassType clientResponseClassType) { + return PrimitiveType.VOID; // return GenericType.CompletableFuture(clientResponseClassType); + } + + @Override + protected IType createSingleValueAsyncReturnType(IType singleValueType) { + return PrimitiveType.VOID; // return GenericType.CompletableFuture(singleValueType); + } + + @Override + protected ClassType getContextClass() { + return ClassType.ANDROID_CONTEXT; + } + + @Override + protected Map getDefaultHttpStatusCodeToExceptionTypeMapping() { + return new HashMap<>(); + } + + @Override + protected ClassType getHttpResponseExceptionType() { + return ClassType.ANDROID_HTTP_RESPONSE_EXCEPTION; + } + + @Override + protected void appendCallbackParameter(List parameters, IType responseBodyType) { + ProxyMethodParameter callbackParameter = new ProxyMethodParameter.Builder() + .description("The async callback associated with this operation.") + .wireType(GenericType.AndroidCallback(GenericType.AndroidResponse(responseBodyType))) + .clientType(GenericType.AndroidCallback(GenericType.AndroidResponse(responseBodyType))) + .name("callback") + .requestParameterLocation(RequestParameterLocation.NONE) + .requestParameterName("callback") + .alreadyEncoded(true) + .constant(false) + .required(false) + .nullable(false) + .fromClient(false) + .parameterReference("callback") + .build(); + parameters.add(callbackParameter); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidProxyParameterMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidProxyParameterMapper.java new file mode 100644 index 000000000..50cf39401 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidProxyParameterMapper.java @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.mapper.android; + +import com.microsoft.typespec.http.client.generator.core.mapper.ProxyParameterMapper; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethodParameter; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.android.AndroidProxyMethodParameter; + +public class AndroidProxyParameterMapper extends ProxyParameterMapper { + private static final ProxyParameterMapper INSTANCE = new AndroidProxyParameterMapper(); + + protected AndroidProxyParameterMapper() { + } + + public static ProxyParameterMapper getInstance() { + return INSTANCE; + } + + @Override + protected ProxyMethodParameter.Builder createProxyMethodParameterBuilder() { + return new AndroidProxyMethodParameter.Builder(); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidServiceClientMapper.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidServiceClientMapper.java new file mode 100644 index 000000000..057a95e99 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/AndroidServiceClientMapper.java @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.mapper.android; + +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.mapper.ServiceClientMapper; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodParameter; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Proxy; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClient; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClientProperty; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.android.AndroidProxy; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.android.AndroidServiceClient; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaVisibility; + +import java.util.List; + +public class AndroidServiceClientMapper extends ServiceClientMapper { + + private static final ServiceClientMapper INSTANCE = new AndroidServiceClientMapper(); + + protected AndroidServiceClientMapper() { + } + + public static ServiceClientMapper getInstance() { + return INSTANCE; + } + + @Override + protected ServiceClient.Builder createClientBuilder() { + return new AndroidServiceClient.Builder(); + } + + @Override + protected Proxy.Builder getProxyBuilder() { + return new AndroidProxy.Builder(); + } + + @Override + protected void addHttpPipelineProperty(List serviceClientProperties) { + serviceClientProperties.add( + new ServiceClientProperty("The HTTP pipeline to send requests through.", ClassType.ANDROID_HTTP_PIPELINE, + "httpPipeline", true, null)); + } + + @Override + protected void addSerializerAdapterProperty(List serviceClientProperties, + JavaSettings settings) { + serviceClientProperties.add(new ServiceClientProperty("The serializer to serialize an object into a string.", + ClassType.ANDROID_JACKSON_SERDER, "jacksonSerder", true, null, + settings.isFluent() ? JavaVisibility.PackagePrivate : JavaVisibility.Public)); + } + + @Override + protected IType getHttpPipelineClassType() { + return ClassType.ANDROID_HTTP_PIPELINE; + } + + @Override + protected ClientMethodParameter createSerializerAdapterParameter() { + return new ClientMethodParameter.Builder().description( + "The serializer to serialize an object into a string") + .finalParameter(false) + .wireType(ClassType.ANDROID_JACKSON_SERDER) + .name("jacksonSerder") + .required(true) + .constant(false) + .fromClient(true) + .defaultValue(null) + .annotations(new java.util.ArrayList<>()) + .build(); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/package-info.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/package-info.java new file mode 100644 index 000000000..62cfba47a --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/android/package-info.java @@ -0,0 +1,4 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.mapper.android; diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientException.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientException.java new file mode 100644 index 000000000..603aadbe1 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientException.java @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.model.clientmodel.android; + +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientException; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; + +public class AndroidClientException extends ClientException { + protected AndroidClientException(String packageKeyword, String name, String errorName, IType parentType) { + super(packageKeyword, name, errorName, parentType); + } + + public static class Builder extends ClientException.Builder { + + @Override + public ClientException build() { + if (parentType.equals(ClassType.HTTP_RESPONSE_EXCEPTION)) { + parentType = ClassType.ANDROID_HTTP_RESPONSE_EXCEPTION; + } + return new AndroidClientException(packageName, name, errorName, parentType); + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientMethod.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientMethod.java new file mode 100644 index 000000000..6984413c2 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientMethod.java @@ -0,0 +1,102 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.model.clientmodel.android; + +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodParameter; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ExternalDocumentation; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodPageDetails; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodPollingDetails; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodTransformationDetail; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethodParameter; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ReturnValue; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaVisibility; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class AndroidClientMethod extends ClientMethod { + + /** + * Create a new ClientMethod with the provided properties. + * + * @param description The description of this ClientMethod. + * @param returnValue The return value of this ClientMethod. + * @param name The name of this ClientMethod. + * @param parameters The parameters of this ClientMethod. + * @param onlyRequiredParameters Whether this ClientMethod has omitted optional parameters. + * @param type The type of this ClientMethod. + * @param proxyMethod The ProxyMethod that this ClientMethod eventually calls. + * @param validateExpressions The expressions (parameters and service client properties) that need to be validated + * in this ClientMethod. + * @param clientReference The reference to the service client. + * @param requiredNullableParameterExpressions The parameter expressions which are required. + * @param isGroupedParameterRequired The parameter that needs to transformed before pagination. + * @param groupedParameterTypeName The type name of groupedParameter. + * @param methodPageDetails The pagination information if this is a paged method. + * @param methodTransformationDetails The parameter transformations before calling ProxyMethod. + * @param externalDocumentation The external documentation. + * @param hasWithContextOverload Whether this ClientMethod has a withContext overload. + */ + protected AndroidClientMethod(String description, ReturnValue returnValue, String name, + List parameters, boolean onlyRequiredParameters, ClientMethodType type, + ProxyMethod proxyMethod, Map validateExpressions, String clientReference, + List requiredNullableParameterExpressions, boolean isGroupedParameterRequired, + String groupedParameterTypeName, MethodPageDetails methodPageDetails, + List methodTransformationDetails, JavaVisibility methodVisibility, + JavaVisibility methodVisibilityInWrapperClient, MethodPollingDetails methodPollingDetails, + ExternalDocumentation externalDocumentation, boolean hasWithContextOverload) { + super(description, returnValue, name, parameters, onlyRequiredParameters, type, proxyMethod, + validateExpressions, clientReference, requiredNullableParameterExpressions, isGroupedParameterRequired, + groupedParameterTypeName, methodPageDetails, methodTransformationDetails, methodVisibility, + methodVisibilityInWrapperClient, null, methodPollingDetails, externalDocumentation, null, + hasWithContextOverload); + } + + @Override + public void addImportsTo(Set imports, boolean includeImplementationImports, JavaSettings settings) { + getReturnValue().addImportsTo(imports, includeImplementationImports); + + imports.add("com.azure.android.core.rest.annotation.ServiceMethod"); + imports.add("com.azure.android.core.rest.annotation.ReturnType"); + imports.add("java.util.concurrent.ExecutionException"); + imports.add("java9.util.function.Function"); + + for (ClientMethodParameter parameter : getParameters()) { + parameter.addImportsTo(imports, includeImplementationImports); + } + + if (getMethodPageDetails() != null) { + imports.add("com.azure.android.core.rest.PagedResponseBase"); + } + + if (includeImplementationImports) { + getProxyMethod().addImportsTo(imports, includeImplementationImports, settings); + for (ProxyMethodParameter parameter : getProxyMethod().getParameters()) { + parameter.getClientType().addImportsTo(imports, true); + } + } + + if (imports.contains(ClassType.UNIX_TIME_DATE_TIME.getFullName())) { + imports.remove(ClassType.UNIX_TIME_DATE_TIME.getFullName()); + imports.add(ClassType.ANDROID_DATE_TIME.getFullName()); + } + } + + public static class Builder extends ClientMethod.Builder { + @Override + public ClientMethod build() { + return new AndroidClientMethod(description, returnValue, name, parameters, onlyRequiredParameters, type, + proxyMethod, validateExpressions, clientReference, requiredNullableParameterExpressions, + isGroupedParameterRequired, groupedParameterTypeName, methodPageDetails, methodTransformationDetails, + methodVisibility, methodVisibilityInWrapperClient, methodPollingDetails, externalDocumentation, + hasWithContextOverload); + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientModel.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientModel.java new file mode 100644 index 000000000..268ca7f86 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidClientModel.java @@ -0,0 +1,63 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.model.clientmodel.android; + +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientModel; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientModelProperty; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientModelPropertyReference; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; + +import java.util.List; +import java.util.Set; + +public class AndroidClientModel extends ClientModel { + protected AndroidClientModel(String packageKeyword, String name, List imports, String description, + boolean isPolymorphic, ClientModelProperty polymorphicDiscriminator, String polymorphicDiscriminatorName, + String serializedName, boolean needsFlatten, String parentModelName, List derivedModels, + String xmlName, String xmlNamespace, List properties, + List propertyReferences, IType modelType, boolean stronglyTypedHeader, + boolean usedInXml, Set serializationFormats) { + super(packageKeyword, name, imports, description, isPolymorphic, polymorphicDiscriminator, + polymorphicDiscriminatorName, serializedName, needsFlatten, parentModelName, derivedModels, xmlName, + xmlNamespace, properties, propertyReferences, modelType, stronglyTypedHeader, null, usedInXml, + serializationFormats, null); + } + + @Override + protected void addFluentAnnotationImport(Set imports) { + imports.add("com.azure.android.core.rest.annotation.Fluent"); + } + + @Override + protected void addImmutableAnnotationImport(Set imports) { + imports.add("com.azure.android.core.rest.annotation.Immutable"); + } + + @Override + protected void addJsonFlattenAnnotationImport(Set imports) { + imports.add("com.azure.android.core.serde.jackson.JsonFlatten"); + } + + @Override + public void addImportsTo(Set imports, JavaSettings settings) { + super.addImportsTo(imports, settings); + + if (imports.contains(ClassType.UNIX_TIME_DATE_TIME.getFullName())) { + imports.remove(ClassType.UNIX_TIME_DATE_TIME.getFullName()); + imports.add(ClassType.ANDROID_DATE_TIME.getFullName()); + } + } + + public static class Builder extends ClientModel.Builder { + @Override + public ClientModel build() { + return new AndroidClientModel(packageName, name, imports, description, isPolymorphic, + polymorphicDiscriminator, polymorphicDiscriminatorName, serializedName, needsFlatten, parentModelName, + derivedModels, xmlName, xmlNamespace, properties, propertyReferences, modelType, stronglyTypedHeader, + usedInXml, serializationFormats); + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidMethodGroupClient.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidMethodGroupClient.java new file mode 100644 index 000000000..e0e7e1989 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidMethodGroupClient.java @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.model.clientmodel.android; + +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodGroupClient; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Proxy; + +import java.util.List; + +public class AndroidMethodGroupClient extends MethodGroupClient { + protected AndroidMethodGroupClient(String packageKeyword, String className, String interfaceName, + List implementedInterfaces, Proxy proxy, String serviceClientName, String variableType, + String variableName, List clientMethods, List supportedInterfaces, String classBaseName) { + super(packageKeyword, className, interfaceName, implementedInterfaces, proxy, serviceClientName, variableType, + variableName, clientMethods, supportedInterfaces, classBaseName != null + ? classBaseName + : (className.endsWith("Impl") ? className.substring(0, className.length() - 4) : className), null); + } + + @Override + protected ClassType getProxyClassType() { + return ClassType.ANDROID_REST_PROXY; + } + + public static class Builder extends MethodGroupClient.Builder { + @Override + public MethodGroupClient build() { + return new AndroidMethodGroupClient(packageName, className, interfaceName, implementedInterfaces, proxy, + serviceClientName, variableType, variableName, clientMethods, supportedInterfaces, classBaseName); + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxy.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxy.java new file mode 100644 index 000000000..b10fcb5ed --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxy.java @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.model.clientmodel.android; + +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Proxy; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethod; + +import java.util.List; +import java.util.Set; + +public class AndroidProxy extends Proxy { + protected AndroidProxy(String name, String clientTypeName, String baseURL, List methods) { + super(name, clientTypeName, baseURL, methods); + } + + @Override + public void addImportsTo(Set imports, boolean includeImplementationImports, JavaSettings settings) { + if (includeImplementationImports) { + imports.add("com.azure.android.core.rest.annotation.Host"); + imports.add("com.azure.android.core.rest.annotation.ServiceInterface"); + } + + for (ProxyMethod method : getMethods()) { + method.addImportsTo(imports, includeImplementationImports, settings); + } + } + + public static final class Builder extends Proxy.Builder { + @Override + public Proxy build() { + return new AndroidProxy(name, clientTypeName, baseURL, methods); + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxyMethod.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxyMethod.java new file mode 100644 index 000000000..d2217f718 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxyMethod.java @@ -0,0 +1,140 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.model.clientmodel.android; + +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethodExample; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethodParameter; +import com.azure.core.http.HttpMethod; +import com.microsoft.typespec.http.client.generator.core.util.CodeNamer; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class AndroidProxyMethod extends ProxyMethod { + + /** + * Create a new RestAPIMethod with the provided properties. + * + * @param requestContentType The Content-Type of the request. + * @param returnType The type of value that is returned from this method. + * @param httpMethod The HTTP method that will be used for this method. + * @param urlPath The path of this method's request URL. + * @param responseExpectedStatusCodes The status codes that are expected in the response. + * @param returnValueWireType The return value's type as it is received from the network (across the wire). + * @param unexpectedResponseExceptionType The exception type to throw if this method receives and unexpected + * response status code. + * @param name The name of this REST API method. + * @param parameters The parameters that are provided to this method. + * @param description The description of this method. + * @param isResumable Whether this method is resumable. + * @param responseContentTypes The media-types in response. + */ + protected AndroidProxyMethod(String requestContentType, IType returnType, HttpMethod httpMethod, String baseUrl, + String urlPath, List responseExpectedStatusCodes, ClassType unexpectedResponseExceptionType, + Map> unexpectedResponseExceptionTypes, String name, + List parameters, String description, IType returnValueWireType, IType responseBodyType, + boolean isResumable, Set responseContentTypes, String operationId, + Map examples, List specialHeaders) { + super(requestContentType, returnType, httpMethod, baseUrl, urlPath, responseExpectedStatusCodes, + unexpectedResponseExceptionType, unexpectedResponseExceptionTypes, name, parameters, parameters, + description, returnValueWireType, responseBodyType, responseBodyType, isResumable, responseContentTypes, + operationId, examples, specialHeaders); + } + + @Override + public void addImportsTo(Set imports, boolean includeImplementationImports, JavaSettings settings) { + + if (includeImplementationImports) { + if (getUnexpectedResponseExceptionType() != null) { + imports.add("com.azure.android.core.rest.annotation.UnexpectedResponseExceptionTypes"); + imports.add("com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType"); + getUnexpectedResponseExceptionType().addImportsTo(imports, includeImplementationImports); + } + if (getUnexpectedResponseExceptionTypes() != null) { + imports.add("com.azure.android.core.rest.annotation.UnexpectedResponseExceptionTypes"); + getUnexpectedResponseExceptionTypes().keySet() + .forEach(e -> e.addImportsTo(imports, includeImplementationImports)); + } + if (isResumable()) { + imports.add("com.azure.android.core.rest.annotation.ResumeOperation"); + } + imports.add(String.format("com.azure.android.core.rest.annotation.%1$s", + CodeNamer.toPascalCase(getHttpMethod().toString().toLowerCase()))); + + if (settings.isFluent()) { + imports.add("com.azure.android.core.rest.annotation.Headers"); + } + + if (getReturnValueWireType() != null) { + imports.add("com.azure.android.core.rest.annotation.ReturnValueWireType"); + imports.add("com.azure.android.core.rest.annotation.UnexpectedResponseExceptionTypes"); + imports.add("com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType"); + getUnexpectedResponseExceptionType().addImportsTo(imports, includeImplementationImports); + } + if (getUnexpectedResponseExceptionTypes() != null) { + imports.add("com.azure.android.core.rest.annotation.UnexpectedResponseExceptionTypes"); + imports.add("com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType"); + getUnexpectedResponseExceptionTypes().keySet() + .forEach(e -> e.addImportsTo(imports, includeImplementationImports)); + } + if (isResumable()) { + imports.add("com.azure.android.core.rest.annotation.ResumeOperation"); + } + imports.add(String.format("com.azure.android.core.rest.annotation.%1$s", + CodeNamer.toPascalCase(getHttpMethod().toString().toLowerCase()))); + + if (settings.isFluent()) { + imports.add("com.azure.android.core.rest.annotation.Headers"); + } + imports.add("com.azure.android.core.rest.annotation.ExpectedResponses"); + imports.add("com.azure.android.core.rest.PagedResponse"); + + if (getReturnValueWireType() != null) { + imports.add("com.azure.android.core.rest.annotation.ReturnValueWireType"); + returnValueWireType.addImportsTo(imports, includeImplementationImports); + } + + returnType.addImportsTo(imports, includeImplementationImports); + + for (ProxyMethodParameter parameter : parameters) { + parameter.addImportsTo(imports, includeImplementationImports, settings); + } + + if (imports.contains(ClassType.UNIX_TIME_DATE_TIME.getFullName())) { + imports.remove(ClassType.UNIX_TIME_DATE_TIME.getFullName()); + imports.add(ClassType.ANDROID_DATE_TIME.getFullName()); + } + + if (imports.contains(ClassType.BASE_64_URL.getFullName())) { + imports.remove(ClassType.BASE_64_URL.getFullName()); + imports.add(ClassType.ANDROID_BASE_64_URL.getFullName()); + } + + if (imports.contains(ClassType.DATE_TIME_RFC_1123.getFullName())) { + imports.remove(ClassType.DATE_TIME_RFC_1123.getFullName()); + imports.add(ClassType.ANDROID_DATE_TIME_RFC_1123.getFullName()); + } + } + } + + public static class Builder extends ProxyMethod.Builder { + + @Override + public ProxyMethod build() { + if (unexpectedResponseExceptionTypes != null && unexpectedResponseExceptionTypes.containsKey( + unexpectedResponseExceptionType)) { + unexpectedResponseExceptionType = null; + } + return new AndroidProxyMethod(requestContentType, returnType, httpMethod, baseUrl, urlPath, + responseExpectedStatusCodes, unexpectedResponseExceptionType, unexpectedResponseExceptionTypes, name, + parameters, description, returnValueWireType, responseBodyType, isResumable, responseContentTypes, + operationId, examples, specialHeaders); + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxyMethodParameter.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxyMethodParameter.java new file mode 100644 index 000000000..1f7373f10 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidProxyMethodParameter.java @@ -0,0 +1,72 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.model.clientmodel.android; + +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.RequestParameterLocation; +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ArrayType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ParameterSynthesizedOrigin; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethodParameter; +import com.microsoft.typespec.http.client.generator.core.util.CodeNamer; +import com.azure.core.util.serializer.CollectionFormat; + +import java.util.Set; + +public class AndroidProxyMethodParameter extends ProxyMethodParameter { + + /** + * Create a new RestAPIParameter based on the provided properties. + * + * @param description The description of this parameter. + * @param wireType The type of this parameter. + * @param clientType The type of this parameter users interact with. + * @param name The name of this parameter when it is used as a variable. + * @param requestParameterLocation The location within the REST API method's HttpRequest where this parameter will + * be added. + * @param requestParameterName The name of the HttpRequest's parameter to substitute with this parameter's value. + * @param alreadyEncoded Whether the value of this parameter will already be encoded (and can therefore be skipped + * when other parameters' values are being encoded. + * @param isConstant Whether this parameter is a constant value. + * @param isRequired Whether this parameter is required. + * @param isNullable Whether this parameter is nullable. + * @param fromClient Whether this parameter's value comes from a ServiceClientProperty. + * @param headerCollectionPrefix The x-ms-header-collection-prefix extension value. + * @param parameterReference The reference to this parameter from a caller. + * @param defaultValue The default value of the parameter. + * @param collectionFormat The collection format if the parameter is a list type. + */ + protected AndroidProxyMethodParameter(String description, IType wireType, IType clientType, String name, + RequestParameterLocation requestParameterLocation, String requestParameterName, boolean alreadyEncoded, + boolean isConstant, boolean isRequired, boolean isNullable, boolean fromClient, String headerCollectionPrefix, + String parameterReference, String defaultValue, CollectionFormat collectionFormat, boolean explode) { + super(description, wireType, wireType, clientType, name, requestParameterLocation, requestParameterName, + alreadyEncoded, isConstant, isRequired, isNullable, fromClient, headerCollectionPrefix, parameterReference, + defaultValue, collectionFormat, explode, ParameterSynthesizedOrigin.NONE); + } + + @Override + public void addImportsTo(Set imports, boolean includeImplementationImports, JavaSettings settings) { + if (getRequestParameterLocation() != RequestParameterLocation.NONE) { + imports.add(String.format("com.azure.android.core.rest.annotation.%1$sParam", + CodeNamer.toPascalCase(getRequestParameterLocation().toString()))); + } + if (getRequestParameterLocation() != RequestParameterLocation.BODY) { + if (getClientType() == ArrayType.BYTE_ARRAY) { + imports.add("com.azure.android.core.util.Base64Util"); + } + } + + getWireType().addImportsTo(imports, includeImplementationImports); + } + + public static class Builder extends ProxyMethodParameter.Builder { + @Override + public ProxyMethodParameter build() { + return new AndroidProxyMethodParameter(description, wireType, clientType, name, requestParameterLocation, + requestParameterName, alreadyEncoded, isConstant, isRequired, isNullable, fromClient, + headerCollectionPrefix, parameterReference, defaultValue, collectionFormat, explode); + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidServiceClient.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidServiceClient.java new file mode 100644 index 000000000..1020f779e --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/AndroidServiceClient.java @@ -0,0 +1,59 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.model.clientmodel.android; + +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodParameter; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Constructor; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodGroupClient; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Proxy; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.SecurityInfo; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClient; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClientProperty; + +import java.util.List; +import java.util.Set; + +public class AndroidServiceClient extends ServiceClient { + + protected AndroidServiceClient(String packageName, String className, String interfaceName, Proxy proxy, + List methodGroupClients, List properties, + List constructors, List clientMethods, + ClientMethodParameter azureEnvironmentParameter, ClientMethodParameter tokenCredentialParameter, + ClientMethodParameter httpPipelineParameter, ClientMethodParameter serializerAdapterParameter, + ClientMethodParameter defaultPollIntervalParameter, String defaultCredentialScopes, boolean builderDisabled, + SecurityInfo securityInfo, String baseUrl) { + super(packageName, className, interfaceName, proxy, methodGroupClients, properties, constructors, clientMethods, + azureEnvironmentParameter, tokenCredentialParameter, httpPipelineParameter, serializerAdapterParameter, + defaultPollIntervalParameter, defaultCredentialScopes, builderDisabled, null, securityInfo, baseUrl, null, + null); + } + + @Override + protected void addRestProxyImport(Set imports) { + imports.add("com.azure.android.core.rest.RestProxy"); + } + + @Override + protected void addPipelineBuilderImport(Set imports) { + imports.add("com.azure.android.core.http.HttpPipelineBuilder"); + } + + @Override + protected void addHttpPolicyImports(Set imports) { + imports.add("com.azure.android.core.http.policy.CookiePolicy"); + imports.add("com.azure.android.core.http.policy.RetryPolicy"); + imports.add("com.azure.android.core.http.policy.UserAgentPolicy"); + } + + public static class Builder extends ServiceClient.Builder { + @Override + public ServiceClient build() { + return new AndroidServiceClient(packageName, className, interfaceName, proxy, methodGroupClients, + properties, constructors, clientMethods, azureEnvironmentParameter, tokenCredentialParameter, + httpPipelineParameter, serializerAdapterParameter, defaultPollIntervalParameter, + defaultCredentialScopes, builderDisabled, securityInfo, baseUrl); + } + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/package-info.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/package-info.java new file mode 100644 index 000000000..c18059907 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/android/package-info.java @@ -0,0 +1,3 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.microsoft.typespec.http.client.generator.core.model.clientmodel.android; diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidClientMethodTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidClientMethodTemplate.java new file mode 100644 index 000000000..8b779847d --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidClientMethodTemplate.java @@ -0,0 +1,216 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.GenericType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.PrimitiveType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethodParameter; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaBlock; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaType; +import com.microsoft.typespec.http.client.generator.core.template.ClientMethodTemplate; + +public class AndroidClientMethodTemplate extends ClientMethodTemplate { + + private static final ClientMethodTemplate INSTANCE = new AndroidClientMethodTemplate(); + + protected AndroidClientMethodTemplate() { + } + + public static ClientMethodTemplate getInstance() { + return INSTANCE; + } + + @Override + protected IType getContextType() { + return ClassType.ANDROID_CONTEXT; + } + + @Override + protected void generatePagingSync(ClientMethod clientMethod, JavaType typeBlock, ProxyMethod restAPIMethod, + JavaSettings settings) { + } + + @Override + protected void generatePagingAsync(ClientMethod clientMethod, JavaType typeBlock, ProxyMethod restAPIMethod, + JavaSettings settings) { + } + + @Override + protected void generatePagedAsyncSinglePage(ClientMethod clientMethod, JavaType typeBlock, + ProxyMethod restAPIMethod, JavaSettings settings) { + typeBlock.annotation("ServiceMethod(returns = ReturnType.SINGLE)"); + writeMethod(typeBlock, clientMethod.getMethodVisibility(), clientMethod.getDeclaration(), function -> { + addValidations(function, clientMethod.getRequiredNullableParameterExpressions(), + clientMethod.getValidateExpressions(), settings); + addOptionalAndConstantVariables(function, clientMethod, restAPIMethod.getParameters(), settings); + applyParameterTransformations(function, clientMethod, settings); + //ConvertClientTypesToWireTypes(function, clientMethod, restAPIMethod.getParameters(), clientMethod.getClientReference(), settings); + + if (clientMethod.getMethodPageDetails().nonNullNextLink()) { + final String completeFutureVariableName = "completableFuture"; + declarePagedCompletableFuture(function, clientMethod, restAPIMethod); + + String serviceMethodCall = generateProxyMethodCall(clientMethod, restAPIMethod, settings); + function.line(serviceMethodCall); + function.methodReturn(completeFutureVariableName); + } else { + // REVISIT: Is there a use case for this? + } + }); + } + + private void declarePagedCompletableFuture(JavaBlock function, ClientMethod clientMethod, + ProxyMethod restAPIMethod) { + ProxyMethodParameter callbackParam = restAPIMethod.getParameters() + .stream() + .filter(param -> param.getName().equals("callback")) + .findFirst() + .get(); + GenericType callbackResponseType + = (GenericType) ((GenericType) callbackParam.getClientType()).getTypeArguments()[0]; + IType callbackDataType = callbackResponseType.getTypeArguments()[0]; + + GenericType clientReturnGenericType = (GenericType) clientMethod.getReturnValue().getType().getClientType(); + GenericType responseType = (GenericType) clientReturnGenericType.getTypeArguments()[0]; + IType modelType = responseType.getTypeArguments()[0]; + + function.line("PagedResponseCompletableFuture<%1$s, %2$s> completableFuture =%n", callbackDataType, modelType); + function.indent(() -> { + function.line("new PagedResponseCompletableFuture<>(response ->"); + function.indent(() -> function.line( + "new PagedResponseBase<>(response.getRequest(), response.getStatusCode(), " + + "response.getHeaders(), response.getValue().getValue(), response.getValue().getNextLink(), null),")); + function.line(");"); + }); + } + + @Override + protected void generateResumable(ClientMethod clientMethod, JavaType typeBlock, ProxyMethod restAPIMethod, + JavaSettings settings) { + } + + @Override + protected void generateSimpleAsyncRestResponse(ClientMethod clientMethod, JavaType typeBlock, + ProxyMethod restAPIMethod, JavaSettings settings) { + typeBlock.annotation("ServiceMethod(returns = ReturnType.SINGLE)"); + writeMethod(typeBlock, clientMethod.getMethodVisibility(), clientMethod.getDeclaration(), function -> { + addValidations(function, clientMethod.getRequiredNullableParameterExpressions(), + clientMethod.getValidateExpressions(), settings); + addOptionalAndConstantVariables(function, clientMethod, restAPIMethod.getParameters(), settings); + applyParameterTransformations(function, clientMethod, settings); + // REVISIT: Restore this call for Android + // ConvertClientTypesToWireTypes(function, clientMethod, restAPIMethod.getParameters(), clientMethod.getClientReference(), settings); + + final String completeFutureVariableName = "completableFuture"; + function.line(declareResponseCompletableFuture(clientMethod)); + + String serviceMethodCall = generateProxyMethodCall(clientMethod, restAPIMethod, settings); + function.line(serviceMethodCall); + function.methodReturn(completeFutureVariableName); + }); + } + + private String declareResponseCompletableFuture(ClientMethod clientMethod) { + GenericType clientReturnGenericType = (GenericType) clientMethod.getReturnValue().getType().getClientType(); + GenericType responseType = (GenericType) clientReturnGenericType.getTypeArguments()[0]; + IType modelType = responseType.getTypeArguments()[0]; + if (modelType.equals(PrimitiveType.VOID)) { + modelType = ClassType.VOID; + } else if (modelType.equals(PrimitiveType.BOOLEAN)) { + modelType = ClassType.BOOLEAN; + } else if (modelType.equals(PrimitiveType.DOUBLE)) { + modelType = ClassType.DOUBLE; + } else if (modelType.equals(PrimitiveType.FLOAT)) { + modelType = ClassType.FLOAT; + } else if (modelType.equals(PrimitiveType.INT)) { + modelType = ClassType.INTEGER; + } else if (modelType.equals(PrimitiveType.LONG)) { + modelType = ClassType.LONG; + } + + return String.format("ResponseCompletableFuture<%1$s> %2$s = new ResponseCompletableFuture<>(); ", modelType, + "completableFuture"); + } + + private String generateProxyMethodCall(ClientMethod clientMethod, ProxyMethod restAPIMethod, + JavaSettings settings) { + java.util.List serviceMethodArgs = clientMethod.getProxyMethodArguments(settings) + .stream() + .map(argVal -> { + if (clientMethod.getParameters() + .stream() + .filter(param -> param.getName().equals(argVal)) + .anyMatch(param -> clientMethod.getMethodTransformationDetails() + .stream() + .anyMatch( + transformation -> param.getName().equals(transformation.getOutParameter().getName())))) { + return argVal + "Local"; + } + + if (!contextInParameters(clientMethod) && argVal.equals("context")) { + return "Context.NONE"; + } + + if (argVal.startsWith("callback")) { + return "completableFuture"; + } + return argVal; + }) + .collect(java.util.stream.Collectors.toList()); + String restAPIMethodArgumentList = String.join(", ", serviceMethodArgs); + return String.format("service.%s(%s);", restAPIMethod.getName(), restAPIMethodArgumentList); + } + + @Override + protected void generateSimpleAsync(ClientMethod clientMethod, JavaType typeBlock, ProxyMethod restAPIMethod, + JavaSettings settings) { + typeBlock.annotation("ServiceMethod(returns = ReturnType.SINGLE)"); + writeMethod(typeBlock, clientMethod.getMethodVisibility(), clientMethod.getDeclaration(), (function -> { + addOptionalVariables(function, clientMethod); + function.line("return %s(%s).thenApply(response -> response.getValue());", + clientMethod.getProxyMethod().getSimpleAsyncRestResponseMethodName(), clientMethod.getArgumentList()); + })); + } + + @Override + protected void generateSyncMethod(ClientMethod clientMethod, JavaType typeBlock, ProxyMethod restAPIMethod, + JavaSettings settings) { + String asyncMethodName = restAPIMethod.getSimpleAsyncMethodName(); + if (clientMethod.getType() == ClientMethodType.SimpleSyncRestResponse) { + asyncMethodName = restAPIMethod.getSimpleAsyncRestResponseMethodName(); + } + String effectiveAsyncMethodName = asyncMethodName; + typeBlock.annotation("ServiceMethod(returns = ReturnType.SINGLE)"); + typeBlock.publicMethod(clientMethod.getDeclaration(), function -> { + addOptionalVariables(function, clientMethod); + if (clientMethod.getReturnValue().getType() == ClassType.INPUT_STREAM) { + throw new UnsupportedOperationException( + "Return type 'ClassType.InputStream' not implemented for android"); + } else { + IType returnType = clientMethod.getReturnValue().getType(); + String proxyMethodCall = String.format("%s(%s).get()", effectiveAsyncMethodName, + clientMethod.getArgumentList()); + + function.line("try {"); + if (returnType != PrimitiveType.VOID) { + function.methodReturn(proxyMethodCall); + } else { + function.line("\t" + proxyMethodCall + ";"); + } + function.line("} catch (InterruptedException e) {"); + function.line("\tthrow new RuntimeException(e);"); + function.line("} catch (ExecutionException e) {"); + function.line("\tthrow new RuntimeException(e);"); + function.line("}"); + } + }); + } + +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidEnumTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidEnumTemplate.java new file mode 100644 index 000000000..9e73caea6 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidEnumTemplate.java @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaContext; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaEnum; +import com.microsoft.typespec.http.client.generator.core.template.EnumTemplate; + +import java.util.Set; + +public class AndroidEnumTemplate extends EnumTemplate { + + private static final EnumTemplate INSTANCE = new AndroidEnumTemplate(); + + private AndroidEnumTemplate() { + } + + public static EnumTemplate getInstance() { + return INSTANCE; + } + + @Override + protected String getStringEnumImport() { + return "com.azure.android.core.util.ExpandableStringEnum"; + } + + @Override + protected void addGeneratedImport(Set imports) { + } + + @Override + protected void addGeneratedAnnotation(JavaContext context) { + } + + @Override + protected void addGeneratedAnnotation(JavaEnum context) { + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidExceptionTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidExceptionTemplate.java new file mode 100644 index 000000000..f7ea2a93d --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidExceptionTemplate.java @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.template.ExceptionTemplate; + +public class AndroidExceptionTemplate extends ExceptionTemplate { + private static final ExceptionTemplate INSTANCE = new AndroidExceptionTemplate(); + + protected AndroidExceptionTemplate() { + } + + public static ExceptionTemplate getInstance() { + return INSTANCE; + } + + @Override + protected String getHttpResponseImport() { + return "com.azure.android.core.http.HttpResponse"; + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidMethodGroupTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidMethodGroupTemplate.java new file mode 100644 index 000000000..e9051a744 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidMethodGroupTemplate.java @@ -0,0 +1,66 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodGroupClient; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaBlock; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaClass; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaVisibility; +import com.microsoft.typespec.http.client.generator.core.template.MethodGroupTemplate; + +public class AndroidMethodGroupTemplate extends MethodGroupTemplate { + private static final MethodGroupTemplate INSTANCE = new AndroidMethodGroupTemplate(); + + protected AndroidMethodGroupTemplate() { + } + + public static MethodGroupTemplate getInstance() { + return INSTANCE; + } + + @Override + protected void writeServiceProxyConstruction(JavaBlock constructor, MethodGroupClient methodGroupClient) { + ClassType proxyType = ClassType.ANDROID_REST_PROXY; + constructor.line(String.format( + "this.service = %1$s.create(%2$s.class, client.getHttpPipeline(), client.getJacksonSerder());", + proxyType.getName(), methodGroupClient.getProxy().getName())); + } + + @Override + protected void writeAdditionalClassBlock(JavaClass classBlock) { + classBlock.privateStaticFinalClass( + "ResponseCompletableFuture extends CompletableFuture> implements Callback>", + embeddedClass -> { + embeddedClass.annotation("Override"); + embeddedClass.publicMethod("void onSuccess(Response response)", method -> { + method.line("this.complete(response);"); + }); + + embeddedClass.annotation("Override"); + embeddedClass.publicMethod("void onFailure(Throwable error)", method -> { + method.line("this.completeExceptionally(error);"); + }); + }); + + classBlock.privateStaticFinalClass( + "PagedResponseCompletableFuture extends CompletableFuture> implements Callback>", + embeddedClass -> { + embeddedClass.privateFinalMemberVariable("Function, PagedResponse>", "converter"); + embeddedClass.constructor(JavaVisibility.PackagePrivate, + "PagedResponseCompletableFuture(Function, PagedResponse> converter)", + constructorBody -> { + constructorBody.line("this.converter = converter;"); + }); + embeddedClass.annotation("Override"); + embeddedClass.publicMethod("void onSuccess(Response

response)", + method -> method.line("this.complete(this.converter.apply(response));")); + + embeddedClass.annotation("Override"); + embeddedClass.publicMethod("void onFailure(Throwable error)", + method -> method.line("this.completeExceptionally(error);")); + }); + } +} + diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidProxyTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidProxyTemplate.java new file mode 100644 index 000000000..7e21a826f --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidProxyTemplate.java @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethod; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaInterface; +import com.microsoft.typespec.http.client.generator.core.template.ProxyTemplate; + +import java.util.List; + +public class AndroidProxyTemplate extends ProxyTemplate { + private static final ProxyTemplate INSTANCE = new AndroidProxyTemplate(); + + protected AndroidProxyTemplate() { + } + + public static ProxyTemplate getInstance() { + return INSTANCE; + } + + @Override + protected void writeUnexpectedExceptions(ProxyMethod restAPIMethod, JavaInterface interfaceBlock) { + StringBuilder annotationBuilder = new StringBuilder(); + annotationBuilder.append(String.format("UnexpectedResponseExceptionTypes({%n")); + for (java.util.Map.Entry> exception : restAPIMethod.getUnexpectedResponseExceptionTypes().entrySet()) { + annotationBuilder.append(String.format("\t@UnexpectedResponseExceptionType(value = %1$s.class, code = {%2$s})%n", + exception.getKey(), exception.getValue().stream().map(String::valueOf).collect(java.util.stream.Collectors.joining(", ")))); + } + annotationBuilder.append(String.format("})%n")); + interfaceBlock.annotation(annotationBuilder.toString()); + } + + @Override + protected void writeSingleUnexpectedException(ProxyMethod restAPIMethod, JavaInterface interfaceBlock) { + // REVISIT: For some reason this can be called even when writeUnexpectedExceptions is called already + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidResponseTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidResponseTemplate.java new file mode 100644 index 000000000..a622c8644 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidResponseTemplate.java @@ -0,0 +1,31 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientResponse; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.GenericType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; +import com.microsoft.typespec.http.client.generator.core.template.ResponseTemplate; + +public class AndroidResponseTemplate extends ResponseTemplate { + private static final AndroidResponseTemplate INSTANCE = new AndroidResponseTemplate(); + + protected AndroidResponseTemplate() { + } + + public static ResponseTemplate getInstance() { + return INSTANCE; + } + + @Override + protected IType getRestResponseType(ClientResponse response) { + return new GenericType("com.azure.android.core.rest", "ResponseBase", response.getHeadersType(), response.getBodyType()); + } + + @Override + protected void addRequestAndHeaderImports(java.util.Set imports) { + imports.add("com.azure.android.core.http.HttpRequest"); + imports.add("com.azure.android.core.http.HttpHeaders"); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceAsyncClientTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceAsyncClientTemplate.java new file mode 100644 index 000000000..eb48d6f32 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceAsyncClientTemplate.java @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaContext; +import com.microsoft.typespec.http.client.generator.core.template.ServiceAsyncClientTemplate; + +public class AndroidServiceAsyncClientTemplate extends ServiceAsyncClientTemplate { + private static final AndroidServiceAsyncClientTemplate INSTANCE = new AndroidServiceAsyncClientTemplate(); + + protected AndroidServiceAsyncClientTemplate() { + } + + public static ServiceAsyncClientTemplate getInstance() { + return INSTANCE; + } + + @Override + protected void addServiceClientAnnotationImports(java.util.Set imports) { + imports.add("com.azure.android.core.rest.annotation.ServiceClient"); + } + + @Override + protected void addGeneratedAnnotation(JavaContext classBlock) { + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceClientBuilderTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceClientBuilderTemplate.java new file mode 100644 index 000000000..95c55b5a9 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceClientBuilderTemplate.java @@ -0,0 +1,130 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.AsyncSyncClient; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ListType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.PipelinePolicyDetails; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.SecurityInfo; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClientProperty; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaBlock; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaClass; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaContext; +import com.microsoft.typespec.http.client.generator.core.template.ServiceClientBuilderTemplate; + +import java.util.ArrayList; +import java.util.Set; + +public class AndroidServiceClientBuilderTemplate extends ServiceClientBuilderTemplate { + private static final ServiceClientBuilderTemplate INSTANCE = new AndroidServiceClientBuilderTemplate(); + + protected AndroidServiceClientBuilderTemplate() { + } + + public static ServiceClientBuilderTemplate getInstance() { + return INSTANCE; + } + + @Override + protected void writeSyncClientBuildMethod(AsyncSyncClient syncClient, AsyncSyncClient asyncClient, + JavaBlock function, String buildMethodName, boolean wrapServiceClient) { + writeSyncClientBuildMethodFromInnerClient(syncClient, function, buildMethodName, wrapServiceClient); + } + + @Override + protected String getSerializerMemberName() { + return "JacksonSerder.createDefault()"; + } + + @Override + protected ArrayList addCommonClientProperties(JavaSettings settings, + SecurityInfo securityInfo) { + ArrayList commonProperties = new ArrayList<>(); + + commonProperties.add( + new ServiceClientProperty("The HTTP pipeline to send requests through", ClassType.ANDROID_HTTP_PIPELINE, + "pipeline", false, "createHttpPipeline()")); + + commonProperties.add( + new ServiceClientProperty("The HTTP client used to send the request.", ClassType.ANDROID_HTTP_CLIENT, + "httpClient", false, null)); + + commonProperties.add( + new ServiceClientProperty("The logging configuration for HTTP requests and " + "responses.", + ClassType.ANDROID_HTTP_LOG_OPTIONS, "httpLogOptions", false, null)); + commonProperties.add(new ServiceClientProperty( + "The retry policy that will attempt to retry failed " + "requests, if applicable.", + ClassType.ANDROID_RETRY_POLICY, "retryPolicy", false, null)); + + commonProperties.add(new ServiceClientProperty("The list of Http pipeline policies to add.", + new ListType(ClassType.ANDROID_HTTP_PIPELINE_POLICY), "pipelinePolicies", true, null)); + + return commonProperties; + } + + @Override + protected void addServiceClientBuilderAnnotationImport(Set imports) { + imports.add("com.azure.android.core.rest.annotation.ServiceClientBuilder"); + } + + @Override + protected void addHttpPolicyImports(Set imports) { + imports.add("com.azure.android.core.http.policy.HttpLoggingPolicy"); + } + + @Override + protected void addImportForCoreUtils(Set imports) { + } + + @Override + protected void addSerializerImport(Set imports, JavaSettings settings) { + imports.add("com.azure.android.core.serde.jackson.JacksonSerder"); + } + + @Override + protected void addCreateHttpPipelineMethod(JavaSettings settings, + JavaClass classBlock, String defaultCredentialScopes, + SecurityInfo securityInfo, PipelinePolicyDetails pipelinePolicyDetails) { + classBlock.privateMethod("HttpPipeline createHttpPipeline()", function -> { + + function.ifBlock("httpLogOptions == null", + action -> action.line("httpLogOptions = new HttpLogOptions();")); + + function.line("List policies = new ArrayList<>();"); + + function.line("String clientName = properties.get(SDK_NAME);"); + function.line("if (clientName == null) {"); + function.increaseIndent(); + function.line("clientName = \"UnknownName\";"); + function.decreaseIndent(); + function.line("}"); + function.line("String clientVersion = properties.get(SDK_VERSION);"); + function.line("if (clientVersion == null) {"); + function.increaseIndent(); + function.line("clientVersion = \"UnknownVersion\";"); + function.decreaseIndent(); + function.line("}"); + + function.line("policies.add(new UserAgentPolicy(null, clientName, clientVersion));"); + function.line("policies.add(retryPolicy == null ? RetryPolicy.withExponentialBackoff() : retryPolicy);"); + function.line("policies.add(new CookiePolicy());"); + function.line("policies.addAll(this.pipelinePolicies);"); + function.line("policies.add(new HttpLoggingPolicy(httpLogOptions));"); + + function.line("HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new " + + "HttpPipelinePolicy[0])).httpClient(httpClient).build();"); + function.methodReturn("httpPipeline"); + }); + } + + @Override + protected void addGeneratedImport(Set imports) { + } + + @Override + protected void addGeneratedAnnotation(JavaContext classBlock) { + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceClientTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceClientTemplate.java new file mode 100644 index 000000000..a8bfb886f --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceClientTemplate.java @@ -0,0 +1,93 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaBlock; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaClass; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaVisibility; +import com.microsoft.typespec.http.client.generator.core.template.ServiceClientTemplate; + +import java.util.Set; + +public class AndroidServiceClientTemplate extends ServiceClientTemplate { + private static final ServiceClientTemplate INSTANCE = new AndroidServiceClientTemplate(); + + protected AndroidServiceClientTemplate() { + } + + public static ServiceClientTemplate getInstance() { + return INSTANCE; + } + + @Override + protected void addServiceClientAnnotationImport(Set imports) { + imports.add("com.azure.android.core.rest.annotation.ServiceClient"); + } + + @Override + protected void addSerializerImport(Set imports) { + imports.add("com.azure.android.core.serde.jackson.JacksonSerder"); + + imports.add("com.azure.android.core.rest.Callback"); + imports.add("com.azure.android.core.rest.Response"); + imports.add("com.azure.android.core.rest.PagedResponse"); + imports.add("java9.util.concurrent.CompletableFuture"); + imports.add("java9.util.function.Function"); + } + + @Override + protected String writeSerializerInitialization() { + return "JacksonSerder.createDefault()"; + } + + @Override + protected String writeRetryPolicyInitialization() { + return "RetryPolicy.withExponentialBackoff()"; + } + + @Override + protected void writeSerializerMemberInitialization(JavaBlock constructorBlock) { + constructorBlock.line("this.jacksonSerder = jacksonSerder;"); + } + + @Override + protected String getSerializerPhrase() { + return "this.jacksonSerder"; + } + + @Override + protected void writeAdditionalClassBlock(JavaClass classBlock) { + classBlock.privateStaticFinalClass( + "ResponseCompletableFuture extends CompletableFuture> implements Callback>", + embeddedClass -> { + embeddedClass.annotation("Override"); + embeddedClass.publicMethod("void onSuccess(Response response)", method -> { + method.line("this.complete(response);"); + }); + + embeddedClass.annotation("Override"); + embeddedClass.publicMethod("void onFailure(Throwable error)", method -> { + method.line("this.completeExceptionally(error);"); + }); + }); + + classBlock.privateStaticFinalClass( + "PagedResponseCompletableFuture extends CompletableFuture> implements Callback>", + embeddedClass -> { + embeddedClass.privateFinalMemberVariable("Function, PagedResponse>", "converter"); + embeddedClass.constructor(JavaVisibility.PackagePrivate, + "PagedResponseCompletableFuture(Function, PagedResponse> converter)", + constructorBody -> { + constructorBody.line("this.converter = converter;"); + }); + embeddedClass.annotation("Override"); + embeddedClass.publicMethod("void onSuccess(Response

response)", + method -> method.line("this.complete(this.converter.apply(response));")); + + embeddedClass.annotation("Override"); + embeddedClass.publicMethod("void onFailure(Throwable error)", + method -> method.line("this.completeExceptionally(error);")); + }); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceSyncClientTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceSyncClientTemplate.java new file mode 100644 index 000000000..67b50e2a5 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidServiceSyncClientTemplate.java @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaContext; +import com.microsoft.typespec.http.client.generator.core.template.ServiceSyncClientTemplate; + +public class AndroidServiceSyncClientTemplate extends ServiceSyncClientTemplate { + private static final AndroidServiceSyncClientTemplate INSTANCE = new AndroidServiceSyncClientTemplate(); + + protected AndroidServiceSyncClientTemplate() { + } + + public static ServiceSyncClientTemplate getInstance() { + return INSTANCE; + } + + protected void addServiceClientAnnotationImport(java.util.Set imports) { + imports.add("com.azure.android.core.rest.annotation.ServiceClient"); + } + + @Override + protected void addGeneratedAnnotation(JavaContext classBlock) { + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidTemplateFactory.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidTemplateFactory.java new file mode 100644 index 000000000..c62f040bc --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidTemplateFactory.java @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.template.ClientMethodTemplate; +import com.microsoft.typespec.http.client.generator.core.template.DefaultTemplateFactory; +import com.microsoft.typespec.http.client.generator.core.template.EnumTemplate; +import com.microsoft.typespec.http.client.generator.core.template.ExceptionTemplate; +import com.microsoft.typespec.http.client.generator.core.template.MethodGroupTemplate; +import com.microsoft.typespec.http.client.generator.core.template.ProxyTemplate; +import com.microsoft.typespec.http.client.generator.core.template.ResponseTemplate; +import com.microsoft.typespec.http.client.generator.core.template.ServiceAsyncClientTemplate; +import com.microsoft.typespec.http.client.generator.core.template.ServiceClientBuilderTemplate; +import com.microsoft.typespec.http.client.generator.core.template.ServiceClientTemplate; +import com.microsoft.typespec.http.client.generator.core.template.ServiceSyncClientTemplate; +import com.microsoft.typespec.http.client.generator.core.template.WrapperClientMethodTemplate; + +public class AndroidTemplateFactory extends DefaultTemplateFactory { + + @Override + public ProxyTemplate getProxyTemplate() { + return AndroidProxyTemplate.getInstance(); + } + + @Override + public ClientMethodTemplate getClientMethodTemplate() { + return AndroidClientMethodTemplate.getInstance(); + } + + @Override + public MethodGroupTemplate getMethodGroupTemplate() { + return AndroidMethodGroupTemplate.getInstance(); + } + + @Override + public ServiceClientTemplate getServiceClientTemplate() { + return AndroidServiceClientTemplate.getInstance(); + } + + @Override + public ServiceClientBuilderTemplate getServiceClientBuilderTemplate() { + return AndroidServiceClientBuilderTemplate.getInstance(); + } + + @Override + public EnumTemplate getEnumTemplate() { + return AndroidEnumTemplate.getInstance(); + } + + @Override + public ExceptionTemplate getExceptionTemplate() { + return AndroidExceptionTemplate.getInstance(); + } + + @Override + public ServiceAsyncClientTemplate getServiceAsyncClientTemplate() { + return AndroidServiceAsyncClientTemplate.getInstance(); + } + + @Override + public ServiceSyncClientTemplate getServiceSynClientTemplate() { + return AndroidServiceSyncClientTemplate.getInstance(); + } + + @Override + public ResponseTemplate getResponseTemplate() { + return AndroidResponseTemplate.getInstance(); + } + + @Override + public WrapperClientMethodTemplate getWrapperClientMethodTemplate() { + return AndroidWrapperClientMethodTemplate.getInstance(); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidWrapperClientMethodTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidWrapperClientMethodTemplate.java new file mode 100644 index 000000000..0caf639c8 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/AndroidWrapperClientMethodTemplate.java @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android; + +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaType; +import com.microsoft.typespec.http.client.generator.core.template.WrapperClientMethodTemplate; + +public class AndroidWrapperClientMethodTemplate extends WrapperClientMethodTemplate { + + private static final WrapperClientMethodTemplate INSTANCE = new AndroidWrapperClientMethodTemplate(); + + public static WrapperClientMethodTemplate getInstance() { + return INSTANCE; + } + + @Override + protected void addGeneratedAnnotation(JavaType typeBlock) { + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/package-info.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/package-info.java new file mode 100644 index 000000000..395aac607 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/android/package-info.java @@ -0,0 +1,4 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template.android;