-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created the RandomImageAction for AGHPB#retrieveRandomBook and fixed …
…AGHPB#retrieveBook
- Loading branch information
1 parent
dbc06ed
commit 6a72fd6
Showing
4 changed files
with
80 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/de/joshicodes/aghpb4j/action/RandomImageAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters