refactor to move duplicate code to method (#429)

Signed-off-by: Arghya Sadhu <arghya88@gmail.com>

Co-authored-by: Artur Souza <artursouza.ms@outlook.com>
This commit is contained in:
Arghya Sadhu 2020-12-30 03:50:31 +05:30 committed by GitHub
parent b5dd421142
commit 4c1773c8cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 23 deletions

View File

@ -190,18 +190,20 @@ public class DaprClientHttp extends AbstractDaprClient {
byte[] serializedRequestBody = objectSerializer.serialize(request);
Mono<DaprHttp.Response> response = this.client.invokeApi(httMethod, path,
httpExtension.getQueryString(), serializedRequestBody, metadata, context);
return response.flatMap(r -> {
try {
T object = objectSerializer.deserialize(r.getBody(), type);
if (object == null) {
return Mono.empty();
}
return response.flatMap(r -> getMono(type, r)).map(r -> new Response<>(context, r));
} catch (Exception ex) {
return DaprException.wrapMono(ex);
}
}
return Mono.just(object);
} catch (Exception ex) {
return DaprException.wrapMono(ex);
}
}).map(r -> new Response<>(context, r));
private <T> Mono<T> getMono(TypeRef<T> type, DaprHttp.Response r) {
try {
T object = objectSerializer.deserialize(r.getBody(), type);
if (object == null) {
return Mono.empty();
}
return Mono.just(object);
} catch (Exception ex) {
return DaprException.wrapMono(ex);
}
@ -258,18 +260,7 @@ public class DaprClientHttp extends AbstractDaprClient {
String httpMethod = DaprHttp.HttpMethods.POST.name();
Mono<DaprHttp.Response> response = this.client.invokeApi(
httpMethod, url.toString(), null, payload, null, context);
return response.flatMap(r -> {
try {
T object = objectSerializer.deserialize(r.getBody(), type);
if (object == null) {
return Mono.empty();
}
return Mono.just(object);
} catch (Exception ex) {
return DaprException.wrapMono(ex);
}
}).map(r -> new Response<>(context, r));
return response.flatMap(r -> getMono(type, r)).map(r -> new Response<>(context, r));
} catch (Exception ex) {
return DaprException.wrapMono(ex);
}