33import me .Logicism .JavaHordeBridge .HordeBridge ;
44import me .Logicism .JavaHordeBridge .network .BrowserClient ;
55import me .Logicism .JavaHordeBridge .network .BrowserData ;
6- import org .json .JSONArray ;
76import org .json .JSONException ;
87import org .json .JSONObject ;
98
109import java .io .IOException ;
10+ import java .net .URISyntaxException ;
1111import java .net .URL ;
1212import java .util .HashMap ;
1313import java .util .Map ;
@@ -31,10 +31,10 @@ public JSONObject startCaptionGeneration(String imageURL) {
3131 headers .put ("User-Agent" , "Java 11 / Java Horde Bridge " + HordeBridge .BRIDGE_VERSION );
3232 while (retryCount < 5 ) {
3333 try {
34- BrowserData generationData = BrowserClient .executePOSTRequest (new URL (kaiURL + "/caption" ), new JSONObject ().put ("url" , imageURL ).toString (), headers );
34+ BrowserData generationData = BrowserClient .executePOSTRequestString (new URL (kaiURL + "/caption" ), new JSONObject ().put ("url" , imageURL ).toString (), headers );
3535
3636 if (generationData .getResponseCode () == 200 ) {
37- generationObject = new JSONObject (BrowserClient . requestToString ( generationData .getResponse () ));
37+ generationObject = new JSONObject (generationData .getResponseString ( ));
3838
3939 break ;
4040 } else if (generationData .getResponseCode () == 500 ) {
@@ -59,7 +59,7 @@ public JSONObject startCaptionGeneration(String imageURL) {
5959 TimeUnit .SECONDS .sleep (5 );
6060 } catch (InterruptedException ignored ) {
6161 }
62- } catch (IOException e ) {
62+ } catch (IOException | InterruptedException | URISyntaxException e ) {
6363 bridge .getLogger ().debug ("Client is unavailable (attempt " + retryCount ++ + "), retrying generation in 5 seconds..." );
6464
6565 try {
@@ -73,6 +73,7 @@ public JSONObject startCaptionGeneration(String imageURL) {
7373 generationObject = new JSONObject ().put ("error" , "The generation could not be completed due to an unknown error" );
7474 }
7575
76+ bridge .getLogger ().info ("Generated: " + generationObject );
7677 return generationObject ;
7778 }
7879
@@ -84,10 +85,10 @@ public JSONObject startSafetyCheckGeneration(String imageURL) {
8485 headers .put ("User-Agent" , "Java 11 / Java Horde Bridge " + HordeBridge .BRIDGE_VERSION );
8586 while (retryCount < 5 ) {
8687 try {
87- BrowserData generationData = BrowserClient .executePOSTRequest (new URL (kaiURL + "/safetycheck" ), new JSONObject ().put ("url" , imageURL ).toString (), headers );
88+ BrowserData generationData = BrowserClient .executePOSTRequestString (new URL (kaiURL + "/safetycheck" ), new JSONObject ().put ("url" , imageURL ).toString (), headers );
8889
8990 if (generationData .getResponseCode () == 200 ) {
90- generationObject = new JSONObject (BrowserClient . requestToString ( generationData .getResponse () ));
91+ generationObject = new JSONObject (generationData .getResponseString ( ));
9192
9293 break ;
9394 } else if (generationData .getResponseCode () == 500 ) {
@@ -112,7 +113,7 @@ public JSONObject startSafetyCheckGeneration(String imageURL) {
112113 TimeUnit .SECONDS .sleep (5 );
113114 } catch (InterruptedException ignored ) {
114115 }
115- } catch (IOException e ) {
116+ } catch (IOException | InterruptedException | URISyntaxException e ) {
116117 bridge .getLogger ().debug ("Client is unavailable (attempt " + retryCount ++ + "), retrying generation in 5 seconds..." );
117118
118119 try {
@@ -126,6 +127,7 @@ public JSONObject startSafetyCheckGeneration(String imageURL) {
126127 generationObject = new JSONObject ().put ("error" , "The generation could not be completed due to an unknown error" );
127128 }
128129
130+ bridge .getLogger ().info ("Generated: " + generationObject );
129131 return generationObject ;
130132 }
131133
@@ -136,11 +138,12 @@ public JSONObject startInterrogationGeneration(String imageURL) {
136138 headers .put ("Content-Type" , "application/json" );
137139 headers .put ("User-Agent" , "Java 11 / Java Horde Bridge " + HordeBridge .BRIDGE_VERSION );
138140 while (retryCount < 5 ) {
141+ bridge .getLogger ().info ("Image: " + imageURL );
139142 try {
140143 BrowserData generationData = BrowserClient .executePOSTRequest (new URL (kaiURL + "/interrogate" ), new JSONObject ().put ("url" , imageURL ).toString (), headers );
141144
142145 if (generationData .getResponseCode () == 200 ) {
143- generationObject = new JSONObject (BrowserClient . requestToString ( generationData .getResponse () ));
146+ generationObject = new JSONObject (generationData .getResponseString ( ));
144147
145148 break ;
146149 } else if (generationData .getResponseCode () == 500 ) {
@@ -165,7 +168,7 @@ public JSONObject startInterrogationGeneration(String imageURL) {
165168 TimeUnit .SECONDS .sleep (5 );
166169 } catch (InterruptedException ignored ) {
167170 }
168- } catch (IOException e ) {
171+ } catch (IOException | InterruptedException | URISyntaxException e ) {
169172 bridge .getLogger ().debug ("Client is unavailable (attempt " + retryCount ++ + "), retrying generation in 5 seconds..." );
170173
171174 try {
@@ -179,6 +182,7 @@ public JSONObject startInterrogationGeneration(String imageURL) {
179182 generationObject = new JSONObject ().put ("error" , "The generation could not be completed due to an unknown error" );
180183 }
181184
185+ bridge .getLogger ().info ("Generated: " + generationObject );
182186 return generationObject ;
183187 }
184188
@@ -193,7 +197,7 @@ public JSONObject startStripBackgroundGeneration(String imageURL) {
193197 BrowserData generationData = BrowserClient .executePOSTRequest (new URL (kaiURL + "/stripbackground" ), new JSONObject ().put ("url" , imageURL ).toString (), headers );
194198
195199 if (generationData .getResponseCode () == 200 ) {
196- generationObject = new JSONObject (BrowserClient . requestToString ( generationData .getResponse () ));
200+ generationObject = new JSONObject (generationData .getResponseString ( ));
197201
198202 break ;
199203 } else if (generationData .getResponseCode () == 500 ) {
@@ -218,7 +222,7 @@ public JSONObject startStripBackgroundGeneration(String imageURL) {
218222 TimeUnit .SECONDS .sleep (5 );
219223 } catch (InterruptedException ignored ) {
220224 }
221- } catch (IOException e ) {
225+ } catch (IOException | InterruptedException | URISyntaxException e ) {
222226 bridge .getLogger ().debug ("Client is unavailable (attempt " + retryCount ++ + "), retrying generation in 5 seconds..." );
223227
224228 try {
@@ -232,11 +236,12 @@ public JSONObject startStripBackgroundGeneration(String imageURL) {
232236 generationObject = new JSONObject ().put ("error" , "The generation could not be completed due to an unknown error" );
233237 }
234238
239+ bridge .getLogger ().info ("Generated: " + generationObject );
235240 return generationObject ;
236241 }
237242
238- public boolean validateClient () throws IOException {
239- BrowserData bd = BrowserClient .executeGETRequest (new URL (kaiURL + "/health" ), null );
243+ public boolean validateClient () throws IOException , URISyntaxException , InterruptedException {
244+ BrowserData bd = BrowserClient .executeGETRequestDiscard (new URL (kaiURL + "/health" ), new HashMap <>() );
240245
241246 return bd .getResponseCode () == 200 ;
242247 }
0 commit comments