7b87511bd5955b40a4555007f2a7541eabd64e34
[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
35 /**
36  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
37  * @deprecated use {@link RxHttpClient} instead
38  */
39 @Deprecated
40 public class CloudHttpClient {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class);
43     private final Gson gson = new Gson();
44     private final RxHttpClient httpClient;
45
46     CloudHttpClient(RxHttpClient httpClient) {
47         this.httpClient = httpClient;
48     }
49
50     public CloudHttpClient() {
51         this(RxHttpClient.create());
52     }
53
54     public CloudHttpClient(SslContext sslContext) {
55         this(RxHttpClient.create(sslContext));
56     }
57
58     public <T> Mono<T> get(String url, Class<T> bodyClass) {
59         return get(url, RequestDiagnosticContext.create(), bodyClass);
60     }
61
62     public <T> Mono<T> get(String url, RequestDiagnosticContext context, Class<T> bodyClass) {
63         return get(url, context, Collections.emptyMap(), bodyClass);
64     }
65
66     public Mono<HttpResponse> get(
67             String url,
68             RequestDiagnosticContext context,
69             Map<String, String> customHeaders) {
70         return httpClient.call(
71                 ImmutableHttpRequest.builder()
72                         .method(HttpMethod.GET)
73                         .url(url)
74                         .customHeaders(HashMap.ofAll(customHeaders))
75                         .diagnosticContext(context)
76                         .build());
77     }
78
79     public <T> Mono<T> get(
80             String url,
81             RequestDiagnosticContext context,
82             Map<String, String> customHeaders,
83             Class<T> bodyClass) {
84         return get(url, context, customHeaders)
85                 .doOnNext(HttpResponse::throwIfUnsuccessful)
86                 .map(HttpResponse::bodyAsString)
87                 .map(body -> gson.fromJson(body, bodyClass));
88     }
89
90
91     public Mono<HttpResponse> post(
92         String url,
93         RequestDiagnosticContext context,
94         Map<String, String> customHeaders,
95         JsonBodyBuilder jsonBodyBuilder,
96         ClientModel clientModel) {
97         return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.POST);
98     }
99
100     public Mono<HttpResponse> patch(
101         String url,
102         RequestDiagnosticContext context,
103         Map<String, String> customHeaders,
104         JsonBodyBuilder jsonBodyBuilder,
105         ClientModel clientModel) {
106         return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PATCH);
107     }
108
109     public Mono<HttpResponse> put(
110         String url,
111         RequestDiagnosticContext context,
112         Map<String, String> customHeaders,
113         JsonBodyBuilder jsonBodyBuilder,
114         ClientModel clientModel) {
115         return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PUT);
116     }
117
118     private Mono<HttpResponse> callForRawResponse(
119         String url,
120         RequestDiagnosticContext context,
121         Map<String, String> customHeaders,
122         JsonBodyBuilder jsonBodyBuilder,
123         ClientModel clientModel,
124         HttpMethod method) {
125
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);
130
131         return httpClient.call(
132             ImmutableHttpRequest.builder()
133                 .url(url)
134                 .customHeaders(HashMap.ofAll(customHeaders))
135                 .diagnosticContext(context)
136                 .body(RequestBody.fromString(jsonBody))
137                 .method(method)
138                 .build());
139     }
140 }
141