org.swaggertools.demo.client.BaseClient Maven / Gradle / Ivy
package org.swaggertools.demo.client;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.*;
import java.util.function.Consumer;
public abstract class BaseClient {
protected static final ParameterizedTypeReference VOID = new ParameterizedTypeReference() {};
private static final Map EMPTY_MAP = new HashMap<>();
private static final MultiValueMap EMPTY_MULTI_MAP = new LinkedMultiValueMap<>();
protected final RestTemplate restTemplate;
protected String basePath = "";
protected Map> headers;
protected Consumer requestCustomizer;
public BaseClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public BaseClient(RestTemplate restTemplate, String basePath) {
this.restTemplate = restTemplate;
this.basePath = basePath;
}
public BaseClient(RestTemplate restTemplate, String basePath, Map> headers) {
this.restTemplate = restTemplate;
this.basePath = basePath;
this.headers = headers;
}
public RestTemplate getRestTemplate() {
return restTemplate;
}
public String getBasePath() {
return basePath;
}
public void setBasePath(String basePath) {
this.basePath = basePath;
}
public Map> getHeaders() {
return headers;
}
public void setHeaders(Map> headers) {
this.headers = headers;
}
public Consumer getRequestCustomizer() {
return requestCustomizer;
}
public void setRequestCustomizer(Consumer requestCustomizer) {
this.requestCustomizer = requestCustomizer;
}
protected MultiValueMap createQueryParameters(Object... keyValues) {
if (keyValues.length == 0) {
return EMPTY_MULTI_MAP;
}
MultiValueMap parameters = new LinkedMultiValueMap<>();
for (int i = 0; i < keyValues.length; i += 2) {
Object key = keyValues[i];
Object value = keyValues[i + 1];
if (value != null) {
if (value instanceof List) {
parameters.put(key.toString(), (List) value);
} else {
parameters.put(key.toString(), Collections.singletonList(value.toString()));
}
}
}
return parameters;
}
protected Map createUrlVariables(Object... keyValues) {
if (keyValues.length == 0) {
return EMPTY_MAP;
}
Map parameters = new HashMap<>();
for (int i = 0; i < keyValues.length; i += 2) {
Object key = keyValues[i];
Object value = keyValues[i + 1];
if (value != null) {
parameters.put(key.toString(), value.toString());
}
}
return parameters;
}
protected ResponseEntity invokeAPI(String path, String method, Map urlVariables, MultiValueMap queryParams, Object body, ParameterizedTypeReference returnType) {
URI baseUrl = restTemplate.getUriTemplateHandler().expand(basePath);
URI uri = UriComponentsBuilder
.fromUri(baseUrl)
.path(path)
.queryParams(queryParams)
.buildAndExpand(urlVariables)
.toUri();
RequestEntity.BodyBuilder requestBuilder = RequestEntity.method(HttpMethod.resolve(method), uri);
customizeRequest(requestBuilder);
RequestEntity