Skip to content

Commit f88caf2

Browse files
authored
Add async behavior to OkHttpClient (#1629)
* Add AsyncOkHttpClient implementation * Make OkHttpClient implement both Client and AsyncClient Removes the need to share code in an abstract class. * Update mindmap * Update CHANGELOG.md * Remove jetbrains specific annotations
1 parent ae5dc2b commit f88caf2

File tree

6 files changed

+1101
-5
lines changed

6 files changed

+1101
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### Version 11.9
2+
3+
* `OkHttpClient` now implements `AsyncClient`
4+
15
### Version 10.9
26

37
* Configurable to disable streaming mode for Default client by verils (#1182)

okhttp/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2012-2021 The Feign Authors
4+
Copyright 2012-2022 The Feign Authors
55
66
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
77
in compliance with the License. You may obtain a copy of the License at
@@ -54,5 +54,11 @@
5454
<artifactId>mockwebserver</artifactId>
5555
<scope>test</scope>
5656
</dependency>
57+
58+
<dependency>
59+
<groupId>com.google.code.gson</groupId>
60+
<artifactId>gson</artifactId>
61+
<scope>test</scope>
62+
</dependency>
5763
</dependencies>
5864
</project>

okhttp/src/main/java/feign/okhttp/OkHttpClient.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import java.nio.charset.Charset;
2020
import java.util.Collection;
2121
import java.util.Map;
22+
import java.util.Optional;
23+
import java.util.concurrent.CompletableFuture;
2224
import java.util.concurrent.TimeUnit;
25+
import feign.AsyncClient;
2326
import feign.Client;
2427
import feign.Request.HttpMethod;
2528
import feign.Request.ProtocolVersion;
@@ -35,7 +38,7 @@
3538
* GitHub github = Feign.builder().client(new OkHttpClient()).target(GitHub.class,
3639
* "https://api.github.com");
3740
*/
38-
public final class OkHttpClient implements Client {
41+
public final class OkHttpClient implements Client, AsyncClient<Object> {
3942

4043
private final okhttp3.OkHttpClient delegate;
4144

@@ -153,9 +156,7 @@ public Reader asReader(Charset charset) throws IOException {
153156
};
154157
}
155158

156-
@Override
157-
public feign.Response execute(feign.Request input, feign.Request.Options options)
158-
throws IOException {
159+
private okhttp3.OkHttpClient getClient(feign.Request.Options options) {
159160
okhttp3.OkHttpClient requestScoped;
160161
if (delegate.connectTimeoutMillis() != options.connectTimeoutMillis()
161162
|| delegate.readTimeoutMillis() != options.readTimeoutMillis()
@@ -168,8 +169,39 @@ public feign.Response execute(feign.Request input, feign.Request.Options options
168169
} else {
169170
requestScoped = delegate;
170171
}
172+
return requestScoped;
173+
}
174+
175+
@Override
176+
public feign.Response execute(feign.Request input, feign.Request.Options options)
177+
throws IOException {
178+
okhttp3.OkHttpClient requestScoped = getClient(options);
171179
Request request = toOkHttpRequest(input);
172180
Response response = requestScoped.newCall(request).execute();
173181
return toFeignResponse(response, input).toBuilder().request(input).build();
174182
}
183+
184+
@Override
185+
public CompletableFuture<feign.Response> execute(feign.Request input,
186+
feign.Request.Options options,
187+
Optional<Object> requestContext) {
188+
okhttp3.OkHttpClient requestScoped = getClient(options);
189+
Request request = toOkHttpRequest(input);
190+
CompletableFuture<feign.Response> responseFuture = new CompletableFuture<>();
191+
requestScoped.newCall(request).enqueue(new Callback() {
192+
@Override
193+
public void onFailure(Call call, IOException e) {
194+
responseFuture.completeExceptionally(e);
195+
}
196+
197+
@Override
198+
public void onResponse(Call call, okhttp3.Response response)
199+
throws IOException {
200+
final feign.Response r =
201+
toFeignResponse(response, input).toBuilder().request(input).build();
202+
responseFuture.complete(r);
203+
}
204+
});
205+
return responseFuture;
206+
}
175207
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2012-2022 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package feign.okhttp;
15+
16+
public class CustomPojo {
17+
18+
private final String name;
19+
private final Integer number;
20+
21+
CustomPojo(String name, Integer number) {
22+
this.name = name;
23+
this.number = number;
24+
}
25+
}

0 commit comments

Comments
 (0)