2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019 Nokia. 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.impl.adapters;
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;
35 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
38 public class CloudHttpClient {
40 private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class);
42 private final Gson gson;
43 private final HttpClient httpClient;
46 public CloudHttpClient() {
47 this(HttpClient.create().doOnRequest(logRequest()).doOnResponse(logResponse()));
51 CloudHttpClient(HttpClient httpClient) {
52 this.gson = new Gson();
53 this.httpClient = httpClient;
57 public <T> Mono<T> callHttpGet(String url, Class<T> genericClassDeclaration) {
60 .doOnResponseError(doOnError())
63 (httpClientResponse, content) -> getJsonFromRequest(content.toString(), genericClassDeclaration));
66 private BiConsumer<HttpClientResponse, Throwable> doOnError() {
67 return (httpClientResponse, throwable) -> {
68 Mono.error(getException(httpClientResponse));
73 private RuntimeException getException(HttpClientResponse response) {
74 return new RuntimeException(String.format("Request for cloud config failed: HTTP %d",
75 response.status().code()));
78 private <T> Mono<T> getJsonFromRequest(String body, Class<T> genericClassDeclaration) {
80 return Mono.just(parseJson(body, genericClassDeclaration));
81 } catch (JsonSyntaxException | IllegalStateException e) {
86 private <T> T parseJson(String body, Class<T> genericClassDeclaration) {
87 return gson.fromJson(body, genericClassDeclaration);
91 private static BiConsumer<HttpClientRequest, Connection> logRequest() {
92 return (httpClientRequest, connection) -> {
93 LOGGER.debug("Request: {} {}", httpClientRequest.method(), httpClientRequest.uri());
94 httpClientRequest.requestHeaders().forEach(stringStringEntry -> {
95 LOGGER.trace("{}={}", stringStringEntry.getKey(), stringStringEntry.getValue());
101 private static BiConsumer<? super HttpClientResponse, ? super Connection> logResponse() {
102 return (httpClientresponse, connection) -> {
103 LOGGER.debug("Response status: {}", httpClientresponse.status());