|
|
|
@ -30,6 +30,7 @@ public class LiblibTextToImage {
|
|
|
|
|
private static final String TEXT_TO_IMG_URL = API_BASE_URL + "/api/generate/webui/text2img";
|
|
|
|
|
private static final String QUERY_PROGRESS_URL = API_BASE_URL + "/api/generate/progress/";
|
|
|
|
|
private static final String QUERY_RESULT_URL = API_BASE_URL + "/api/generate/result/";
|
|
|
|
|
private static final String QUERY_STATUS_URL = API_BASE_URL + "/api/generate/webui/status";
|
|
|
|
|
|
|
|
|
|
private static final int MAX_RETRIES = 30; // 最大重试次数
|
|
|
|
|
private static final int RETRY_INTERVAL = 3000; // 重试间隔(毫秒)
|
|
|
|
@ -254,11 +255,23 @@ public class LiblibTextToImage {
|
|
|
|
|
// 创建OkHttpClient
|
|
|
|
|
OkHttpClient client = createHttpClient();
|
|
|
|
|
|
|
|
|
|
// 获取API路径
|
|
|
|
|
String uri = "/api/generate/result/" + generateUuid;
|
|
|
|
|
|
|
|
|
|
// 生成签名信息
|
|
|
|
|
SignUtil.SignatureInfo signInfo = SignUtil.makeSign(uri, secretKey);
|
|
|
|
|
|
|
|
|
|
// 构建带签名的URL
|
|
|
|
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(QUERY_RESULT_URL + generateUuid).newBuilder()
|
|
|
|
|
.addQueryParameter("AccessKey", accessKey)
|
|
|
|
|
.addQueryParameter("Signature", signInfo.getSignature())
|
|
|
|
|
.addQueryParameter("Timestamp", String.valueOf(signInfo.getTimestamp()))
|
|
|
|
|
.addQueryParameter("SignatureNonce", signInfo.getSignatureNonce());
|
|
|
|
|
|
|
|
|
|
// 创建请求
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(QUERY_RESULT_URL + generateUuid)
|
|
|
|
|
.url(urlBuilder.build())
|
|
|
|
|
.method("GET", null)
|
|
|
|
|
.addHeader("Authorization", "Bearer " + apiKey)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// 执行请求
|
|
|
|
@ -285,6 +298,69 @@ public class LiblibTextToImage {
|
|
|
|
|
return responseJson.getJSONObject("data");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询生图状态和结果
|
|
|
|
|
* @param generateUuid 生成任务UUID
|
|
|
|
|
* @return 任务状态和结果信息
|
|
|
|
|
* @throws IOException 异常信息
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject queryGenerateStatus(String generateUuid) throws IOException {
|
|
|
|
|
// 创建OkHttpClient
|
|
|
|
|
OkHttpClient client = createHttpClient();
|
|
|
|
|
|
|
|
|
|
// 构建请求体
|
|
|
|
|
JSONObject requestBody = new JSONObject();
|
|
|
|
|
requestBody.put("generateUuid", generateUuid);
|
|
|
|
|
|
|
|
|
|
// 获取API路径
|
|
|
|
|
String uri = "/api/generate/webui/status";
|
|
|
|
|
|
|
|
|
|
// 生成签名信息
|
|
|
|
|
SignUtil.SignatureInfo signInfo = SignUtil.makeSign(uri, secretKey);
|
|
|
|
|
|
|
|
|
|
// 构建带签名的URL
|
|
|
|
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(QUERY_STATUS_URL).newBuilder()
|
|
|
|
|
.addQueryParameter("AccessKey", accessKey)
|
|
|
|
|
.addQueryParameter("Signature", signInfo.getSignature())
|
|
|
|
|
.addQueryParameter("Timestamp", String.valueOf(signInfo.getTimestamp()))
|
|
|
|
|
.addQueryParameter("SignatureNonce", signInfo.getSignatureNonce());
|
|
|
|
|
|
|
|
|
|
// 创建请求
|
|
|
|
|
MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
|
RequestBody body = RequestBody.create(mediaType, requestBody.toJSONString());
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(urlBuilder.build())
|
|
|
|
|
.method("POST", body)
|
|
|
|
|
.addHeader("Content-Type", "application/json")
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// 执行请求
|
|
|
|
|
log.info("查询生图状态: {}", requestBody.toJSONString());
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
|
|
|
|
|
// 处理响应
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
|
String errorMsg = "查询生图状态失败,状态码: " + response.code();
|
|
|
|
|
log.error(errorMsg);
|
|
|
|
|
throw new IOException(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析响应
|
|
|
|
|
String responseBody = response.body().string();
|
|
|
|
|
log.info("查询生图状态响应: {}", responseBody);
|
|
|
|
|
|
|
|
|
|
JSONObject responseJson = JSON.parseObject(responseBody);
|
|
|
|
|
int code = responseJson.getIntValue("code");
|
|
|
|
|
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
String errorMsg = "查询生图状态失败,错误码: " + code + ", 错误信息: " + responseJson.getString("msg");
|
|
|
|
|
log.error(errorMsg);
|
|
|
|
|
throw new IOException(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseJson.getJSONObject("data");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 下载生成的图片
|
|
|
|
|
* @param imageUrl 图片URL
|
|
|
|
|