Browse Source

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
pull/1632/head
Joël Marty 2 years ago committed by GitHub
parent
commit
fdc7bc2fb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      CHANGELOG.md
  2. 8
      okhttp/pom.xml
  3. 40
      okhttp/src/main/java/feign/okhttp/OkHttpClient.java
  4. 25
      okhttp/src/test/java/feign/okhttp/CustomPojo.java
  5. 1028
      okhttp/src/test/java/feign/okhttp/OkHttpClientAsyncTest.java
  6. 1
      src/docs/overview-mindmap.iuml

4
CHANGELOG.md

@ -1,3 +1,7 @@ @@ -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)

8
okhttp/pom.xml

@ -1,7 +1,7 @@ @@ -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
@ -54,5 +54,11 @@ @@ -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
okhttp/src/main/java/feign/okhttp/OkHttpClient.java

@ -19,7 +19,10 @@ import java.io.Reader; @@ -19,7 +19,10 @@ import java.io.Reader;
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;
@ -35,7 +38,7 @@ import static feign.Util.enumForName; @@ -35,7 +38,7 @@ import static feign.Util.enumForName;
* 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;
@ -153,9 +156,7 @@ public final class OkHttpClient implements Client { @@ -153,9 +156,7 @@ public final class OkHttpClient implements Client {
};
}
@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()
@ -168,8 +169,39 @@ public final class OkHttpClient implements Client { @@ -168,8 +169,39 @@ public final class OkHttpClient implements Client {
} 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
okhttp/src/test/java/feign/okhttp/CustomPojo.java

@ -0,0 +1,25 @@ @@ -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;
}
}

1028
okhttp/src/test/java/feign/okhttp/OkHttpClientAsyncTest.java

File diff suppressed because it is too large Load Diff

1
src/docs/overview-mindmap.iuml

@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
** async clients
*** java.net.URL
*** Apache HC5
*** OkHttp
** contracts
*** Feign
*** JAX-RS

Loading…
Cancel
Save