132d3d830fca6d06eae6498df9900464821aa661
[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.adapters.http;
22
23 import com.google.gson.Gson;
24 import io.netty.handler.ssl.SslContext;
25 import io.vavr.collection.HashMap;
26 import java.util.Collections;
27 import java.util.Map;
28 import org.onap.dcaegen2.services.sdk.rest.services.model.ClientModel;
29 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
30 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import reactor.core.publisher.Mono;
34 import reactor.netty.http.client.HttpClientResponse;
35
36
37 /**
38  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
39  * @deprecated use {@link RxHttpClient} instead
40  */
41 @Deprecated
42 public class CloudHttpClient {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class);
45     private final Gson gson = new Gson();
46     private final RxHttpClient httpClient;
47
48     CloudHttpClient(RxHttpClient httpClient) {
49         this.httpClient = httpClient;
50     }
51
52     public CloudHttpClient() {
53         this(RxHttpClient.create());
54     }
55
56     public CloudHttpClient(SslContext sslContext) {
57         this(RxHttpClient.create(sslContext));
58     }
59
60     public <T> Mono<T> get(String url, Class<T> bodyClass) {
61         return get(url, RequestDiagnosticContext.create(), bodyClass);
62     }
63
64     public <T> Mono<T> get(String url, RequestDiagnosticContext context, Class<T> bodyClass) {
65         return get(url, context, Collections.emptyMap(), bodyClass);
66     }
67
68     public <T> Mono<T> get(
69         String url,
70         RequestDiagnosticContext context,
71         Map<String, String> customHeaders,
72         Class<T> bodyClass) {
73         return httpClient.call(
74             ImmutableHttpRequest.builder()
75                 .method(HttpMethod.GET)
76                 .url(url)
77                 .customHeaders(HashMap.ofAll(customHeaders))
78                 .diagnosticContext(context)
79                 .build())
80             .doOnNext(HttpResponse::throwIfUnsuccessful)
81             .map(HttpResponse::bodyAsString)
82             .map(body -> gson.fromJson(body, bodyClass));
83     }
84
85
86     public Mono<HttpClientResponse> post(
87         String url,
88         RequestDiagnosticContext context,
89         Map<String, String> customHeaders,
90         JsonBodyBuilder jsonBodyBuilder,
91         ClientModel clientModel) {
92         return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.POST);
93     }
94
95     public Mono<HttpClientResponse> patch(
96         String url,
97         RequestDiagnosticContext context,
98         Map<String, String> customHeaders,
99         JsonBodyBuilder jsonBodyBuilder,
100         ClientModel clientModel) {
101         return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PATCH);
102     }
103
104     public Mono<HttpClientResponse> put(
105         String url,
106         RequestDiagnosticContext context,
107         Map<String, String> customHeaders,
108         JsonBodyBuilder jsonBodyBuilder,
109         ClientModel clientModel) {
110         return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PUT);
111     }
112
113     private Mono<HttpClientResponse> callForRawResponse(
114         String url,
115         RequestDiagnosticContext context,
116         Map<String, String> customHeaders,
117         JsonBodyBuilder jsonBodyBuilder,
118         ClientModel clientModel,
119         HttpMethod method) {
120
121         String jsonBody = jsonBodyBuilder.createJsonBody(clientModel);
122         LOGGER.debug("CloudHttpClient JSon body:: {}", jsonBody);
123         LOGGER.debug("CloudHttpClient url: {}", url);
124         LOGGER.debug("CloudHttpClient customHeaders: {}", customHeaders);
125
126         return httpClient.prepareRequest(
127             ImmutableHttpRequest.builder()
128                 .url(url)
129                 .customHeaders(HashMap.ofAll(customHeaders))
130                 .diagnosticContext(context)
131                 .body(RequestBody.fromString(jsonBody))
132                 .method(method)
133                 .build())
134             .responseSingle((httpClientResponse, byteBufMono) -> Mono.just(httpClientResponse));
135     }
136
137
138 }
139