(Unfinished) Testing API Retrieval

pull/4/head
Hammy 4 years ago
parent 696115535b
commit d734e76db8

@ -26,7 +26,7 @@ public class MyWaifuClient {
* <p>See <a href="https://mywaifulist.docs.stoplight.io/">MyWaifuList</a> for obtaining an API Key</p> * <p>See <a href="https://mywaifulist.docs.stoplight.io/">MyWaifuList</a> for obtaining an API Key</p>
* @param apiKey API Key to authorise API request * @param apiKey API Key to authorise API request
*/ */
public MyWaifuClient(@NotNull String apiKey) { MyWaifuClient(@NotNull String apiKey) {
myWaifuWrapper = new MyWaifuWrapper(apiKey); myWaifuWrapper = new MyWaifuWrapper(apiKey);
} }
@ -47,6 +47,14 @@ public class MyWaifuClient {
return myWaifuClient; return myWaifuClient;
} }
public void getWaifu(String slug) {
myWaifuWrapper.getWaifu(httpClient, slug);
}
public void getWaifu(Integer id) {
myWaifuWrapper.getWaifu(httpClient, String.valueOf(id));
}
/** /**
* Sets an instance of HttpClient * Sets an instance of HttpClient
* *

@ -1,13 +1,25 @@
package org.goudham.me; package org.goudham.me;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
/** /**
* Returns API information to {@link MyWaifuClient} * Returns API information to {@link MyWaifuClient}
*/ */
class MyWaifuWrapper { class MyWaifuWrapper {
private final String version = "1.0"; private final String version = "1.0";
private final String host = "https://mywaifulist.moe/api/v1/"; private static final String host = "https://mywaifulist.moe/api/v1/";
private final String apiKey; private final String apiKey;
private final ObjectMapper objectMapper = new ObjectMapper();
/** /**
* Instantiates an instance of {@link MyWaifuWrapper} to retrieve API Information * Instantiates an instance of {@link MyWaifuWrapper} to retrieve API Information
* @param apiKey API Key to authorise API request * @param apiKey API Key to authorise API request
@ -15,4 +27,45 @@ class MyWaifuWrapper {
MyWaifuWrapper(String apiKey) { MyWaifuWrapper(String apiKey) {
this.apiKey = apiKey; this.apiKey = apiKey;
} }
private void sendRequest(HttpClient httpClient, String param) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(host + param))
.version(httpClient.version())
.timeout(Duration.ofSeconds(30))
.header("Content-Type", "application/json")
.header("apikey", apiKey)
.GET()
.build();
CompletableFuture<HttpResponse<String>> response = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response);
// String resultBody = "";
// int resultStatusCode = 0;
// try {
// resultBody = response.thenApply(HttpResponse::body).get();
// resultStatusCode = response.thenApply(HttpResponse::statusCode).get();
//
// } catch (InterruptedException | ExecutionException e) {
// e.printStackTrace();
// }
//
// System.out.println(resultStatusCode);
// System.out.println(resultBody);
//
// try {
// JsonNode parent = objectMapper.readTree(resultBody);
// String waifuData = parent.get("data").toString();
// Waifu waifu = objectMapper.readValue(waifuData, Waifu.class);
// System.out.println(waifu);
// } catch (JsonProcessingException e) {
// e.printStackTrace();
// }
}
void getWaifu(HttpClient httpClient, String param) {
sendRequest(httpClient, "waifu/" + param);
}
} }

Loading…
Cancel
Save