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