All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.social.partnercenter.http.client.RestResource Maven / Gradle / Ivy

Go to download

A provider extension for Spring Social to enable connectivity with Microsoft and an API binding for Microsoft's Partner Center API.

There is a newer version: 10.11.0
Show newest version
package org.springframework.social.partnercenter.http.client;

import static java.util.Collections.singletonList;

import java.net.URI;
import java.util.Map;

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.web.client.RestTemplate;

public class RestResource {
	private RestTemplate restTemplate;
	private String resourceBaseUri;

	public HttpRequestBuilder request(){
		return new HttpRequestBuilder(this, this.resourceBaseUri);
	}
	public HttpRequestBuilder request(MediaType mediaType){
		return new HttpRequestBuilder(this, this.resourceBaseUri).header(HttpHeaders.CONTENT_TYPE, singletonList(mediaType.toString()));
	}

	protected RestTemplate getRestTemplate() {
		return restTemplate;
	}

	public RestResource(RestTemplate restTemplate, String resourceBaseUri) {
		this.restTemplate = restTemplate;
		this.resourceBaseUri = resourceBaseUri;
	}

	public  T get(URI url, ParameterizedTypeReference responseType) {
		return execute(url, HttpMethod.GET, null, responseType);
	}

	public  T get(URI url, Class responseType) {
		return execute(url, HttpMethod.GET, null, responseType);
	}

	private   T get(URI url, Class responseType, Map header) {
		return execute(url, HttpMethod.GET, new HttpEntity<>(header), responseType);
	}

	public  T post(URI url, ParameterizedTypeReference responseType) {
		return execute(url, HttpMethod.POST, HttpEntity.EMPTY, responseType);
	}

	public  T post(URI url, ParameterizedTypeReference responseType, Map headers) {
		return execute(url, HttpMethod.POST, new HttpEntity(headers), responseType);
	}

	public  R post(URI uri, T entity, Class responseType) {
		return execute(uri, HttpMethod.POST, new HttpEntity<>(entity), responseType);
	}

	public  R post(URI uri, HttpEntity entity, Class responseType) {
		return execute(uri, HttpMethod.POST, entity, responseType);
	}

	public  R patch(URI uri, HttpEntity entity, Class responseType) {
		return execute(uri, HttpMethod.PATCH, entity, responseType);
	}

	public  R put(URI uri, HttpEntity entity, Class responseType) {
		return execute(uri, HttpMethod.PUT, entity, responseType);
	}

	public void delete(URI uri){
		restTemplate.delete(uri);
	}

	public  R execute(URI uri, HttpMethod httpMethod, HttpEntity entity, Class responseType){

		return restTemplate.exchange(
				uri, httpMethod, entity, responseType
		).getBody();
	}

	public  R execute(URI uri, HttpMethod httpMethod, HttpEntity entity, ParameterizedTypeReference responseType){
		return restTemplate.exchange(
				uri, httpMethod, entity, responseType
		).getBody();
	}

}