Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async behavior to OkHttpClient #1629

Merged
merged 5 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
41 changes: 37 additions & 4 deletions okhttp/src/main/java/feign/okhttp/OkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
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;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what is that, but please remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some annotations IntelliJ added when generating the interface implementation. I removed it :)

import static feign.Util.enumForName;

/**
Expand All @@ -35,7 +39,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 +157,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 +170,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(@NotNull Call call, @NotNull IOException e) {
responseFuture.completeExceptionally(e);
}

@Override
public void onResponse(@NotNull Call call, @NotNull 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