package com.goafanti.core.auth;

import java.util.Base64;


import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class RestClient {
	private final String BS_HOST= "http://localhost:8090/";
	//@Test
	public void whenSecuredRestApiIsConsumed_then200OK() {
	    RestTemplate restTemplate = new RestTempleteConfig().getRestTemplate();
	    ResponseEntity<String> entity = restTemplate.exchange(BS_HOST, HttpMethod.GET, null, String.class);
	    System.out.println(entity.getStatusCode());
	    System.out.println(entity.getBody());
	}
	private HttpHeaders getHeaders(){
		String plainCredentials="dev:123456";
		String base64Credentials = Base64.getEncoder().encodeToString(plainCredentials.getBytes());
		HttpHeaders headers = new HttpHeaders();
		headers.add("Authorization", "Basic " + base64Credentials);
		return headers;
	}
 
	public String sendToBs(String apiUrl,HttpMethod method,LinkedMultiValueMap<String, String> params) {
		RestTemplate restTemplate = new RestTemplate();
		HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(params,getHeaders());
		ResponseEntity<String> response = restTemplate.exchange(BS_HOST + "/" + apiUrl, method,request, String.class);
		
		return response.getBody();
	}
	
//	@Test
	public void testLogin() {
		System.out.println(sendToBs("login", HttpMethod.GET, null));
	}
}
