Merge "API documentation"
[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 java.lang.invoke.MethodHandles;
24
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;
33
34 /**
35  * Generic reactive REST client.
36  */
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;
41
42     public AsyncRestClient(String baseUrl) {
43         this.client = WebClient.create(baseUrl);
44         this.baseUrl = baseUrl;
45     }
46
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() //
51             .uri(uri) //
52             .contentType(MediaType.APPLICATION_JSON) //
53             .body(bodyProducer, String.class);
54         return retrieve(request);
55     }
56
57     public Mono<String> post(String uri, @Nullable String body) {
58         return postForEntity(uri, body) //
59             .flatMap(this::toBody);
60     }
61
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() //
65             .uri(uri) //
66             .headers(headers -> headers.setBasicAuth(username, password)) //
67             .contentType(MediaType.APPLICATION_JSON) //
68             .bodyValue(body);
69         return retrieve(request) //
70             .flatMap(this::toBody);
71     }
72
73     public Mono<ResponseEntity<String>> putForEntity(String uri, String body) {
74         logger.debug("PUT uri = '{}{}''", baseUrl, uri);
75         RequestHeadersSpec<?> request = client.put() //
76             .uri(uri) //
77             .contentType(MediaType.APPLICATION_JSON) //
78             .bodyValue(body);
79         return retrieve(request);
80     }
81
82     public Mono<String> put(String uri, String body) {
83         return putForEntity(uri, body) //
84             .flatMap(this::toBody);
85     }
86
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);
91     }
92
93     public Mono<String> get(String uri) {
94         return getForEntity(uri) //
95             .flatMap(this::toBody);
96     }
97
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);
102     }
103
104     public Mono<String> delete(String uri) {
105         return deleteForEntity(uri) //
106             .flatMap(this::toBody);
107     }
108
109     private Mono<ResponseEntity<String>> retrieve(RequestHeadersSpec<?> request) {
110         return request.retrieve() //
111             .toEntity(String.class);
112     }
113
114     Mono<String> toBody(ResponseEntity<String> entity) {
115         if (entity.getBody() == null) {
116             return Mono.just("");
117         } else {
118             return Mono.just(entity.getBody());
119         }
120     }
121
122 }