Skip to content

Commit

Permalink
update helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity committed Jul 23, 2019
1 parent 9eb817d commit 424cca3
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -199,6 +200,21 @@ public static <T> Mono<T> withContext(Function<Context, Mono<T>> serviceCall) {
.flatMap(serviceCall);
}

/**
* Converts the incoming content to Mono.
*<p>
* If the response {@link Response#value() value} is empty, an empty Mono is returned.
*</p>
* <p><strong>Code samples</strong></p>
* {@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 <T> Mono<T> toMono(Response<T> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -54,4 +66,33 @@ private Mono<String> 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 <T> Mono<Response<T>> getMonoRestResponse(T value) {
Response<T> response = new Response<T>() {
@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, ")
Expand Down Expand Up @@ -401,4 +409,29 @@ private File createFileIfNotExist(String fileName) throws IOException {
return file;
}

private <T> Mono<Response<T>> getMonoRestResponse(T value) {
Response<T> response = new Response<T>() {
@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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,7 +98,7 @@ public final class KeyAsyncClient {
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> createKey(String name, KeyType keyType) {
return withContext(context -> createKey(name, keyType, context))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -155,7 +156,7 @@ Mono<Response<Key>> createKey(String name, KeyType keyType, Context context) {
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> createKey(KeyCreateOptions keyCreateOptions) {
return withContext(context -> createKey(keyCreateOptions, context))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

Mono<Response<Key>> createKey(KeyCreateOptions keyCreateOptions, Context context) {
Expand Down Expand Up @@ -196,7 +197,7 @@ Mono<Response<Key>> createKey(KeyCreateOptions keyCreateOptions, Context context
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> createRsaKey(RsaKeyCreateOptions rsaKeyCreateOptions) {
return withContext(context -> createRsaKey(rsaKeyCreateOptions, context))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -262,7 +263,7 @@ Mono<Response<Key>> createRsaKey(RsaKeyCreateOptions rsaKeyCreateOptions, Contex
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> createEcKey(EcKeyCreateOptions ecKeyCreateOptions) {
return withContext(context -> createEcKey(ecKeyCreateOptions, context))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -326,7 +327,7 @@ Mono<Response<Key>> createEcKey(EcKeyCreateOptions ecKeyCreateOptions, Context c
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> importKey(String name, JsonWebKey keyMaterial) {
return withContext(context -> importKey(name, keyMaterial, context))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

Mono<Response<Key>> importKey(String name, JsonWebKey keyMaterial, Context context) {
Expand Down Expand Up @@ -366,7 +367,7 @@ Mono<Response<Key>> importKey(String name, JsonWebKey keyMaterial, Context conte
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> importKey(KeyImportOptions keyImportOptions) {
return withContext(context -> importKey(keyImportOptions, context))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -433,7 +434,7 @@ public Mono<Key> getKey(String name, String version) {
return getKey(name);
}
return withContext(context -> getKey(name, version, context))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -574,7 +575,7 @@ public Mono<Response<Key>> getKeyWithResponse(KeyBase keyBase) {
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> updateKey(KeyBase key) {
return withContext(context -> updateKey(key, context))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -654,7 +655,7 @@ public Mono<Response<Key>> updateKeyWithResponse(KeyBase key, KeyOperation... ke
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> updateKey(KeyBase key, KeyOperation... keyOperations) {
return withContext(context -> updateKey(key, context, keyOperations))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

Mono<Response<Key>> updateKey(KeyBase key, Context context, KeyOperation... keyOperations) {
Expand Down Expand Up @@ -690,7 +691,7 @@ Mono<Response<Key>> updateKey(KeyBase key, Context context, KeyOperation... keyO
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DeletedKey> deleteKey(String name) {
return withContext(context -> deleteKey(name, context))
.flatMap(deletedKeyResponse -> Mono.justOrEmpty(deletedKeyResponse.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -743,7 +744,7 @@ Mono<Response<DeletedKey>> deleteKey(String name, Context context) {
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DeletedKey> getDeletedKey(String name) {
return withContext(context -> getDeletedKey(name, context))
.flatMap(deletedKeyResponse -> Mono.justOrEmpty(deletedKeyResponse.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -822,7 +823,7 @@ Mono<VoidResponse> purgeDeletedKey(String name, Context context) {
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> recoverDeletedKey(String name) {
return withContext(context -> recoverDeletedKey(name, context))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -878,7 +879,7 @@ Mono<Response<Key>> recoverDeletedKey(String name, Context context) {
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<byte[]> backupKey(String name) {
return withContext(context -> backupKey(name, context))
.flatMap(response -> Mono.justOrEmpty(response.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down Expand Up @@ -940,7 +941,7 @@ Mono<Response<byte[]>> backupKey(String name, Context context) {
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Key> restoreKey(byte[] backup) {
return withContext(context -> restoreKey(backup, context))
.flatMap(keyResponse -> Mono.justOrEmpty(keyResponse.value()));
.flatMap(FluxUtil::toMono);
}

/**
Expand Down

0 comments on commit 424cca3

Please sign in to comment.