package gs.teamup.bot.template;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;

import com.teamup.bot.template.oauth2.Oauth2Template;

import lombok.extern.apachecommons.CommonsLog;

@CommonsLog
@Component
public class BaseTemplate {

	@Autowired
	Oauth2Template oauth2;

	@Autowired
	@Qualifier("restTemplate")
	RestOperations rest;

	protected ResponseEntity<byte[]> getByte(String url) {

		HttpEntity<String> entity = getDownEntity();
		ResponseEntity<byte[]> response = rest.exchange(url, HttpMethod.GET, entity, byte[].class);

		return response;
	}

	protected <T> T get(String url, ParameterizedTypeReference<T> p) {
		return send(url, null, p, HttpMethod.GET);
	}

	protected <T> T post(String url, Object request, ParameterizedTypeReference<T> p) {
		return send(url, request, p, HttpMethod.POST);
	}

	private <T> T send(String url, Object request, ParameterizedTypeReference<T> p, HttpMethod httpMethod) {

		HttpEntity<Object> entity = getEntity(request);
		if( entity == null){
			return null;
		}
		ResponseEntity<T> responseEntity = null;

		try {
			responseEntity = rest.exchange(url, httpMethod, entity, p);
		} catch (Exception e) {
			log.error(url, e);
		}

		if (responseEntity != null && responseEntity.getStatusCode().equals(HttpStatus.OK)) {
			return responseEntity.getBody();
		} else {
			if(responseEntity != null){
				log.error(responseEntity.getStatusCode());
			}
		}
		return null;

	}

	private HttpEntity<Object> getEntity(Object request) {

		OAuth2AccessToken token = oauth2.token();

		if (token == null) {
			log.debug("token is null ");
			return null;
		}

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.add("Authorization", "bearer " + token.getValue());
		return new HttpEntity<Object>(request, headers);
	}

	private HttpEntity<String> getDownEntity() {

		OAuth2AccessToken token = oauth2.token();

		if (token == null) {
			log.debug("token is null ");
			return null;
		}

		HttpHeaders headers = new HttpHeaders();
		headers.add("Authorization", "bearer " + token.getValue());
		return new HttpEntity<String>(headers);
	}
}

@@ -22,24 +22,125 @@
 .template;%0A%0A
+%0Aimport gs.teamup.bot.template.oauth2.Oauth2Template;%0Aimport lombok.extern.apachecommons.CommonsLog;%0A
 import org.s
@@ -349,242 +349,9 @@
 ttp.
-HttpEntity;%0Aimport org.springframework.http.HttpHeaders;%0Aimport org.springframework.http.HttpMethod;%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.http.ResponseEntity
+*
 ;%0Aim
@@ -512,28 +512,32 @@
 Rest
-Opera
+ClientExcep
 tion
-s;%0A
+;
 %0Aimport 
 com.
@@ -536,101 +536,53 @@
 ort 
-com.teamup.bot.template.oauth2.Oauth2Template;%0A%0Aimport lombok.extern.apachecommons.CommonsLog
+org.springframework.web.client.RestOperations
 ;%0A%0A@
@@ -666,16 +666,24 @@
 e oauth2
+Template
 ;%0A%0A%09@Aut
@@ -744,230 +744,8 @@
 t;%0A%0A
-%09protected ResponseEntity%3Cbyte%5B%5D%3E getByte(String url) %7B%0A%0A%09%09HttpEntity%3CString%3E entity = getDownEntity();%0A%09%09ResponseEntity%3Cbyte%5B%5D%3E response = rest.exchange(url, HttpMethod.GET, entity, byte%5B%5D.class);%0A%0A%09%09return response;%0A%09%7D%0A%0A
 %09pro
@@ -1321,16 +1321,26 @@
  catch (
+RestClient
 Exceptio
@@ -1698,32 +1698,40 @@
 n token = oauth2
+Template
 .token();%0A%0A%09%09if 
@@ -1752,40 +1752,8 @@
 ) %7B%0A
-%09%09%09log.debug(%22token is null %22);%0A
 %09%09%09r
@@ -1987,324 +1987,6 @@
 %09%7D%0A%0A
-%09private HttpEntity%3CString%3E getDownEntity() %7B%0A%0A%09%09OAuth2AccessToken token = oauth2.token();%0A%0A%09%09if (token == null) %7B%0A%09%09%09log.debug(%22token is null %22);%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09HttpHeaders headers = new HttpHeaders();%0A%09%09headers.add(%22Authorization%22, %22bearer %22 + token.getValue());%0A%09%09return new HttpEntity%3CString%3E(headers);%0A%09%7D%0A
 %7D%0A

add base template