d97edb3b993c1c55753af94d87779fc272fd65e0
[dcaegen2/services/sdk.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration;
22
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;
33
34
35 /**
36  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
37  */
38
39 public class CloudHttpClient {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class);
42
43     private final Gson gson;
44     private final HttpClient httpClient;
45
46
47     public CloudHttpClient() {
48         this(HttpClient.create().doOnRequest(logRequest()).doOnResponse(logResponse()));
49     }
50
51
52     CloudHttpClient(HttpClient httpClient) {
53         this.gson = new Gson();
54         this.httpClient = httpClient;
55     }
56
57
58     public <T> Mono<T> callHttpGet(String url, Class<T> genericClassDeclaration) {
59         return httpClient
60             .baseUrl(url)
61             .doOnResponseError(doOnError())
62             .get()
63             .responseSingle(
64                 (httpClientResponse, content) -> getJsonFromRequest(content.toString(), genericClassDeclaration));
65     }
66
67     private BiConsumer<HttpClientResponse, Throwable> doOnError() {
68         return (httpClientResponse, throwable) -> {
69             Mono.error(getException(httpClientResponse));
70         };
71     }
72
73
74     private RuntimeException getException(HttpClientResponse response) {
75         return new RuntimeException(String.format("Request for cloud config failed: HTTP %d",
76             response.status().code()));
77     }
78
79     private <T> Mono<T> getJsonFromRequest(String body, Class<T> genericClassDeclaration) {
80         try {
81             return Mono.just(parseJson(body, genericClassDeclaration));
82         } catch (JsonSyntaxException | IllegalStateException e) {
83             return Mono.error(e);
84         }
85     }
86
87     private <T> T parseJson(String body, Class<T> genericClassDeclaration) {
88         return gson.fromJson(body, genericClassDeclaration);
89     }
90
91
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());
97             });
98
99         };
100     }
101
102     private static BiConsumer<? super HttpClientResponse, ? super Connection> logResponse() {
103         return (httpClientresponse, connection) -> {
104             LOGGER.debug("Response status: {}", httpClientresponse.status());
105         };
106     }
107
108
109 }