forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asjordi.java
37 lines (24 loc) · 1.01 KB
/
asjordi.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
37
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class RickAndMortyApi {
private final int id;
public RickAndMortyApi(int id) {
this.id = id;
}
public String getCharacter() throws URISyntaxException, IOException, InterruptedException {
if (this.id > 826 || this.id < 1) return "The id parameter must be between 1 and 826";
String url = String.format("https://rickandmortyapi.com/api/character/%s", this.id);
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest getRequest = HttpRequest.newBuilder()
.uri(new URI(url))
.GET()
.build();
HttpResponse<String> getResponse = httpClient.send(getRequest, HttpResponse.BodyHandlers.ofString());
if (getResponse.statusCode() > 400) return "There was an error!";
return getResponse.body();
}
}