package de.yannicpunktdee.yoshibot.utils;

import lombok.SneakyThrows;
import org.apache.commons.lang3.StringEscapeUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public final class RestHelper {
	
	@SneakyThrows
	public static String getFromURL(String url) {
		StringBuilder response = new StringBuilder("");
		
		HttpURLConnection con = null;
		
		try {
			con = (HttpURLConnection) (new URL(url)).openConnection();
			con.setRequestMethod("GET");
			BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
			
			String line;
			while ((line = br.readLine()) != null) {
				response.append(StringEscapeUtils.unescapeHtml4(line));
			}
			
			br.close();
		} catch (IOException e) {
			return "";
		} finally {
			if (con != null) con.disconnect();
		}
		
		return response.toString();
	}
	
}
