diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/util/FluxUtil.java b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/util/FluxUtil.java index c13d5b9bee023..17dddcc6f1a1a 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/implementation/util/FluxUtil.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/implementation/util/FluxUtil.java @@ -3,6 +3,7 @@ package com.azure.core.implementation.util; +import com.azure.core.http.rest.Response; import com.azure.core.util.Context; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; @@ -199,6 +200,21 @@ public static Mono withContext(Function> serviceCall) { .flatMap(serviceCall); } + /** + * Converts the incoming content to Mono. + *

+ * If the response {@link Response#value() value} is empty, an empty Mono is returned. + *

+ *

Code samples

+ * {@codesnippet com.azure.core.implementation.util.fluxutil.toMono} + * + * @param response whose {@link Response#value() value} is to be converted + * @return The converted {@link Mono} + */ + public static Mono toMono(Response response) { + return Mono.justOrEmpty(response.value()); + } + /** * This method converts the incoming {@code subscriberContext} from {@link reactor.util.context.Context Reactor * Context} to {@link Context Azure Context} and calls the given lambda function with this context and returns a diff --git a/sdk/core/azure-core/src/samples/java/com/azure/core/implementation/util/FluxUtilJavaDocCodeSnippets.java b/sdk/core/azure-core/src/samples/java/com/azure/core/implementation/util/FluxUtilJavaDocCodeSnippets.java index d09272ddb377e..1379b5534a350 100644 --- a/sdk/core/azure-core/src/samples/java/com/azure/core/implementation/util/FluxUtilJavaDocCodeSnippets.java +++ b/sdk/core/azure-core/src/samples/java/com/azure/core/implementation/util/FluxUtilJavaDocCodeSnippets.java @@ -3,6 +3,9 @@ package com.azure.core.implementation.util; +import com.azure.core.http.HttpHeaders; +import com.azure.core.http.HttpRequest; +import com.azure.core.http.rest.Response; import com.azure.core.util.Context; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -34,6 +37,15 @@ public void codeSnippetForCallWithCollectionResponse() { // END: com.azure.core.implementation.util.fluxutil.fluxcontext } + /** + * Code snippet for using {@link FluxUtil} with collection response + */ + public void toMonoCodeSnippet() { + // BEGIN: com.azure.core.implementation.util.fluxutil.toMono + getMonoRestResponse("Hello").flatMap(FluxUtil::toMono); + // END: com.azure.core.implementation.util.fluxutil.toMono + } + /** * Implementation not provided * @param prefix The prefix @@ -54,4 +66,33 @@ private Mono serviceCallReturnsSingle(String prefix, Context context) { return Mono.empty(); } + /** + * Implementation not provided + * @param value The value + * @return A {@link Mono} containing a {@link Response} containing a {@link Response#value() value}. + */ + private Mono> getMonoRestResponse(T value) { + Response response = new Response() { + @Override + public int statusCode() { + return 200; + } + + @Override + public HttpHeaders headers() { + return null; + } + + @Override + public HttpRequest request() { + return null; + } + + @Override + public T value() { + return value; + } + }; + return Mono.just(response); + } } diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/implementation/util/FluxUtilTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/implementation/util/FluxUtilTests.java index 50a427fc25b54..3ebeb7544b147 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/implementation/util/FluxUtilTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/implementation/util/FluxUtilTests.java @@ -12,6 +12,7 @@ import com.azure.core.http.HttpRequest; import com.azure.core.http.rest.PagedFlux; import com.azure.core.http.rest.PagedResponse; +import com.azure.core.http.rest.Response; import com.azure.core.implementation.http.PagedResponseBase; import com.azure.core.util.Context; import io.netty.buffer.ByteBuf; @@ -278,6 +279,13 @@ public void testCollectByteBufStream() { 0, 0, 1, 0}, bytes); } + @Test + public void testToMono() { + String value = "test"; + Assert.assertEquals(getMonoRestResponse(value).flatMap(FluxUtil::toMono).block(), value); + Assert.assertEquals(getMonoRestResponse("").flatMap(FluxUtil::toMono).block(), ""); + } + @Test public void testCallWithContextGetSingle() { String response = getSingle("Hello, ") @@ -401,4 +409,29 @@ private File createFileIfNotExist(String fileName) throws IOException { return file; } + private Mono> getMonoRestResponse(T value) { + Response response = new Response() { + @Override + public int statusCode() { + return 200; + } + + @Override + public HttpHeaders headers() { + return null; + } + + @Override + public HttpRequest request() { + return null; + } + + @Override + public T value() { + return value; + } + }; + return Mono.just(response); + } + } diff --git a/sdk/keyvault/azure-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/KeyAsyncClient.java b/sdk/keyvault/azure-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/KeyAsyncClient.java index 615bdd82c1a5d..949797d7825cc 100644 --- a/sdk/keyvault/azure-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/KeyAsyncClient.java +++ b/sdk/keyvault/azure-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/KeyAsyncClient.java @@ -16,6 +16,7 @@ import com.azure.core.implementation.annotation.ReturnType; import com.azure.core.implementation.annotation.ServiceClient; import com.azure.core.implementation.annotation.ServiceMethod; +import com.azure.core.implementation.util.FluxUtil; import com.azure.core.util.Context; import com.azure.core.util.logging.ClientLogger; import com.azure.security.keyvault.keys.models.DeletedKey; @@ -97,7 +98,7 @@ public final class KeyAsyncClient { @ServiceMethod(returns = ReturnType.SINGLE) public Mono createKey(String name, KeyType keyType) { return withContext(context -> createKey(name, keyType, context)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } /** @@ -155,7 +156,7 @@ Mono> createKey(String name, KeyType keyType, Context context) { @ServiceMethod(returns = ReturnType.SINGLE) public Mono createKey(KeyCreateOptions keyCreateOptions) { return withContext(context -> createKey(keyCreateOptions, context)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } Mono> createKey(KeyCreateOptions keyCreateOptions, Context context) { @@ -196,7 +197,7 @@ Mono> createKey(KeyCreateOptions keyCreateOptions, Context context @ServiceMethod(returns = ReturnType.SINGLE) public Mono createRsaKey(RsaKeyCreateOptions rsaKeyCreateOptions) { return withContext(context -> createRsaKey(rsaKeyCreateOptions, context)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } /** @@ -262,7 +263,7 @@ Mono> createRsaKey(RsaKeyCreateOptions rsaKeyCreateOptions, Contex @ServiceMethod(returns = ReturnType.SINGLE) public Mono createEcKey(EcKeyCreateOptions ecKeyCreateOptions) { return withContext(context -> createEcKey(ecKeyCreateOptions, context)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } /** @@ -326,7 +327,7 @@ Mono> createEcKey(EcKeyCreateOptions ecKeyCreateOptions, Context c @ServiceMethod(returns = ReturnType.SINGLE) public Mono importKey(String name, JsonWebKey keyMaterial) { return withContext(context -> importKey(name, keyMaterial, context)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } Mono> importKey(String name, JsonWebKey keyMaterial, Context context) { @@ -366,7 +367,7 @@ Mono> importKey(String name, JsonWebKey keyMaterial, Context conte @ServiceMethod(returns = ReturnType.SINGLE) public Mono importKey(KeyImportOptions keyImportOptions) { return withContext(context -> importKey(keyImportOptions, context)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } /** @@ -433,7 +434,7 @@ public Mono getKey(String name, String version) { return getKey(name); } return withContext(context -> getKey(name, version, context)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } /** @@ -574,7 +575,7 @@ public Mono> getKeyWithResponse(KeyBase keyBase) { @ServiceMethod(returns = ReturnType.SINGLE) public Mono updateKey(KeyBase key) { return withContext(context -> updateKey(key, context)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } /** @@ -654,7 +655,7 @@ public Mono> updateKeyWithResponse(KeyBase key, KeyOperation... ke @ServiceMethod(returns = ReturnType.SINGLE) public Mono updateKey(KeyBase key, KeyOperation... keyOperations) { return withContext(context -> updateKey(key, context, keyOperations)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } Mono> updateKey(KeyBase key, Context context, KeyOperation... keyOperations) { @@ -690,7 +691,7 @@ Mono> updateKey(KeyBase key, Context context, KeyOperation... keyO @ServiceMethod(returns = ReturnType.SINGLE) public Mono deleteKey(String name) { return withContext(context -> deleteKey(name, context)) - .flatMap(deletedKeyResponse -> Mono.justOrEmpty(deletedKeyResponse.value())); + .flatMap(FluxUtil::toMono); } /** @@ -743,7 +744,7 @@ Mono> deleteKey(String name, Context context) { @ServiceMethod(returns = ReturnType.SINGLE) public Mono getDeletedKey(String name) { return withContext(context -> getDeletedKey(name, context)) - .flatMap(deletedKeyResponse -> Mono.justOrEmpty(deletedKeyResponse.value())); + .flatMap(FluxUtil::toMono); } /** @@ -822,7 +823,7 @@ Mono purgeDeletedKey(String name, Context context) { @ServiceMethod(returns = ReturnType.SINGLE) public Mono recoverDeletedKey(String name) { return withContext(context -> recoverDeletedKey(name, context)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } /** @@ -878,7 +879,7 @@ Mono> recoverDeletedKey(String name, Context context) { @ServiceMethod(returns = ReturnType.SINGLE) public Mono backupKey(String name) { return withContext(context -> backupKey(name, context)) - .flatMap(response -> Mono.justOrEmpty(response.value())); + .flatMap(FluxUtil::toMono); } /** @@ -940,7 +941,7 @@ Mono> backupKey(String name, Context context) { @ServiceMethod(returns = ReturnType.SINGLE) public Mono restoreKey(byte[] backup) { return withContext(context -> restoreKey(backup, context)) - .flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value())); + .flatMap(FluxUtil::toMono); } /**