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;
36 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
37 * @deprecated use {@link RxHttpClient} instead
40 public class CloudHttpClient {
42 private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class);
43 private final Gson gson = new Gson();
44 private final RxHttpClient httpClient;
46 CloudHttpClient(RxHttpClient httpClient) {
47 this.httpClient = httpClient;
50 public CloudHttpClient() {
51 this(RxHttpClientFactory.create());
54 public CloudHttpClient(SslContext sslContext) {
55 this(RxHttpClientFactory.create(sslContext));
58 public <T> Mono<T> get(String url, Class<T> bodyClass) {
59 return get(url, RequestDiagnosticContext.create(), bodyClass);
62 public <T> Mono<T> get(String url, RequestDiagnosticContext context, Class<T> bodyClass) {
63 return get(url, context, Collections.emptyMap(), bodyClass);
66 public Mono<HttpResponse> get(
68 RequestDiagnosticContext context,
69 Map<String, String> customHeaders) {
70 return httpClient.call(
71 ImmutableHttpRequest.builder()
72 .method(HttpMethod.GET)
74 .customHeaders(HashMap.ofAll(customHeaders))
75 .diagnosticContext(context)
79 public <T> Mono<T> get(
81 RequestDiagnosticContext context,
82 Map<String, String> customHeaders,
84 return get(url, context, customHeaders)
85 .doOnNext(HttpResponse::throwIfUnsuccessful)
86 .map(HttpResponse::bodyAsString)
87 .map(body -> gson.fromJson(body, bodyClass));
91 public Mono<HttpResponse> post(
93 RequestDiagnosticContext context,
94 Map<String, String> customHeaders,
95 JsonBodyBuilder jsonBodyBuilder,
96 ClientModel clientModel) {
97 return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.POST);
100 public Mono<HttpResponse> patch(
102 RequestDiagnosticContext context,
103 Map<String, String> customHeaders,
104 JsonBodyBuilder jsonBodyBuilder,
105 ClientModel clientModel) {
106 return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PATCH);
109 public Mono<HttpResponse> put(
111 RequestDiagnosticContext context,
112 Map<String, String> customHeaders,
113 JsonBodyBuilder jsonBodyBuilder,
114 ClientModel clientModel) {
115 return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PUT);
118 private Mono<HttpResponse> callForRawResponse(
120 RequestDiagnosticContext context,
121 Map<String, String> customHeaders,
122 JsonBodyBuilder jsonBodyBuilder,
123 ClientModel clientModel,
126 String jsonBody = jsonBodyBuilder.createJsonBody(clientModel);
127 LOGGER.debug("CloudHttpClient JSon body:: {}", jsonBody);
128 LOGGER.debug("CloudHttpClient url: {}", url);
129 LOGGER.debug("CloudHttpClient customHeaders: {}", customHeaders);
131 return httpClient.call(
132 ImmutableHttpRequest.builder()
134 .customHeaders(HashMap.ofAll(customHeaders))
135 .diagnosticContext(context)
136 .body(RequestBody.fromString(jsonBody))