Merge "Add sdnc-a1-controller in build process"
[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 public class AsyncRestClient {
35     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
36     private final WebClient client;
37     private final String baseUrl;
38
39     public AsyncRestClient(String baseUrl) {
40         this.client = WebClient.create(baseUrl);
41         this.baseUrl = baseUrl;
42     }
43
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() //
48             .uri(uri) //
49             .contentType(MediaType.APPLICATION_JSON) //
50             .body(bodyProducer, String.class);
51         return retrieve(request);
52     }
53
54     public Mono<String> post(String uri, @Nullable String body) {
55         return postForEntity(uri, body) //
56             .flatMap(this::toBody);
57     }
58
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() //
62             .uri(uri) //
63             .headers(headers -> headers.setBasicAuth(username, password)) //
64             .contentType(MediaType.APPLICATION_JSON) //
65             .bodyValue(body);
66         return retrieve(request) //
67             .flatMap(this::toBody);
68     }
69
70     public Mono<ResponseEntity<String>> putForEntity(String uri, String body) {
71         logger.debug("PUT uri = '{}{}''", baseUrl, uri);
72         RequestHeadersSpec<?> request = client.put() //
73             .uri(uri) //
74             .contentType(MediaType.APPLICATION_JSON) //
75             .bodyValue(body);
76         return retrieve(request);
77     }
78
79     public Mono<String> put(String uri, String body) {
80         return putForEntity(uri, body) //
81             .flatMap(this::toBody);
82     }
83
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);
88     }
89
90     public Mono<String> get(String uri) {
91         return getForEntity(uri) //
92             .flatMap(this::toBody);
93     }
94
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);
99     }
100
101     public Mono<String> delete(String uri) {
102         return deleteForEntity(uri) //
103             .flatMap(this::toBody);
104     }
105
106     private Mono<ResponseEntity<String>> retrieve(RequestHeadersSpec<?> request) {
107         return request.retrieve() //
108             .toEntity(String.class);
109     }
110
111     Mono<String> toBody(ResponseEntity<String> entity) {
112         if (entity.getBody() == null) {
113             return Mono.just("");
114         } else {
115             return Mono.just(entity.getBody());
116         }
117     }
118
119 }