-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathApp.java
32 lines (30 loc) · 983 Bytes
/
App.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.io.InputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class App {
public static void main(String[] args) {
var fetcher = new JokeFetcher(new RealHttpClient());
var joke = fetcher.getJokeText("R7UfaahVfFd");
if (joke == null) {
System.out.println("Cannot fetch joke.");
} else {
System.out.println(joke);
}
}
}
final class RealHttpClient implements HttpClient {
@Override
public InputStream get(String url) throws IOException {
URL javaUrl;
try {
javaUrl = new URL(url);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Bad URL");
}
var connection = (HttpURLConnection) javaUrl.openConnection();
connection.setRequestProperty("Accept", "text/plain");
return connection.getInputStream();
}
}