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 reactor.core.publisher.Mono;
32 import reactor.netty.http.client.HttpClient;
33 import reactor.netty.http.client.HttpClientResponse;
37 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
38 * @deprecated use {@link RxHttpClient} instead
41 public class CloudHttpClient {
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(RxHttpClient.create());
54 public CloudHttpClient(SslContext sslContext) {
55 this(RxHttpClient.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 <T> Mono<T> get(
68 RequestDiagnosticContext context,
69 Map<String, String> customHeaders,
71 return httpClient.call(
72 ImmutableHttpRequest.builder()
73 .method(HttpMethod.GET)
75 .customHeaders(HashMap.ofAll(customHeaders))
76 .diagnosticContext(context)
78 .doOnNext(HttpResponse::throwIfUnsuccessful)
79 .map(HttpResponse::bodyAsString)
80 .map(body -> gson.fromJson(body, bodyClass));
84 public Mono<HttpClientResponse> post(
86 RequestDiagnosticContext context,
87 Map<String, String> customHeaders,
88 JsonBodyBuilder jsonBodyBuilder,
89 ClientModel clientModel) {
90 return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.POST);
93 public Mono<HttpClientResponse> patch(
95 RequestDiagnosticContext context,
96 Map<String, String> customHeaders,
97 JsonBodyBuilder jsonBodyBuilder,
98 ClientModel clientModel) {
99 return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PATCH);
102 public Mono<HttpClientResponse> put(
104 RequestDiagnosticContext context,
105 Map<String, String> customHeaders,
106 JsonBodyBuilder jsonBodyBuilder,
107 ClientModel clientModel) {
108 return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PUT);
111 private Mono<HttpClientResponse> callForRawResponse(
113 RequestDiagnosticContext context,
114 Map<String, String> customHeaders,
115 JsonBodyBuilder jsonBodyBuilder,
116 ClientModel clientModel,
118 return httpClient.prepareRequest(
119 ImmutableHttpRequest.builder()
121 .customHeaders(HashMap.ofAll(customHeaders))
122 .diagnosticContext(context)
123 .body(RequestBody.fromString(jsonBodyBuilder.createJsonBody(clientModel)))
126 .responseSingle((httpClientResponse, byteBufMono) -> Mono.just(httpClientResponse));