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;
34 public class AsyncRestClient {
35 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
36 private final WebClient client;
37 private final String baseUrl;
39 public AsyncRestClient(String baseUrl) {
40 this.client = WebClient.create(baseUrl);
41 this.baseUrl = baseUrl;
44 public Mono<ResponseEntity<String>> postForEntity(String uri, @Nullable String body) {
45 logger.debug("POST uri = '{}{}''", baseUrl, uri);
46 Mono<String> bodyProducer = body != null ? Mono.just(body) : Mono.empty();
47 RequestHeadersSpec<?> request = client.post() //
49 .contentType(MediaType.APPLICATION_JSON) //
50 .body(bodyProducer, String.class);
51 return retrieve(request);
54 public Mono<String> post(String uri, @Nullable String body) {
55 return postForEntity(uri, body) //
56 .flatMap(this::toBody);
59 public Mono<String> postWithAuthHeader(String uri, String body, String username, String password) {
60 logger.debug("POST (auth) uri = '{}{}''", baseUrl, uri);
61 RequestHeadersSpec<?> request = client.post() //
63 .headers(headers -> headers.setBasicAuth(username, password)) //
64 .contentType(MediaType.APPLICATION_JSON) //
66 return retrieve(request) //
67 .flatMap(this::toBody);
70 public Mono<ResponseEntity<String>> putForEntity(String uri, String body) {
71 logger.debug("PUT uri = '{}{}''", baseUrl, uri);
72 RequestHeadersSpec<?> request = client.put() //
74 .contentType(MediaType.APPLICATION_JSON) //
76 return retrieve(request);
79 public Mono<String> put(String uri, String body) {
80 return putForEntity(uri, body) //
81 .flatMap(this::toBody);
84 public Mono<ResponseEntity<String>> getForEntity(String uri) {
85 logger.debug("GET uri = '{}{}''", baseUrl, uri);
86 RequestHeadersSpec<?> request = client.get().uri(uri);
87 return retrieve(request);
90 public Mono<String> get(String uri) {
91 return getForEntity(uri) //
92 .flatMap(this::toBody);
95 public Mono<ResponseEntity<String>> deleteForEntity(String uri) {
96 logger.debug("DELETE uri = '{}{}''", baseUrl, uri);
97 RequestHeadersSpec<?> request = client.delete().uri(uri);
98 return retrieve(request);
101 public Mono<String> delete(String uri) {
102 return deleteForEntity(uri) //
103 .flatMap(this::toBody);
106 private Mono<ResponseEntity<String>> retrieve(RequestHeadersSpec<?> request) {
107 return request.retrieve() //
108 .toEntity(String.class);
111 Mono<String> toBody(ResponseEntity<String> entity) {
112 if (entity.getBody() == null) {
113 return Mono.just("");
115 return Mono.just(entity.getBody());