-
Notifications
You must be signed in to change notification settings - Fork 176
/
JokeFetcher.java
36 lines (31 loc) · 1.18 KB
/
JokeFetcher.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
33
34
35
36
import java.io.InputStream;
import java.io.IOException;
import java.util.Scanner;
interface HttpClient {
// NOTE: in a real app one might want to return `InputStream` as a more general interface,
// or `String` if it's always read as one chunk, or even both
InputStream get(String url) throws IOException;
}
public final class JokeFetcher {
private final HttpClient client;
// NOTE: `client` could also be injected in `getJokeText`, in a real app it'd depend if it's used only for one method or more generally in the class
public JokeFetcher(HttpClient client) {
if (client == null) {
throw new IllegalArgumentException("Client cannot be null");
}
this.client = client;
}
/**
* Returns the joke with the specified ID.
*
* @param jokeId e.g., "R7UfaahVfFd"
*/
public String getJokeText(String jokeId) {
try (var connectionStream = client.get("https://icanhazdadjoke.com/j/" + jokeId);
var s = new Scanner(connectionStream).useDelimiter("\\A")) {
return s.next();
} catch (IOException e) {
return null;
}
}
}