2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration;
23 import com.google.gson.Gson;
24 import com.google.gson.JsonSyntaxException;
25 import java.util.function.BiConsumer;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import reactor.core.publisher.Mono;
29 import reactor.netty.Connection;
30 import reactor.netty.http.client.HttpClient;
31 import reactor.netty.http.client.HttpClientRequest;
32 import reactor.netty.http.client.HttpClientResponse;
36 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
39 public class CloudHttpClient {
41 private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class);
43 private final Gson gson;
44 private final HttpClient httpClient;
47 public CloudHttpClient() {
48 this(HttpClient.create().doOnRequest(logRequest()).doOnResponse(logResponse()));
52 CloudHttpClient(HttpClient httpClient) {
53 this.gson = new Gson();
54 this.httpClient = httpClient;
58 public <T> Mono<T> callHttpGet(String url, Class<T> genericClassDeclaration) {
61 .doOnResponseError(doOnError())
64 (httpClientResponse, content) -> getJsonFromRequest(content.toString(), genericClassDeclaration));
67 private BiConsumer<HttpClientResponse, Throwable> doOnError() {
68 return (httpClientResponse, throwable) -> {
69 Mono.error(getException(httpClientResponse));
74 private RuntimeException getException(HttpClientResponse response) {
75 return new RuntimeException(String.format("Request for cloud config failed: HTTP %d",
76 response.status().code()));
79 private <T> Mono<T> getJsonFromRequest(String body, Class<T> genericClassDeclaration) {
81 return Mono.just(parseJson(body, genericClassDeclaration));
82 } catch (JsonSyntaxException | IllegalStateException e) {
87 private <T> T parseJson(String body, Class<T> genericClassDeclaration) {
88 return gson.fromJson(body, genericClassDeclaration);
92 private static BiConsumer<HttpClientRequest, Connection> logRequest() {
93 return (httpClientRequest, connection) -> {
94 LOGGER.debug("Request: {} {}", httpClientRequest.method(), httpClientRequest.uri());
95 httpClientRequest.requestHeaders().forEach(stringStringEntry -> {
96 LOGGER.trace("{}={}", stringStringEntry.getKey(), stringStringEntry.getValue());
102 private static BiConsumer<? super HttpClientResponse, ? super Connection> logResponse() {
103 return (httpClientresponse, connection) -> {
104 LOGGER.debug("Response status: {}", httpClientresponse.status());