Merge pull request #5 from sgoudham/main

Implement Basic Functionality for Get Waifu Endpoint
pull/8/head
Hamothy 3 years ago committed by GitHub
commit 346433368f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,2 +1,29 @@
# MyWaifuWrapper <h1 align="center">MyWaifuWrapper | An Asynchronous Java API Wrapper for MyWaifuList</h1>
MyWaifuWrapper - An Asynchronous Java API Wrapper for MyWaifuList
<p align="center">
<img src="https://goudham.me/jenkins/job/MyWaifuWrapper/job/release/badge/icon"/>
<a href="https://codecov.io/gh/sgoudham/MyWaifuWrapper">
<img src="https://codecov.io/gh/sgoudham/MyWaifuWrapper/branch/release/graph/badge.svg?token=RxUDnCWnF0"/>
</a>
</p>
<p align="center">
<img src="https://img.shields.io/nexus/maven-goudham/org.goudham.me/MyWaifuWrapper?server=https%3A%2F%2Fgoudham.me"/>
<img src="https://img.shields.io/badge/project%20type-personal-blueviolet"/>
<img src="https://img.shields.io/github/last-commit/sgoudham/MyWaifuWrapper"/>
<img src="https://img.shields.io/github/issues/sgoudham/MyWaifuWrapper?label=issues"/>
<img src="https://img.shields.io/github/issues-pr/sgoudham/MyWaifuWrapper"/>
</p>
# Summary
This is an Asynchronous API Wrapper for [MyWaifuList](https://mywaifulist.moe/dash)
# Download
TODO
# Usage
TODO

@ -6,7 +6,7 @@
<groupId>org.goudham.me</groupId> <groupId>org.goudham.me</groupId>
<artifactId>MyWaifuWrapper</artifactId> <artifactId>MyWaifuWrapper</artifactId>
<version>0.1</version> <version>0.1.2</version>
<name>MyWaifuWrapper</name> <name>MyWaifuWrapper</name>
<properties> <properties>

@ -1,5 +1,7 @@
package org.goudham.me; package org.goudham.me;
import org.goudham.me.api.entity.waifu.Waifu;
import org.goudham.me.exception.APIResponseException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLParameters;
@ -47,12 +49,12 @@ public class MyWaifuClient {
return myWaifuClient; return myWaifuClient;
} }
public void getWaifu(String slug) { public Response<Waifu> getWaifu(String slug) throws APIResponseException {
myWaifuWrapper.getWaifu(httpClient, slug); return myWaifuWrapper.getWaifu(httpClient, slug);
} }
public void getWaifu(Integer id) { public Response<Waifu> getWaifu(Integer id) throws APIResponseException {
myWaifuWrapper.getWaifu(httpClient, String.valueOf(id)); return myWaifuWrapper.getWaifu(httpClient, String.valueOf(id));
} }
/** /**
@ -60,7 +62,7 @@ public class MyWaifuClient {
* *
* @param httpClient HttpClient for executing API requests * @param httpClient HttpClient for executing API requests
*/ */
public void setHttpClient(HttpClient httpClient) { void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient; this.httpClient = httpClient;
} }

@ -1,6 +1,10 @@
package org.goudham.me; package org.goudham.me;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.goudham.me.api.entity.waifu.Waifu;
import org.goudham.me.exception.APIResponseException;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
@ -8,12 +12,13 @@ import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.time.Duration; import java.time.Duration;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/** /**
* Returns API information to {@link MyWaifuClient} * Returns API information to {@link MyWaifuClient}
*/ */
class MyWaifuWrapper { public class MyWaifuWrapper {
private final String version = "1.0"; private final String version = "1.0";
private static 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;
@ -28,7 +33,13 @@ class MyWaifuWrapper {
this.apiKey = apiKey; this.apiKey = apiKey;
} }
private void sendRequest(HttpClient httpClient, String param) { /**
*
*
* @param httpClient The {@link HttpClient} to use for sending {@link HttpRequest}'s
* @param param The end of the endpoint appended onto the host
*/
private Result sendRequest(HttpClient httpClient, String param) throws APIResponseException {
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(host + param)) .uri(URI.create(host + param))
.version(httpClient.version()) .version(httpClient.version())
@ -40,32 +51,38 @@ class MyWaifuWrapper {
CompletableFuture<HttpResponse<String>> response = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()); CompletableFuture<HttpResponse<String>> response = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); int responseCode;
String responseBody;
try {
responseCode = response.thenApply(HttpResponse::statusCode).get();
responseBody = response.thenApply(HttpResponse::body).get();
} catch (InterruptedException | ExecutionException exp) {
throw new APIResponseException(exp.getMessage(), exp);
}
// String resultBody = ""; return new Result(responseCode, responseBody);
// 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) { Response<Waifu> getWaifu(HttpClient httpClient, String param) throws APIResponseException {
sendRequest(httpClient, "waifu/" + param); Result waifuResult = sendRequest(httpClient, "waifu/" + param);
Integer statusCode = waifuResult.getStatusCode();
String body = waifuResult.getBody();
Waifu waifu = null;
if (statusCode == 200) {
try {
JsonNode parent = objectMapper.readTree(body);
String waifuData = parent.get("data").toString();
waifu = objectMapper.readValue(waifuData, Waifu.class);
} catch (JsonProcessingException jpe) {
jpe.printStackTrace();
statusCode = 100;
body = "{\"message\":\"If you are seeing this message, this is more than likely a fault in my logic. " +
"Please raise an issue with the printed stacktrace :D\",\"code\":Custom 100}";
}
}
return new Response<>(statusCode, body, waifu);
} }
} }

@ -0,0 +1,30 @@
package org.goudham.me;
public class Response<T> {
private T entity;
private final Integer responseCode;
private final String responseBody;
Response(Integer responseCode, String responseBody, T entity) {
this.responseCode = responseCode;
this.responseBody = responseBody;
this.entity = entity;
}
Response(Integer responseCode, String responseBody) {
this.responseCode = responseCode;
this.responseBody = responseBody;
}
public T getEntity() {
return entity;
}
public Integer getResponseCode() {
return responseCode;
}
public String getResponseBody() {
return responseBody;
}
}

@ -0,0 +1,19 @@
package org.goudham.me;
class Result {
private final Integer statusCode;
private final String body;
Result(Integer statusCode, String body) {
this.statusCode = statusCode;
this.body = body;
}
Integer getStatusCode() {
return statusCode;
}
String getBody() {
return body;
}
}

@ -2,10 +2,7 @@ package org.goudham.me.api.entity;
import javax.annotation.processing.Generated; import javax.annotation.processing.Generated;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.Objects; import java.util.Objects;
@ -29,6 +26,12 @@ import java.util.Objects;
}) })
@Generated("jsonschema2pojo") @Generated("jsonschema2pojo")
public class Studio { public class Studio {
@JsonCreator
Studio(String name) {
this.name = name;
}
/** /**
* {@link Studio} ID * {@link Studio} ID
*/ */

@ -138,8 +138,7 @@ public class Series {
/** /**
* {@link Studio} * {@link Studio}
* <p> * <p> Contains information on a given animation or game development studio </p>
* Contains information on a given animation or game development studio
* *
*/ */
@JsonProperty("studio") @JsonProperty("studio")

@ -0,0 +1,14 @@
package org.goudham.me.exception;
import org.goudham.me.MyWaifuWrapper;
/**
* Thrown when {@link MyWaifuWrapper} fails to return API information
*
*/
public class APIResponseException extends Throwable {
public APIResponseException(String errorMessage, Throwable error) {
super(errorMessage, error);
}
}
Loading…
Cancel
Save