2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.clients;
23 import java.lang.invoke.MethodHandles;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.http.MediaType;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.lang.Nullable;
30 import org.springframework.web.reactive.function.client.WebClient;
31 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
32 import reactor.core.publisher.Mono;
35 * Generic reactive REST client.
37 public class AsyncRestClient {
38 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
39 private final WebClient client;
40 private final String baseUrl;
42 public AsyncRestClient(String baseUrl) {
43 this.client = WebClient.create(baseUrl);
44 this.baseUrl = baseUrl;
47 public Mono<ResponseEntity<String>> postForEntity(String uri, @Nullable String body) {
48 logger.debug("POST uri = '{}{}''", baseUrl, uri);
49 Mono<String> bodyProducer = body != null ? Mono.just(body) : Mono.empty();
50 RequestHeadersSpec<?> request = client.post() //
52 .contentType(MediaType.APPLICATION_JSON) //
53 .body(bodyProducer, String.class);
54 return retrieve(request);
57 public Mono<String> post(String uri, @Nullable String body) {
58 return postForEntity(uri, body) //
59 .flatMap(this::toBody);
62 public Mono<String> postWithAuthHeader(String uri, String body, String username, String password) {
63 logger.debug("POST (auth) uri = '{}{}''", baseUrl, uri);
64 RequestHeadersSpec<?> request = client.post() //
66 .headers(headers -> headers.setBasicAuth(username, password)) //
67 .contentType(MediaType.APPLICATION_JSON) //
69 return retrieve(request) //
70 .flatMap(this::toBody);
73 public Mono<ResponseEntity<String>> putForEntity(String uri, String body) {
74 logger.debug("PUT uri = '{}{}''", baseUrl, uri);
75 RequestHeadersSpec<?> request = client.put() //
77 .contentType(MediaType.APPLICATION_JSON) //
79 return retrieve(request);
82 public Mono<String> put(String uri, String body) {
83 return putForEntity(uri, body) //
84 .flatMap(this::toBody);
87 public Mono<ResponseEntity<String>> getForEntity(String uri) {
88 logger.debug("GET uri = '{}{}''", baseUrl, uri);
89 RequestHeadersSpec<?> request = client.get().uri(uri);
90 return retrieve(request);
93 public Mono<String> get(String uri) {
94 return getForEntity(uri) //
95 .flatMap(this::toBody);
98 public Mono<ResponseEntity<String>> deleteForEntity(String uri) {
99 logger.debug("DELETE uri = '{}{}''", baseUrl, uri);
100 RequestHeadersSpec<?> request = client.delete().uri(uri);
101 return retrieve(request);
104 public Mono<String> delete(String uri) {
105 return deleteForEntity(uri) //
106 .flatMap(this::toBody);
109 private Mono<ResponseEntity<String>> retrieve(RequestHeadersSpec<?> request) {
110 return request.retrieve() //
111 .toEntity(String.class);
114 Mono<String> toBody(ResponseEntity<String> entity) {
115 if (entity.getBody() == null) {
116 return Mono.just("");
118 return Mono.just(entity.getBody());