Skip to content

Commit

Permalink
Moved resources and search to different packages
Browse files Browse the repository at this point in the history
  • Loading branch information
G1org1owo committed Mar 18, 2023
1 parent e7e2767 commit 542a37c
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 67 deletions.
67 changes: 34 additions & 33 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions src/jszuru/SzurubooruAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import com.google.gson.reflect.TypeToken;
import jszuru.exceptions.SzurubooruHTTPException;
import jszuru.exceptions.SzurubooruResourceNotSynchronizedException;
import jszuru.resources.FileToken;
import jszuru.resources.SzurubooruPost;
import jszuru.resources.SzurubooruTag;
import jszuru.search.SzurubooruSearch;
import jszuru.search.SzurubooruSearchResult;
import org.apache.http.*;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URLEncodedUtils;
Expand Down Expand Up @@ -198,10 +203,10 @@ protected String createApiUrl(List<String> parts, Map<String, String> query){
.toList(), "UTF-8");
}

protected Map<String, Object> call(String method, List<String> urlParts) throws IOException, SzurubooruHTTPException {
public Map<String, Object> call(String method, List<String> urlParts) throws IOException, SzurubooruHTTPException {
return call(method, urlParts, null, null);
}
protected Map<String, Object> call(String method,
public Map<String, Object> call(String method,
List<String> urlParts,
Map<String, String> urlQuery,
Map<String, Object> body) throws IOException, SzurubooruHTTPException {
Expand Down Expand Up @@ -251,10 +256,10 @@ public FileToken uploadFile(File file) throws IOException {
}
}

protected String createDataUrl(String relativeUrl) throws URISyntaxException, MalformedURLException {
public String createDataUrl(String relativeUrl) throws URISyntaxException, MalformedURLException {
return createDataUrl(relativeUrl, true);
}
protected String createDataUrl(String relativeUrl, boolean overrideBase) throws URISyntaxException, MalformedURLException {
public String createDataUrl(String relativeUrl, boolean overrideBase) throws URISyntaxException, MalformedURLException {
if(overrideBase){
String basePath = new File("/", urlPathPrefix).toString();
String relativePath = new URI(relativeUrl).getPath();
Expand Down Expand Up @@ -298,11 +303,11 @@ public SzurubooruPost createPost(FileToken content, String safety) throws IOExce
}

SzurubooruPost post = new SzurubooruPost(this, new HashMap<>());
post.newJson = Map.of(
post.setNewJson(Map.of(
"tags", new ArrayList<>(),
"safety", safety,
"contentToken", content.getToken()
);
));

post.push();
return post;
Expand All @@ -311,7 +316,7 @@ public SzurubooruPost[] searchPost(String searchQuery) throws IOException, Szuru
return searchPost(searchQuery, 20, false);
}
public SzurubooruPost[] searchPost(String searchQuery, int pageSize, boolean eagerLoad) throws IOException, SzurubooruHTTPException {
return (SzurubooruPost[])SzurubooruSearch.searchGeneric(this, searchQuery, new SzurubooruPost(this, new HashMap<>()), pageSize, eagerLoad);
return (SzurubooruPost[]) SzurubooruSearch.searchGeneric(this, searchQuery, new SzurubooruPost(this, new HashMap<>()), pageSize, eagerLoad);
}

public SzurubooruTag getTag(String id) throws IOException, SzurubooruHTTPException, SzurubooruResourceNotSynchronizedException {
Expand All @@ -332,7 +337,7 @@ public SzurubooruTag createTag(String name) throws IOException, SzurubooruHTTPEx
}

SzurubooruTag tag = new SzurubooruTag(this, new HashMap<>());
tag.newJson = Map.of("names", List.of(name), "category", defaultCategory);
tag.setNewJson(Map.of("names", List.of(name), "category", defaultCategory));

tag.push();
return tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package jszuru;
package jszuru.resources;

@SuppressWarnings("unused")
public class FileToken {
private final String token;
private final String filepath;

FileToken(String token, String filepath){
public FileToken(String token, String filepath){
this.token = token;
this.filepath = filepath;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jszuru;
package jszuru.resources;

import jszuru.SzurubooruAPI;
import jszuru.exceptions.SzurubooruException;
import jszuru.exceptions.SzurubooruHTTPException;
import jszuru.exceptions.SzurubooruResourceNotSynchronizedException;
Expand All @@ -17,7 +18,7 @@ public SzurubooruPost(SzurubooruAPI api, Map<String, Object> initialJson){
super(api, initialJson);
}

protected static boolean isValidSafety(String safety){
public static boolean isValidSafety(String safety){
return safety.equalsIgnoreCase("safe") ||
safety.equalsIgnoreCase("sketchy") ||
safety.equalsIgnoreCase("unsafe");
Expand All @@ -29,16 +30,16 @@ protected SzurubooruTag stringToTag(String value) throws IOException, Szurubooru
}

@Override
protected List<String> getInstanceUrlParts() {
public List<String> getInstanceUrlParts() {
return List.of("post", ((Double)json.get("id")).intValue() + "");
}
@Override
protected List<String> getClassUrlParts() {
public List<String> getClassUrlParts() {
return List.of("posts");
}

@Override
protected List<String> lazyLoadComponents() {
public List<String> lazyLoadComponents() {
return List.of("id", "safety", "type", "contentUrl", "flags", "tags", "relations");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jszuru;
package jszuru.resources;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jszuru;
package jszuru.resources;

import jszuru.SzurubooruAPI;
import jszuru.exceptions.SzurubooruHTTPException;
import jszuru.exceptions.SzurubooruResourceNotSynchronizedException;

Expand All @@ -22,10 +23,10 @@ public SzurubooruResource(SzurubooruAPI api, Map<String, Object> initialJson){
this.newJson = new HashMap<>();
}

protected abstract List<String> getInstanceUrlParts();
protected abstract List<String> getClassUrlParts();
public abstract List<String> getInstanceUrlParts();
public abstract List<String> getClassUrlParts();

protected abstract List<String> lazyLoadComponents();
public abstract List<String> lazyLoadComponents();
protected abstract Map<String, Function<Object, Object>> getterTransforms();
protected abstract Map<String, Function<Object, Object>> setterTransforms();

Expand Down Expand Up @@ -87,10 +88,6 @@ public boolean isSynchronized(){
return !newJson.isEmpty();
}

public SzurubooruAPI getApi(){
return api;
}

public static Object applyTransforms(Map<String, Function<Object, Object>> transforms, String propertyName, Object propertyValue){
if(propertyValue == null) return null;
if(propertyValue instanceof List<?> list){
Expand All @@ -106,6 +103,7 @@ public static Object applyTransforms(Map<String, Function<Object, Object>> trans
protected Object genericGetter(String propertyName) throws IOException, SzurubooruHTTPException, SzurubooruResourceNotSynchronizedException {
return genericGetter(propertyName, true);
}

protected Object genericGetter(String propertyName, boolean dynamicRefresh) throws IOException, SzurubooruHTTPException, SzurubooruResourceNotSynchronizedException {
if(newJson.containsKey(propertyName)){
return applyTransforms(this.getterTransforms(), propertyName, newJson.get(propertyName));
Expand All @@ -120,10 +118,10 @@ protected Object genericGetter(String propertyName, boolean dynamicRefresh) thro

throw new IllegalStateException(propertyName + " is not present in the JSON response");
}

protected void genericSetter(String propertyName, Object propertyValue) throws IOException, SzurubooruHTTPException, SzurubooruResourceNotSynchronizedException {
genericSetter(propertyName, propertyValue, true);
}

protected void genericSetter(String propertyName, Object propertyValue, boolean dynamicRefresh) throws IOException, SzurubooruHTTPException, SzurubooruResourceNotSynchronizedException {
if(json.containsKey(propertyName)){
if(json.get(propertyName) instanceof List && !(propertyValue instanceof Iterable<?>)){
Expand All @@ -142,10 +140,10 @@ protected void genericSetter(String propertyName, Object propertyValue, boolean

throw new IllegalStateException(propertyName + " is not present in the JSON response");
}

protected String fileGetter(String propertyName) throws IOException, SzurubooruHTTPException, SzurubooruResourceNotSynchronizedException, URISyntaxException {
return fileGetter(propertyName, true);
}

protected String fileGetter(String propertyName, boolean dynamicRefresh) throws IOException, SzurubooruHTTPException, SzurubooruResourceNotSynchronizedException, URISyntaxException {
if(json.containsKey(propertyName + "Url")){
return api.createDataUrl(json.get(propertyName + "Url").toString());
Expand All @@ -158,17 +156,40 @@ protected String fileGetter(String propertyName, boolean dynamicRefresh) throws

throw new IllegalStateException(propertyName + " is not a URL resource in the JSON response");
}

protected void fileSetter(String propertyName, FileToken propertyValue){
fileSetter(propertyName,propertyValue, true);
}

protected void fileSetter(String propertyName, FileToken propertyValue, boolean dynamicRefresh){
newJson.put(propertyName + "Token", propertyValue.getToken());
}

protected static List<String> getPrimaryNames(List<Map<String, Object>> list){
return list.stream()
.map(y -> ((List<String>)y.get("names")).get(0))
.toList();
}

public SzurubooruAPI getApi(){
return api;
}
public SzurubooruResource setApi(SzurubooruAPI api) {
this.api = api;
return this;
}

public Map<String, Object> getJson() {
return json;
}
public SzurubooruResource setJson(Map<String, Object> json) {
this.json = json;
return this;
}

public Map<String, Object> getNewJson() {
return newJson;
}
public SzurubooruResource setNewJson(Map<String, Object> newJson) {
this.newJson = newJson;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jszuru;
package jszuru.resources;

import jszuru.SzurubooruAPI;
import jszuru.exceptions.SzurubooruException;
import jszuru.exceptions.SzurubooruHTTPException;
import jszuru.exceptions.SzurubooruResourceNotSynchronizedException;
Expand All @@ -11,7 +12,7 @@
@SuppressWarnings("unused")
public class SzurubooruTag extends SzurubooruResource{
@Override
protected List<String> getInstanceUrlParts() {
public List<String> getInstanceUrlParts() {
List<String> urlParts = new ArrayList<>();
urlParts.add("tag");

Expand All @@ -21,15 +22,15 @@ protected List<String> getInstanceUrlParts() {
return urlParts;
}
@Override
protected List<String> getClassUrlParts() {
public List<String> getClassUrlParts() {
ArrayList<String> urlParts = new ArrayList<>();
urlParts.add("tags");

return urlParts;
}

@Override
protected List<String> lazyLoadComponents() {
public List<String> lazyLoadComponents() {
ArrayList<String> components = new ArrayList<>();
components.add("names");
components.add("category");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package jszuru;
package jszuru.search;

import jszuru.SzurubooruAPI;
import jszuru.exceptions.SzurubooruHTTPException;
import jszuru.resources.SzurubooruResource;

import java.io.IOException;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package jszuru;
package jszuru.search;

import jszuru.resources.SzurubooruPost;

@SuppressWarnings("unused")
public class SzurubooruSearchResult {
Expand Down

0 comments on commit 542a37c

Please sign in to comment.