package com.libreecm.opencmis.consumers;

import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import com.libreecm.commonAPI.auxiliars.exceptions.ErrorConsumingRESTException;
import com.libreecm.commonAPI.model.users.LibreECMUser;

public class UserConsumer {

	RestTemplate restTemplate = new RestTemplate();
	 
	public UserConsumer(){ 	
	}
	
	public LibreECMUser getUser(String id, String url){
	try {
		return restTemplate.getForObject(url+"?id="+id, LibreECMUser.class);
	} catch (HttpClientErrorException e){
		throw new ErrorConsumingRESTException(url);
	}
	}
	

	public LibreECMUser getUserByName(String name, String url){
		try {
			return restTemplate.getForObject(url+"?name="+name, LibreECMUser.class);
		} catch (HttpClientErrorException e){
			throw new ErrorConsumingRESTException(url);
		}
	}

	public LibreECMUser setUser(LibreECMUser user, String url){
		try {
			return restTemplate.postForObject(url, user, LibreECMUser.class);
		} catch (HttpClientErrorException e){
			throw new ErrorConsumingRESTException(url);
		}
	}

	
}
