22
33import okhttp3 .MediaType ;
44import okhttp3 .Request ;
5+ import okhttp3 .Response ;
6+ import okhttp3 .ResponseBody ;
57
68import java .io .IOException ;
79import java .io .InputStream ;
@@ -101,33 +103,43 @@ public List<String> getValue() throws ExecutionException, InterruptedException {
101103 return this .value .get ();
102104 }
103105
104- public CompletableFuture <Void > delete () {
105- return isUploadedFile
106- ? value .thenCompose (urls -> Http .requestDelete (urls .get (0 )))
107- : CompletableFuture .completedFuture (null );
108- }
109-
110106 private static CompletableFuture <List <String >> upload (InputStream stream , String fileName , Config config ) {
111107 return CompletableFuture .supplyAsync (() -> {
112108 Request request = Http .getRequestBuilder ()
113- .url (Http .getUrlBuilder (config ).addPathSegment ("upload" )
114- .addQueryParameter ("filename" , fileName )
115- .build ())
116- .post (RequestBodyStream .create (MediaType .parse ("application/octet-stream" ), stream ))
117- .build ();
118- try {
119- String id = Http .getClient ().newCall (request ).execute ().body ().string ();
120- return Collections .singletonList (id );
109+ .url (Http .getUrlBuilder (config ).addPathSegment ("upload" )
110+ .addQueryParameter ("filename" , fileName )
111+ .build ())
112+ .post (RequestBodyStream .create (MediaType .parse ("application/octet-stream" ), stream ))
113+ .build ();
114+
115+ try (Response response = Http .getClient ().newCall (request ).execute ()) {
116+ ResponseBody body = response .body ();
117+ if (body != null ) {
118+ if (response .code () != 200 ) {
119+ throw new ConversionException (body .string (), response .code ());
120+ }
121+ String id = body .string ();
122+ return Collections .singletonList (id );
123+ } else {
124+ throw new ConversionException ("Response body is empty" , response .code ());
125+ }
121126 } catch (IOException e ) {
122127 throw new RuntimeException (e );
123128 }
124129 });
125130 }
126131
132+ @ SuppressWarnings ("unused" )
133+ public CompletableFuture <Void > delete () {
134+ return isUploadedFile
135+ ? value .thenCompose (urls -> Http .requestDelete (urls .get (0 )))
136+ : CompletableFuture .completedFuture (null );
137+ }
138+
127139 public static Param [] concat (Param [] a , Param [] b ) {
128140 Param [] result = new Param [a .length + b .length ];
129141 System .arraycopy (a , 0 , result , 0 , a .length );
130142 System .arraycopy (b , 0 , result , a .length , b .length );
131143 return result ;
132144 }
133- }
145+ }
0 commit comments