8e317f8b8c1994b74768cffe4823691e460d8af7
[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 reactor.core.publisher.Mono;
32 import reactor.netty.http.client.HttpClient;
33 import reactor.netty.http.client.HttpClientResponse;
34
35
36 /**
37  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
38  * @deprecated use {@link RxHttpClient} instead
39  */
40 @Deprecated
41 public class CloudHttpClient {
42
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 <T> Mono<T> get(
67             String url,
68             RequestDiagnosticContext context,
69             Map<String, String> customHeaders,
70             Class<T> bodyClass) {
71         return httpClient.call(
72                 ImmutableHttpRequest.builder()
73                         .method(HttpMethod.GET)
74                         .url(url)
75                         .customHeaders(HashMap.ofAll(customHeaders))
76                         .diagnosticContext(context)
77                         .build())
78                 .doOnNext(HttpResponse::throwIfUnsuccessful)
79                 .map(HttpResponse::bodyAsString)
80                 .map(body -> gson.fromJson(body, bodyClass));
81     }
82
83
84     public Mono<HttpClientResponse> post(
85             String url,
86             RequestDiagnosticContext context,
87             Map<String, String> customHeaders,
88             JsonBodyBuilder jsonBodyBuilder,
89             ClientModel clientModel) {
90         return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.POST);
91     }
92
93     public Mono<HttpClientResponse> patch(
94             String url,
95             RequestDiagnosticContext context,
96             Map<String, String> customHeaders,
97             JsonBodyBuilder jsonBodyBuilder,
98             ClientModel clientModel) {
99         return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PATCH);
100     }
101
102     public Mono<HttpClientResponse> put(
103         String url,
104         RequestDiagnosticContext context,
105         Map<String, String> customHeaders,
106         JsonBodyBuilder jsonBodyBuilder,
107         ClientModel clientModel) {
108         return callForRawResponse(url, context, customHeaders, jsonBodyBuilder, clientModel, HttpMethod.PUT);
109     }
110
111     private Mono<HttpClientResponse> callForRawResponse(
112             String url,
113             RequestDiagnosticContext context,
114             Map<String, String> customHeaders,
115             JsonBodyBuilder jsonBodyBuilder,
116             ClientModel clientModel,
117             HttpMethod method) {
118         return httpClient.prepareRequest(
119                 ImmutableHttpRequest.builder()
120                         .url(url)
121                         .customHeaders(HashMap.ofAll(customHeaders))
122                         .diagnosticContext(context)
123                         .body(RequestBody.fromString(jsonBodyBuilder.createJsonBody(clientModel)))
124                         .method(method)
125                         .build())
126                 .responseSingle((httpClientResponse, byteBufMono) -> Mono.just(httpClientResponse));
127     }
128
129 }
130