package com.libreecm.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){
		LibreECMUser user = null;
		String link=url+"?id="+id;
		try {
			user =restTemplate.getForObject(link, LibreECMUser.class);
		} catch (HttpClientErrorException e){
			throw new ErrorConsumingRESTException(link);
		}
		
		return user;
	}
	

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

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