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.adapters.http;
23 import com.google.gson.Gson;
24 import io.netty.handler.ssl.SslContext;
25 import io.vavr.collection.HashMap;
26 import java.util.Collections;
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;
38 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
39 * @deprecated use {@link RxHttpClient} instead
42 public class CloudHttpClient {
44 private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class);
45 private final Gson gson = new Gson();
46 private final RxHttpClient httpClient;
48 CloudHttpClient(RxHttpClient httpClient) {
49 this.httpClient = httpClient;
52 public CloudHttpClient() {
53 this(RxHttpClient.create());
56 public CloudHttpClient(SslContext sslContext) {
57 this(RxHttpClient.create(sslContext));
60 public <T> Mono<T> get(String url, Class<T> bodyClass) {
61 return get(url, RequestDiagnosticContext.create(), bodyClass);
64 public <T> Mono<T> get(String url, RequestDiagnosticContext context, Class<T> bodyClass) {
65 return get(url, context, Collections.emptyMap(), bodyClass);
68 public <T> Mono<T> get(
70 RequestDiagnosticContext context,
71 Map<String, String> customHeaders,
73 return httpClient.call(
74 ImmutableHttpRequest.builder()
75 .method(HttpMethod.GET)
77 .customHeaders(HashMap.ofAll(customHeaders))
78 .diagnosticContext(context)
80 .doOnNext(HttpResponse::throwIfUnsuccessful)
81 .map(HttpResponse::bodyAsString)
82 .map(body -> gson.fromJson(body, bodyClass));
86 public Mono<HttpClientResponse> post(
88 RequestDiagnosticContext context,
89 Map<String, String> customHeaders,
90 JsonBodyBuilder jsonBodyBuilder,
91 ClientModel clientModel) {
92 return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.POST);
95 public Mono<HttpClientResponse> patch(
97 RequestDiagnosticContext context,
98 Map<String, String> customHeaders,
99 JsonBodyBuilder jsonBodyBuilder,
100 ClientModel clientModel) {
101 return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PATCH);
104 public Mono<HttpClientResponse> put(
106 RequestDiagnosticContext context,
107 Map<String, String> customHeaders,
108 JsonBodyBuilder jsonBodyBuilder,
109 ClientModel clientModel) {
110 return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PUT);
113 private Mono<HttpClientResponse> callForRawResponse(
115 RequestDiagnosticContext context,
116 Map<String, String> customHeaders,
117 JsonBodyBuilder jsonBodyBuilder,
118 ClientModel clientModel,
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);
126 return httpClient.prepareRequest(
127 ImmutableHttpRequest.builder()
129 .customHeaders(HashMap.ofAll(customHeaders))
130 .diagnosticContext(context)
131 .body(RequestBody.fromString(jsonBody))
134 .responseSingle((httpClientResponse, byteBufMono) -> Mono.just(httpClientResponse));