Skip to content

Commit

Permalink
Add async behavior to OkHttpClient (#1629)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
joelmarty authored Jun 13, 2022
1 parent d27d385 commit fdc7bc2
Show file tree
Hide file tree
Showing 6 changed files with 1,101 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Version 11.9

* `OkHttpClient` now implements `AsyncClient`

### Version 10.9

* Configurable to disable streaming mode for Default client by verils (#1182)
Expand Down
8 changes: 7 additions & 1 deletion okhttp/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012-2021 The Feign Authors
Copyright 2012-2022 The Feign Authors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -54,5 +54,11 @@
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
40 changes: 36 additions & 4 deletions okhttp/src/main/java/feign/okhttp/OkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import feign.AsyncClient;
import feign.Client;
import feign.Request.HttpMethod;
import feign.Request.ProtocolVersion;
Expand All @@ -35,7 +38,7 @@
* GitHub github = Feign.builder().client(new OkHttpClient()).target(GitHub.class,
* "https://api.github.com");
*/
public final class OkHttpClient implements Client {
public final class OkHttpClient implements Client, AsyncClient<Object> {

private final okhttp3.OkHttpClient delegate;

Expand Down Expand Up @@ -153,9 +156,7 @@ public Reader asReader(Charset charset) throws IOException {
};
}

@Override
public feign.Response execute(feign.Request input, feign.Request.Options options)
throws IOException {
private okhttp3.OkHttpClient getClient(feign.Request.Options options) {
okhttp3.OkHttpClient requestScoped;
if (delegate.connectTimeoutMillis() != options.connectTimeoutMillis()
|| delegate.readTimeoutMillis() != options.readTimeoutMillis()
Expand All @@ -168,8 +169,39 @@ public feign.Response execute(feign.Request input, feign.Request.Options options
} else {
requestScoped = delegate;
}
return requestScoped;
}

@Override
public feign.Response execute(feign.Request input, feign.Request.Options options)
throws IOException {
okhttp3.OkHttpClient requestScoped = getClient(options);
Request request = toOkHttpRequest(input);
Response response = requestScoped.newCall(request).execute();
return toFeignResponse(response, input).toBuilder().request(input).build();
}

@Override
public CompletableFuture<feign.Response> execute(feign.Request input,
feign.Request.Options options,
Optional<Object> requestContext) {
okhttp3.OkHttpClient requestScoped = getClient(options);
Request request = toOkHttpRequest(input);
CompletableFuture<feign.Response> responseFuture = new CompletableFuture<>();
requestScoped.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
responseFuture.completeExceptionally(e);
}

@Override
public void onResponse(Call call, okhttp3.Response response)
throws IOException {
final feign.Response r =
toFeignResponse(response, input).toBuilder().request(input).build();
responseFuture.complete(r);
}
});
return responseFuture;
}
}
25 changes: 25 additions & 0 deletions okhttp/src/test/java/feign/okhttp/CustomPojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2012-2022 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign.okhttp;

public class CustomPojo {

private final String name;
private final Integer number;

CustomPojo(String name, Integer number) {
this.name = name;
this.number = number;
}
}
Loading

0 comments on commit fdc7bc2

Please sign in to comment.