Skip to content

Commit

Permalink
Created the RandomImageAction for AGHPB#retrieveRandomBook and fixed …
Browse files Browse the repository at this point in the history
…AGHPB#retrieveBook
  • Loading branch information
JoshiCodes committed Aug 13, 2024
1 parent dbc06ed commit 6a72fd6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 36 deletions.
43 changes: 10 additions & 33 deletions src/main/java/de/joshicodes/aghpb4j/AGHPB.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import de.joshicodes.aghpb4j.action.ImageAction;
import de.joshicodes.aghpb4j.action.RandomImageAction;
import de.joshicodes.aghpb4j.action.RestAction;
import de.joshicodes.aghpb4j.objects.AGHPBook;

Expand Down Expand Up @@ -41,8 +42,8 @@ public AGHPB(final String url) {
* @see #retrieveRandomBook(AGHPBook.BookImageType)
* @see #retrieveRandomBook(String, AGHPBook.BookImageType)
*/
public ImageAction<AGHPBook> retrieveRandomBook() {
return retrieveRandomBook(null, AGHPBook.BookImageType.PNG);
public RandomImageAction retrieveRandomBook() {
return new RandomImageAction(url);
}

/**
Expand All @@ -55,8 +56,8 @@ public ImageAction<AGHPBook> retrieveRandomBook() {
* @see #retrieveRandomBook(AGHPBook.BookImageType)
* @see #retrieveRandomBook(String, AGHPBook.BookImageType)
*/
public ImageAction<AGHPBook> retrieveRandomBook(String category) {
return retrieveRandomBook(category, AGHPBook.BookImageType.PNG);
public RandomImageAction retrieveRandomBook(String category) {
return retrieveRandomBook().withCategory(category);
}

/**
Expand All @@ -69,8 +70,8 @@ public ImageAction<AGHPBook> retrieveRandomBook(String category) {
* @see #retrieveRandomBook(String)
* @see #retrieveRandomBook(String, AGHPBook.BookImageType)
*/
public ImageAction<AGHPBook> retrieveRandomBook(AGHPBook.BookImageType type) {
return retrieveRandomBook(null, type);
public RandomImageAction retrieveRandomBook(AGHPBook.BookImageType type) {
return retrieveRandomBook().useType(type);
}

/**
Expand All @@ -83,32 +84,8 @@ public ImageAction<AGHPBook> retrieveRandomBook(AGHPBook.BookImageType type) {
* @see #retrieveRandomBook(String)
* @see #retrieveRandomBook(AGHPBook.BookImageType)
*/
public ImageAction<AGHPBook> retrieveRandomBook(String category, AGHPBook.BookImageType type) {
final String url = this.url + "/random" + (category == null ? "" : "?category=" + category);
return new ImageAction<>(url, AGHPBook.class, (request) -> {
request.header("Accept", "image/" + type.name().toLowerCase());
return request;
}, response -> {
final String contentType = response.httpResponse().headers().firstValue("Content-Type").orElse(null);
if(contentType == null || !contentType.startsWith("image/")) {
JsonElement json = response.getAsJsonElement();
throw new IllegalStateException("Response is not an image! " + (json != null ? ("( " + json + " )") : ""));
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
baos.write(response.httpResponse().body());
} catch (IOException e) {
throw new RuntimeException(e);
}
final String imageCategory = response.httpResponse().headers().firstValue("Book-Category").orElse(null);
final String bookName = response.httpResponse().headers().firstValue("Book-Name").orElse(null);
final String commitAuthor = response.httpResponse().headers().firstValue("Book-Commit-Author").orElse(null);
final String commitHash = response.httpResponse().headers().firstValue("Book-Commit-Hash").orElse(null);
final String commitUrl = response.httpResponse().headers().firstValue("Book-Commit-Url").orElse(null);
final int searchId = Integer.parseInt(response.httpResponse().headers().firstValue("Book-Search-Id").orElse("-1"));
// final Date dateAdded = new Date(response.httpResponse().headers().firstValue("Book-Date-Added").orElse(null));
return new AGHPBook(baos.toByteArray(), type, imageCategory, commitAuthor, commitHash, commitUrl, bookName, searchId);
});
public RandomImageAction retrieveRandomBook(String category, AGHPBook.BookImageType type) {
return retrieveRandomBook().withCategory(category).useType(type);
}

/**
Expand Down Expand Up @@ -266,7 +243,7 @@ public ImageAction<AGHPBook> retrieveBook(AGHPBook book, AGHPBook.BookImageType
* @return An ImageAction for the book.
*/
public ImageAction<AGHPBook> retrieveBook(int searchId, AGHPBook.BookImageType type) {
final String url = this.url + "/book?search_id=" + searchId;
final String url = this.url + "/get/id/" + searchId;
return new ImageAction<>(url, AGHPBook.class, (request) -> {
request.header("Accept", "image/" + type.name().toLowerCase());
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class ImageAction<T> extends RestAction<T> {

private final Function<RestResponse<byte[]>, T> responseHandler;
protected Function<RestResponse<byte[]>, T> responseHandler;

public ImageAction(String url, Class<T> tClass, Function<HttpRequest.Builder, HttpRequest.Builder> clientModifier, Function<RestResponse<byte[]>, T> responseHandler) {
super(url, "GET", tClass, clientModifier, null);
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/de/joshicodes/aghpb4j/action/RandomImageAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.joshicodes.aghpb4j.action;

import com.google.gson.JsonElement;
import de.joshicodes.aghpb4j.objects.AGHPBook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.http.HttpRequest;
import java.util.function.Function;

public class RandomImageAction extends ImageAction<AGHPBook> {

public static final String URL = "%s/random";

private final String initialUrl;
private AGHPBook.BookImageType type = AGHPBook.BookImageType.PNG;

public RandomImageAction(final String apiUrl) {
super(String.format(URL, apiUrl), AGHPBook.class, null, null);
initialUrl = apiUrl;
clientModifier = (request) -> {
request.header("Accept", "image/" + type.name().toLowerCase());
return request;
};
responseHandler = response -> {
final String contentType = response.httpResponse().headers().firstValue("Content-Type").orElse(null);
if(contentType == null || !contentType.startsWith("image/")) {
throw new IllegalStateException("Failed request to " + url + "! Response is not an image! " + "( " + response.rawBody() + " )." + " Content Type is " + contentType + ". Expected image/*.");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
baos.write(response.httpResponse().body());
} catch (IOException e) {
throw new RuntimeException(e);
}
final String imageCategory = response.httpResponse().headers().firstValue("Book-Category").orElse(null);
final String bookName = response.httpResponse().headers().firstValue("Book-Name").orElse(null);
final String commitAuthor = response.httpResponse().headers().firstValue("Book-Commit-Author").orElse(null);
final String commitHash = response.httpResponse().headers().firstValue("Book-Commit-Hash").orElse(null);
final String commitUrl = response.httpResponse().headers().firstValue("Book-Commit-Url").orElse(null);
final int searchId = Integer.parseInt(response.httpResponse().headers().firstValue("Book-Search-Id").orElse("-1"));
// final Date dateAdded = new Date(response.httpResponse().headers().firstValue("Book-Date-Added").orElse(null));
return new AGHPBook(baos.toByteArray(), type, imageCategory, commitAuthor, commitHash, commitUrl, bookName, searchId);
};
}

public RandomImageAction useType(final AGHPBook.BookImageType type) {
this.type = type;
return this;
}

public RandomImageAction withCategory(final String category) {
final String cat = category != null ? ("?category=" + category) : "";
url = String.format(URL, this.initialUrl) + cat;
System.out.println("URL: " + url);
return this;
}

}
12 changes: 10 additions & 2 deletions src/main/java/de/joshicodes/aghpb4j/action/RestAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

public class RestAction<T> {

protected final String url;
protected String url;
protected final String method;
protected final Class<T> tClass;

protected final Function<HttpRequest.Builder, HttpRequest.Builder> clientModifier;
protected Function<HttpRequest.Builder, HttpRequest.Builder> clientModifier;
protected final Function<RestResponse, T> responseHandler;

public RestAction(String url, String method, Class<T> tClass, Function<HttpRequest.Builder, HttpRequest.Builder> clientModifier, Function<RestResponse, T> responseHandler) {
Expand Down Expand Up @@ -114,7 +114,15 @@ public T execute() {

public record RestResponse<A>(HttpResponse<A> httpResponse, Class<A> aClass) {

public String rawBody() {
if(httpResponse.body() == null)
return null;
return String.valueOf(httpResponse.body());
}

public JsonElement getAsJsonElement() {
if(httpResponse.statusCode() != 200)
return null;
if(httpResponse.body() == null)
return null;
String body = String.valueOf(httpResponse.body());
Expand Down

0 comments on commit 6a72fd6

Please sign in to comment.