Minor changes
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / AsyncRestClient.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.clients;
22
23 import io.netty.channel.ChannelOption;
24 import io.netty.handler.timeout.ReadTimeoutHandler;
25 import io.netty.handler.timeout.WriteTimeoutHandler;
26
27 import java.lang.invoke.MethodHandles;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.http.MediaType;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
34 import org.springframework.lang.Nullable;
35 import org.springframework.web.reactive.function.client.WebClient;
36 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
37
38 import reactor.core.publisher.Mono;
39 import reactor.netty.http.client.HttpClient;
40 import reactor.netty.tcp.TcpClient;
41
42 /**
43  * Generic reactive REST client.
44  */
45 public class AsyncRestClient {
46     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
47     private final WebClient client;
48     private final String baseUrl;
49
50     public AsyncRestClient(String baseUrl) {
51
52         TcpClient tcpClient = TcpClient.create() //
53             .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10_000) //
54             .doOnConnected(connection -> {
55                 connection.addHandler(new ReadTimeoutHandler(10));
56                 connection.addHandler(new WriteTimeoutHandler(30));
57             });
58         HttpClient httpClient = HttpClient.from(tcpClient);
59         ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
60
61         this.client = WebClient.builder() //
62             .clientConnector(connector) //
63             .baseUrl(baseUrl) //
64             .build();
65
66         this.baseUrl = baseUrl;
67     }
68
69     public Mono<ResponseEntity<String>> postForEntity(String uri, @Nullable String body) {
70         logger.debug("POST uri = '{}{}''", baseUrl, uri);
71         Mono<String> bodyProducer = body != null ? Mono.just(body) : Mono.empty();
72         RequestHeadersSpec<?> request = client.post() //
73             .uri(uri) //
74             .contentType(MediaType.APPLICATION_JSON) //
75             .body(bodyProducer, String.class);
76         return retrieve(request);
77     }
78
79     public Mono<String> post(String uri, @Nullable String body) {
80         return postForEntity(uri, body) //
81             .flatMap(this::toBody);
82     }
83
84     public Mono<String> postWithAuthHeader(String uri, String body, String username, String password) {
85         logger.debug("POST (auth) uri = '{}{}''", baseUrl, uri);
86         RequestHeadersSpec<?> request = client.post() //
87             .uri(uri) //
88             .headers(headers -> headers.setBasicAuth(username, password)) //
89             .contentType(MediaType.APPLICATION_JSON) //
90             .bodyValue(body);
91         return retrieve(request) //
92             .flatMap(this::toBody);
93     }
94
95     public Mono<ResponseEntity<String>> putForEntity(String uri, String body) {
96         logger.debug("PUT uri = '{}{}''", baseUrl, uri);
97         RequestHeadersSpec<?> request = client.put() //
98             .uri(uri) //
99             .contentType(MediaType.APPLICATION_JSON) //
100             .bodyValue(body);
101         return retrieve(request);
102     }
103
104     public Mono<String> put(String uri, String body) {
105         return putForEntity(uri, body) //
106             .flatMap(this::toBody);
107     }
108
109     public Mono<ResponseEntity<String>> getForEntity(String uri) {
110         logger.debug("GET uri = '{}{}''", baseUrl, uri);
111         RequestHeadersSpec<?> request = client.get().uri(uri);
112         return retrieve(request);
113     }
114
115     public Mono<String> get(String uri) {
116         return getForEntity(uri) //
117             .flatMap(this::toBody);
118     }
119
120     public Mono<ResponseEntity<String>> deleteForEntity(String uri) {
121         logger.debug("DELETE uri = '{}{}''", baseUrl, uri);
122         RequestHeadersSpec<?> request = client.delete().uri(uri);
123         return retrieve(request);
124     }
125
126     public Mono<String> delete(String uri) {
127         return deleteForEntity(uri) //
128             .flatMap(this::toBody);
129     }
130
131     private Mono<ResponseEntity<String>> retrieve(RequestHeadersSpec<?> request) {
132         return request.retrieve() //
133             .toEntity(String.class);
134     }
135
136     Mono<String> toBody(ResponseEntity<String> entity) {
137         if (entity.getBody() == null) {
138             return Mono.just("");
139         } else {
140             return Mono.just(entity.getBody());
141         }
142     }
143
144 }