-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarvelApiClient.java
More file actions
63 lines (55 loc) · 2.14 KB
/
MarvelApiClient.java
File metadata and controls
63 lines (55 loc) · 2.14 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.IOException;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
public class MarvelApiClient implements APIClient {
private static final String MARVEL_API_URL = "https://gateway.marvel.com/v1/public/comics";
private final String privateKey;
private final String publicKey;
public MarvelApiClient(String privateKey, String publicKey) {
this.privateKey = privateKey;
this.publicKey = publicKey;
}
@Override
public String getBody() {
try {
String timestamp = String.valueOf(Calendar.getInstance().getTimeInMillis());
String hash = getMD5Hash(timestamp+privateKey+publicKey);
String url = MARVEL_API_URL + "?ts=" + timestamp
+ "&apikey=" + this.publicKey
+ "&hash=" + hash;
HttpRequest request = HttpRequest
.newBuilder()
.uri(new URI(url))
.GET()
.build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
} catch (URISyntaxException | InterruptedException | IOException e) {
System.out.println("ERROR: " + e.getMessage());
e.printStackTrace();
return "";
}
}
private String getMD5Hash(String text) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] result = messageDigest.digest(text.getBytes(StandardCharsets.UTF_8));
return String.format("%032x", new BigInteger(1, result));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
@Override
public Class<? extends Content> type() {
return ComicBook.class;
}
}