Skip to content

Commit

Permalink
add SdWebuiOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Robothy committed Oct 13, 2023
1 parent b3f7094 commit 7058bb9
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/main/java/io/github/robothy/sdwebui/sdk/SdWebui.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.github.robothy.sdwebui.sdk;


import io.github.robothy.sdwebui.sdk.models.SdWebuiOptions;
import io.github.robothy.sdwebui.sdk.services.SdWebuiInvocationHandler;

import java.lang.reflect.Proxy;

public interface SdWebui extends SystemInfoFetcher, Txt2Image, Image2Image {

static SdWebui create(String endpoint) {
return (SdWebui) Proxy.newProxyInstance(SdWebui.class.getClassLoader(), new Class[]{SdWebui.class}, new SdWebuiInvocationHandler(endpoint));
SdWebuiOptions options = new SdWebuiOptions(endpoint);
return (SdWebui) Proxy.newProxyInstance(SdWebui.class.getClassLoader(), new Class[]{SdWebui.class}, new SdWebuiInvocationHandler(options));
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.github.robothy.sdwebui.sdk;

import io.github.robothy.sdwebui.sdk.models.SdWebuiOptions;
import io.github.robothy.sdwebui.sdk.services.DefaultSdWebuiBeanContainer;

public interface SdWebuiBeanContainer {

static SdWebuiBeanContainer create(String endpoint) {
return new DefaultSdWebuiBeanContainer(endpoint);
static SdWebuiBeanContainer create(SdWebuiOptions options) {
return new DefaultSdWebuiBeanContainer(options);
}

<T> T getBean(Class<T> serviceClass);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.robothy.sdwebui.sdk.models;

import lombok.Getter;

@Getter
public class SdWebuiOptions {

private final String endpoint;

public SdWebuiOptions(String endpoint) {
this.endpoint = endpoint;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@ public class SystemInfo {
@JsonProperty("Version")
private String sdwebuiVersion;

private String endpoint;

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void fetchSystemInfoIfAbsent() {
synchronized (this) {
if (cachedSystemInfo == null) {
cachedSystemInfo = fetchSystemInfo();
cachedSystemInfo.setEndpoint(endpoint);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.robothy.sdwebui.sdk.services;

import io.github.robothy.sdwebui.sdk.SdWebuiBeanContainer;
import io.github.robothy.sdwebui.sdk.models.SdWebuiOptions;
import io.github.robothy.sdwebui.sdk.utils.SdWebuiResponseUtils;
import java.io.IOException;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet;

final class CommonGetService {

private final SdWebuiBeanContainer container;

public CommonGetService(SdWebuiBeanContainer container) {
this.container = container;
}

public <T> T getData(String path, Class<T> clazz) {
HttpClient client = this.container.getBean(HttpClient.class);
SdWebuiOptions sdWebuiOptions = this.container.getBean(SdWebuiOptions.class);
try {
return client.execute(new HttpGet(sdWebuiOptions.getEndpoint() + path),
response -> SdWebuiResponseUtils.parseResponse(response, clazz));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.robothy.sdwebui.sdk.Image2Image;
import io.github.robothy.sdwebui.sdk.SdWebuiBeanContainer;
import io.github.robothy.sdwebui.sdk.models.SdWebuiOptions;
import io.github.robothy.sdwebui.sdk.models.SystemInfo;
import io.github.robothy.sdwebui.sdk.models.options.Image2ImageOptions;
import io.github.robothy.sdwebui.sdk.models.results.Image2ImageResult;
Expand Down Expand Up @@ -37,7 +38,7 @@ public Image2ImageResult img2img(Image2ImageOptions options) {
}

ClassicHttpRequest buildRequest(Image2ImageOptions options) {
String url = container.getBean(SystemInfo.class).getEndpoint() + IMG2IMG_PATH;
String url = container.getBean(SdWebuiOptions.class).getEndpoint() + IMG2IMG_PATH;
ClassicHttpRequest request = new HttpPost(url);
request.setEntity(new StringEntity(JsonUtils.toJson(options)));
request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.robothy.sdwebui.sdk.*;
import io.github.robothy.sdwebui.sdk.models.SdWebuiOptions;
import io.github.robothy.sdwebui.sdk.models.SystemInfo;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
Expand All @@ -14,11 +15,11 @@ public class DefaultSdWebuiBeanContainer implements SdWebuiBeanContainer {

private final Map<Class<?>, Object> services;

private final String endpoint;
private final SdWebuiOptions sdWebuiOptions;

public DefaultSdWebuiBeanContainer(String endpoint) {
public DefaultSdWebuiBeanContainer(SdWebuiOptions options) {
this.services = new HashMap<>();
this.endpoint = endpoint;
this.sdWebuiOptions = options;
init();
}

Expand All @@ -44,9 +45,10 @@ public void register(Class<?> serviceClass, Object service) {

private void init() {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
this.services.put(SdWebuiOptions.class, sdWebuiOptions);
this.services.put(ObjectMapper.class, new ObjectMapper());
this.services.put(HttpClient.class, closeableHttpClient);
this.services.put(SystemInfo.class, new CacheableSystemInfoFetcher(endpoint, this));
this.services.put(SystemInfo.class, new CacheableSystemInfoFetcher(sdWebuiOptions.getEndpoint(), this));
this.services.put(Txt2Image.class, new DefaultTxt2ImageService(this));
this.services.put(Image2Image.class, new DefaultImage2ImageService(this));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.robothy.sdwebui.sdk.SdWebuiBeanContainer;
import io.github.robothy.sdwebui.sdk.Txt2Image;
import io.github.robothy.sdwebui.sdk.models.SdWebuiOptions;
import io.github.robothy.sdwebui.sdk.models.SystemInfo;
import io.github.robothy.sdwebui.sdk.models.options.Txt2ImageOptions;
import io.github.robothy.sdwebui.sdk.models.results.Txt2ImgResult;
Expand Down Expand Up @@ -39,8 +40,8 @@ public Txt2ImgResult txt2Img(Txt2ImageOptions options) {
}

ClassicHttpRequest buildTxt2ImageRequest(Txt2ImageOptions options) {
SystemInfo systemInfo = this.beanContainer.getBean(SystemInfo.class);
HttpPost httpPost = new HttpPost(systemInfo.getEndpoint() + TXT2IMG_PATH);
SdWebuiOptions sdWebuiOptions = this.beanContainer.getBean(SdWebuiOptions.class);
HttpPost httpPost = new HttpPost(sdWebuiOptions.getEndpoint() + TXT2IMG_PATH);
HttpEntity entity = buildTxt2ImageRequestEntity(options);
httpPost.setEntity(entity);
httpPost.addHeader("Content-Type", "application/json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import io.github.robothy.sdwebui.sdk.SdWebuiBeanContainer;

import io.github.robothy.sdwebui.sdk.models.SdWebuiOptions;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public final class SdWebuiInvocationHandler implements InvocationHandler {

private final SdWebuiBeanContainer serviceContainer;

public SdWebuiInvocationHandler(String endpoint) {
this.serviceContainer = SdWebuiBeanContainer.create(endpoint);
public SdWebuiInvocationHandler(SdWebuiOptions options) {
this.serviceContainer = SdWebuiBeanContainer.create(options);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ public static void checkResponseStatus(ClassicHttpResponse response) {

}

public static <T> T parseResponse(ClassicHttpResponse response, Class<T> clazz) {
checkResponseStatus(response);
try {
return JsonUtils.fromJson(response.getEntity().getContent(), clazz);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class SystemInfoTest {
@Test
void testSerialization() throws JsonProcessingException {
SystemInfo systemInfo = new SystemInfo();
systemInfo.setEndpoint("http://localhost:8080");
systemInfo.setPlatform("Linux");
systemInfo.setPythonVersion("3.8.5");
systemInfo.setSdwebuiVersion("0.0.1");
Expand Down

0 comments on commit 7058bb9

Please sign in to comment.