org.springframework.social.partnercenter.http.client.RestClient Maven / Gradle / Ivy
package org.springframework.social.partnercenter.http.client;
import static java.util.Collections.singletonList;
import java.net.URI;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.social.partnercenter.api.ApiFault;
import org.springframework.social.partnercenter.api.ApiFaultException;
import org.springframework.social.partnercenter.serialization.Json;
import org.springframework.social.partnercenter.serialization.JsonSerializationException;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestOperations;
import org.springframework.web.util.UriComponentsBuilder;
public class RestClient implements RestResource {
private RestOperations restTemplate;
private UriComponentsBuilder builder;
public RestClient(RestOperations restTemplate, URI resourceBaseUri) {
this.restTemplate = restTemplate;
this.builder = UriComponentsBuilder.fromUri(resourceBaseUri);
}
public HttpRequestBuilder request(){
return new HttpRequestBuilder(this, this.builder);
}
public HttpRequestBuilder request(String msRequestId, String msCorrelationId){
return new HttpRequestBuilder(this, this.builder, msRequestId, msCorrelationId);
}
public HttpRequestBuilder request(MediaType mediaType){
return new HttpRequestBuilder(this, this.builder).header(HttpHeaders.CONTENT_TYPE, singletonList(mediaType.toString()));
}
public ResponseEntity delete(URI uri, HttpHeaders headers){
try {
return restTemplate.exchange(uri, HttpMethod.DELETE, new HttpEntity<>(headers), String.class);
} catch (HttpClientErrorException e) {
throw buildApiFault(e);
}
}
public ResponseEntity execute(URI uri, HttpMethod httpMethod, HttpEntity entity, Class responseType) {
try{
return restTemplate.exchange(uri, httpMethod, entity, responseType);
} catch (HttpClientErrorException e){
throw buildApiFault(e);
}
}
public ResponseEntity execute(URI uri, HttpMethod httpMethod, HttpEntity entity, ParameterizedTypeReference responseType) {
try{
return restTemplate.exchange(uri, httpMethod, entity, responseType);
} catch (HttpClientErrorException e){
throw buildApiFault(e);
}
}
protected ApiFaultException buildApiFault(HttpClientErrorException e) {
String responseBody = e.getResponseBodyAsString();
try {
ApiFault apiFault = Json.fromJson(responseBody, ApiFault.class);
return new ApiFaultException(apiFault.getErrorMessage(), e, e.getStatusCode(), apiFault);
} catch (JsonSerializationException serializationException) {
ApiFault apiFault = new ApiFault();
apiFault.setErrorMessage(responseBody);
return new ApiFaultException(responseBody, e, e.getStatusCode(), apiFault);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy