Last active
June 12, 2022 17:53
-
-
Save Matt-MX/3273a4ecd9bef2c172b50d392c1a420e to your computer and use it in GitHub Desktop.
Java Fetch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.nio.charset.StandardCharsets; | |
import java.util.concurrent.*; | |
import java.util.function.Consumer; | |
public class Fetch<T> extends Thread { | |
private Callable<T> task; | |
private Consumer<Exception> fail; | |
private Consumer<T> then; | |
private URL url; | |
public Fetch(String url) { | |
try { | |
this.url = new URL(url); | |
this.task = () -> (T)getPage(url); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public Fetch(Callable<T> task) { | |
this.task = task; | |
} | |
public Fetch<T> then(Consumer<T> task) { | |
then = task; | |
return this; | |
} | |
public Fetch<T> fail(Consumer<Exception> task) { | |
fail = task; | |
return this; | |
} | |
public Thread get() { | |
Thread thread = new Thread(this::execute); | |
thread.start(); | |
return thread; | |
} | |
private void execute() { | |
ExecutorService executor = Executors.newSingleThreadExecutor(); | |
Future<T> result = executor.submit(task); | |
while (!result.isDone()) { | |
} | |
try { | |
T actualResult = result.get(); | |
if (then != null) then.accept(actualResult); | |
} catch (InterruptedException | ExecutionException e) { | |
fail.accept(e); | |
} | |
} | |
private String getPage(String url) { | |
try { | |
URLConnection connection = new URL(url).openConnection(); | |
connection.setRequestProperty("User-Agent", | |
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); | |
connection.connect(); | |
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = r.readLine()) != null) { | |
sb.append(line); | |
} | |
return sb.toString(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
// URL example | |
new Fetch<>("https://www.google.com/") | |
.then(System.out::println) // print the content of the webpage | |
.fail(Throwable::printStackTrace) // print stacktrace on a failure | |
.get(); // actually fetch the data | |
// Function example | |
// Provide the DataType that the function returns | |
new Fetch<AbstractPack>(() -> PackHub.api().getPackFromId(0)) | |
// function to convert String into an AbstractPack (JSON string -> AbstractPack) | |
.then(result -> { | |
// print result.getName() if isValid() is true, otherwise print the error. | |
System.out.println(result.isValid() ? result.getName() : result.getError()); | |
}).fail(exception -> { | |
exception.printStackTrace(); // print stacktrace on failure | |
}).get(); // fetch data | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment