|
10 | 10 | import java.io.OutputStream; |
11 | 11 | import java.util.concurrent.CompletableFuture; |
12 | 12 | import java.util.function.Consumer; |
| 13 | +import okhttp3.Call; |
| 14 | +import okhttp3.Callback; |
13 | 15 | import okhttp3.MediaType; |
14 | 16 | import okhttp3.OkHttpClient; |
15 | 17 | import okhttp3.RequestBody; |
| 18 | +import okhttp3.Response; |
16 | 19 | import okio.BufferedSink; |
17 | 20 | import org.jetbrains.annotations.NotNull; |
18 | 21 | import org.jetbrains.annotations.Nullable; |
@@ -46,20 +49,27 @@ public CompletableFuture<Response> send(Consumer<OutputStream> writer, int conte |
46 | 49 | RequestBody body = new RawRequestBody(writer, contentLength, MEDIA_TYPE); |
47 | 50 | builder.post(body); |
48 | 51 |
|
49 | | - try { |
50 | | - okhttp3.Response response = client.newCall(builder.build()).execute(); |
51 | | - if (response.isSuccessful()) { |
52 | | - if (response.body() != null) { |
53 | | - future.complete(new OkHttpResponse(response)); |
54 | | - } |
55 | | - } else { |
56 | | - future.completeExceptionally(new HttpErrorException(response.code(), response.message())); |
57 | | - } |
58 | | - } catch (IOException e) { |
59 | | - future.completeExceptionally(e); |
60 | | - } |
61 | | - |
62 | | - future.completeExceptionally(new IllegalStateException()); |
| 52 | + client |
| 53 | + .newCall(builder.build()) |
| 54 | + .enqueue( |
| 55 | + new Callback() { |
| 56 | + @Override |
| 57 | + public void onResponse(@NotNull Call call, @NotNull okhttp3.Response response) { |
| 58 | + if (response.isSuccessful()) { |
| 59 | + if (response.body() != null) { |
| 60 | + future.complete(new OkHttpResponse(response)); |
| 61 | + } |
| 62 | + } else { |
| 63 | + future.completeExceptionally( |
| 64 | + new HttpErrorException(response.code(), response.message())); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 70 | + future.completeExceptionally(e); |
| 71 | + } |
| 72 | + }); |
63 | 73 |
|
64 | 74 | return future; |
65 | 75 | } |
|
0 commit comments