Skip to content

Commit

Permalink
Updated to Version v1.2
Browse files Browse the repository at this point in the history
Created AGHPB#retrieveSearch and AGHPB#retrieveBook
  • Loading branch information
JoshiCodes committed Jun 16, 2024
1 parent a1ae488 commit a24a2bd
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.joshicodes</groupId>
<artifactId>AGHPB4J</artifactId>
<version>1.1</version>
<version>1.2</version>

<distributionManagement>
<repository>
Expand Down
141 changes: 141 additions & 0 deletions src/main/java/de/joshicodes/aghpb4j/AGHPB.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,147 @@ public RestAction<List<String>> retrieveAllCategories() {
});
}

/**
* Retrieves a List of AGHPBooks from the AGHPB API. <br>
* <b>NOTE:</b> The AGHPBooks will not contain any image data. Use #retrieveBook(String) with the search_id.<br>
* @param query The query to search for.
* @return A RestAction for the search.
*/
public RestAction<List<AGHPBook>> retrieveSearch(String query) {
return retrieveSearch(query, null, -1);
}

/**
* Retrieves a List of AGHPBooks from the AGHPB API. <br>
* <b>NOTE:</b> The AGHPBooks will not contain any image data. Use #retrieveBook(String) with the search_id.<br>
* @param query The query to search for.
* @param limit The limit of books to retrieve. Must be greater than 0.
* @return A RestAction for the search.
*/
public RestAction<List<AGHPBook>> retrieveSearch(String query, int limit) {
return retrieveSearch(query, null, limit);
}

/**
* Retrieves a List of AGHPBooks from the AGHPB API. <br>
* <b>NOTE:</b> The AGHPBooks will not contain any image data. Use #retrieveBook(String) with the search_id.<br>
* @param query The query to search for.
* @param category The category to search in. Can be null.
* @return A RestAction for the search.
*/
public RestAction<List<AGHPBook>> retrieveSearch(String query, String category) {
return retrieveSearch(query, category, -1);
}

/**
* Retrieves a List of AGHPBooks from the AGHPB API. <br>
* <b>NOTE:</b> The AGHPBooks will not contain any image data. Use #retrieveBook(String) with the search_id.<br>
* @param query The query to search for.
* @param category The category to search in. Can be null.
* @param limit The limit of books to retrieve. Must be greater than 0.
* @return A RestAction for the search.
*/
public RestAction<List<AGHPBook>> retrieveSearch(String query, String category, int limit) {
final String url = this.url + "/search?query=" + query + (category != null ? "&category=" + category : "") + (limit > 0 ? "&limit=" + limit : "");
return new RestAction<>(url, "GET", null, (response) -> {
JsonElement json = response.getAsJsonElement();
if(json == null || !json.isJsonArray()) {
throw new IllegalStateException("Response is not a JSON array");
}
JsonArray array = json.getAsJsonArray();
List<AGHPBook> books = new ArrayList<>();
for(JsonElement element : array) {
if(!element.isJsonObject()) {
continue;
}
JsonObject obj = element.getAsJsonObject();
if(
!obj.has("search_id")
|| !obj.has("name")
|| !obj.has("category")
|| !obj.has("date_added")
|| !obj.has("commit_url")
|| !obj.has("commit_author")
|| !obj.has("commit_hash")
) {
continue;
}
final int searchId = Integer.parseInt(obj.get("search_id").getAsString());
final String name = obj.get("name").getAsString();
final String category1 = obj.get("category").getAsString();
final String dateAdded = obj.get("date_added").getAsString();
final String commitUrl = obj.get("commit_url").getAsString();
final String commitAuthor = obj.get("commit_author").getAsString();
final String commitHash = obj.get("commit_hash").getAsString();
books.add(new AGHPBook(null, null, category1, commitAuthor, commitHash, commitUrl, name, searchId));
}
return books;
});
}

/**
* Retrieves a book from the AGHPB API. <br>
* This Method is used, to get the image data of a book, which was retrieved by #retrieveSearch(String) and does not contain any image data. <br>
* @param book The book to retrieve.
* @return An ImageAction for the book.
*/
public ImageAction<AGHPBook> retrieveBook(AGHPBook book) {
return retrieveBook(book.searchId());
}

/**
* Retrieves a book from the AGHPB API. <br>
* The returned book will contain the image data in PNG. <br>
* @param searchId The search_id of the book to retrieve.
* @return An ImageAction for the book.
*/
public ImageAction<AGHPBook> retrieveBook(int searchId) {
return retrieveBook(searchId, AGHPBook.BookImageType.PNG);
}

/**
* Retrieves a book from the AGHPB API. <br>
* @param book The book to retrieve.
* @param type The format of the book.
* @return An ImageAction for the book.
*/
public ImageAction<AGHPBook> retrieveBook(AGHPBook book, AGHPBook.BookImageType type) {
return retrieveBook(book.searchId(), type);
}

/**
* Retrieves a book from the AGHPB API. <br>
* @param searchId The search_id of the book to retrieve.
* @param type The format of the book.
* @return An ImageAction for the book.
*/
public ImageAction<AGHPBook> retrieveBook(int searchId, AGHPBook.BookImageType type) {
final String url = this.url + "/book?search_id=" + searchId;
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 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);
});
}

/**
* Retrieves the info of the AGHPB API. <br>
* The Info contains the book count and the API version. <br>
Expand Down

0 comments on commit a24a2bd

Please sign in to comment.