Skip to content

Commit b79dfc8

Browse files
Merge pull request #5 from saasquatch/dev
Dev
2 parents d841c2b + 3e59781 commit b79dfc8

File tree

10 files changed

+97
-86
lines changed

10 files changed

+97
-86
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ hs_err_pid*
2727
.settings
2828
/target/
2929
bin
30+
31+
.idea
32+
*.iml

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ Thin wrapper around [Apache HttpComponents](https://hc.apache.org/) HttpAsyncCli
1010

1111
```java
1212
import static java.nio.charset.StandardCharsets.UTF_8;
13-
import java.nio.ByteBuffer;
14-
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
15-
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
16-
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
13+
1714
import com.saasquatch.client5reactive.HttpReactiveClient;
1815
import com.saasquatch.client5reactive.HttpReactiveClients;
1916
import io.reactivex.rxjava3.core.Single;
17+
import java.nio.ByteBuffer;
18+
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
19+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
20+
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
2021

2122
public class Example {
2223

@@ -31,8 +32,8 @@ public class Example {
3132
// HttpReactiveClient is just a thin wrapper
3233
final HttpReactiveClient reactiveClient = HttpReactiveClients.create(asyncClient);
3334
// Execute a simple in-memory request
34-
Single
35-
.fromPublisher(reactiveClient.execute(SimpleHttpRequests.get("https://www.example.com")))
35+
Single.fromPublisher(
36+
reactiveClient.execute(SimpleRequestBuilder.get("https://www.example.com").build()))
3637
.doOnSuccess(response -> {
3738
// Get the response status and body in memory
3839
System.out.println(response.getCode());
@@ -42,9 +43,8 @@ public class Example {
4243
System.out.println("----------");
4344
// Execute a streaming request
4445
// In this case, the request is a simple in-memory request without a request body
45-
Single
46-
.fromPublisher(
47-
reactiveClient.streamingExecute(SimpleHttpRequests.get("https://www.example.com")))
46+
Single.fromPublisher(reactiveClient.streamingExecute(
47+
SimpleRequestBuilder.get("https://www.example.com").build()))
4848
.flatMapPublisher(message -> {
4949
// Get the status before subscribing to the streaming body
5050
System.out.println(message.getHead().getCode());
@@ -101,14 +101,14 @@ Maven
101101
<dependency>
102102
<groupId>com.github.saasquatch</groupId>
103103
<artifactId>apache-client5-reactive</artifactId>
104-
<version>0.0.3</version>
104+
<version>0.0.4</version>
105105
</dependency>
106106
```
107107

108108
Gradle
109109

110110
```gradle
111-
compile 'com.github.saasquatch:apache-client5-reactive:0.0.3'
111+
implementation 'com.github.saasquatch:apache-client5-reactive:0.0.4'
112112
```
113113

114114
## License

pom.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.saasquatch</groupId>
55
<artifactId>apache-client5-reactive</artifactId>
6-
<version>0.0.3-SNAPSHOT</version>
6+
<version>0.0.4-SNAPSHOT</version>
77
<packaging>jar</packaging>
88

99
<name>apache-client5-reactive</name>
@@ -19,7 +19,7 @@
1919

2020
<properties>
2121
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22-
<junit.version>5.7.0</junit.version>
22+
<junit.version>5.7.2</junit.version>
2323
</properties>
2424

2525
<dependencies>
@@ -38,17 +38,17 @@
3838
<dependency>
3939
<groupId>org.apache.httpcomponents.client5</groupId>
4040
<artifactId>httpclient5</artifactId>
41-
<version>5.0.3</version>
41+
<version>5.1</version>
4242
</dependency>
4343
<dependency>
4444
<groupId>org.apache.httpcomponents.core5</groupId>
4545
<artifactId>httpcore5-reactive</artifactId>
46-
<version>5.0.2</version>
46+
<version>5.1.1</version>
4747
</dependency>
4848
<dependency>
4949
<groupId>io.reactivex.rxjava3</groupId>
5050
<artifactId>rxjava</artifactId>
51-
<version>3.0.7</version>
51+
<version>3.0.13</version>
5252
</dependency>
5353
<dependency>
5454
<groupId>com.google.code.findbugs</groupId>
@@ -77,7 +77,7 @@
7777
<plugin>
7878
<groupId>org.apache.maven.plugins</groupId>
7979
<artifactId>maven-source-plugin</artifactId>
80-
<version>3.2.0</version>
80+
<version>3.2.1</version>
8181
<executions>
8282
<execution>
8383
<id>attach-sources</id>
@@ -90,9 +90,10 @@
9090
<plugin>
9191
<groupId>org.apache.maven.plugins</groupId>
9292
<artifactId>maven-javadoc-plugin</artifactId>
93-
<version>3.1.1</version>
93+
<version>3.3.0</version>
9494
<configuration>
9595
<doclint>none</doclint>
96+
<source>8</source>
9697
</configuration>
9798
<executions>
9899
<execution>

src/main/java/com/saasquatch/client5reactive/HttpReactiveClient.java

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,39 @@
2323
/**
2424
* Thin wrapper around Apache {@link HttpAsyncClient} to expose
2525
* <a href="https://www.reactive-streams.org/">Reactive Streams</a> interfaces.<br>
26-
* The methods in this interface aim to mirror the ones in {@link HttpAsyncClient} and
27-
* {@link CloseableHttpAsyncClient}.
26+
* The methods in this interface aim to mirror the ones in {@link HttpAsyncClient} and {@link
27+
* CloseableHttpAsyncClient}.
2828
*
2929
* @author sli
3030
* @see HttpReactiveClients
3131
*/
3232
public interface HttpReactiveClient {
3333

3434
/**
35-
* Execute the given request. This method is equivalent to
36-
* {@link HttpAsyncClient#execute(AsyncRequestProducer, AsyncResponseConsumer, HandlerFactory, HttpContext , FutureCallback)}.
37-
* If the {@link Future} produced by the equivalent {@link HttpAsyncClient} method completes with
38-
* {@code null}, then the returning {@link Publisher} of this method will complete with no
39-
* element.
35+
* Execute the given request. This method is equivalent to {@link HttpAsyncClient#execute(AsyncRequestProducer,
36+
* AsyncResponseConsumer, HandlerFactory, HttpContext, FutureCallback)}. If the {@link Future}
37+
* produced by the equivalent {@link HttpAsyncClient} method completes with {@code null}, then the
38+
* returning {@link Publisher} of this method will complete with no element.
4039
*/
4140
<T> Publisher<T> execute(@Nonnull AsyncRequestProducer requestProducer,
4241
@Nonnull AsyncResponseConsumer<T> responseConsumer,
4342
@Nullable HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
4443
@Nullable HttpContext context);
4544

4645
/**
47-
* Convenience method for
48-
* {@link #execute(AsyncRequestProducer, AsyncResponseConsumer, HandlerFactory, HttpContext)},
49-
* equivalent to
50-
* {@link CloseableHttpAsyncClient#execute(AsyncRequestProducer, AsyncResponseConsumer, HttpContext, FutureCallback)}
46+
* Convenience method for {@link #execute(AsyncRequestProducer, AsyncResponseConsumer,
47+
* HandlerFactory, HttpContext)}, equivalent to {@link CloseableHttpAsyncClient#execute(AsyncRequestProducer,
48+
* AsyncResponseConsumer, HttpContext, FutureCallback)}
5149
*/
5250
default <T> Publisher<T> execute(@Nonnull AsyncRequestProducer requestProducer,
5351
@Nonnull AsyncResponseConsumer<T> responseConsumer, @Nullable HttpContext context) {
5452
return execute(requestProducer, responseConsumer, null, context);
5553
}
5654

5755
/**
58-
* Convenience method for
59-
* {@link #execute(AsyncRequestProducer, AsyncResponseConsumer, HandlerFactory, HttpContext)},
60-
* equivalent to
61-
* {@link CloseableHttpAsyncClient#execute(AsyncRequestProducer, AsyncResponseConsumer, FutureCallback)}.
56+
* Convenience method for {@link #execute(AsyncRequestProducer, AsyncResponseConsumer,
57+
* HandlerFactory, HttpContext)}, equivalent to {@link CloseableHttpAsyncClient#execute(AsyncRequestProducer,
58+
* AsyncResponseConsumer, FutureCallback)}.
6259
*/
6360
default <T> Publisher<T> execute(@Nonnull AsyncRequestProducer requestProducer,
6461
@Nonnull AsyncResponseConsumer<T> responseConsumer) {
@@ -67,63 +64,62 @@ default <T> Publisher<T> execute(@Nonnull AsyncRequestProducer requestProducer,
6764

6865
/**
6966
* Execute a simple in-memory request and get a simple in-memory response. This method is
70-
* equivalent to
71-
* {@link CloseableHttpAsyncClient#execute(SimpleHttpRequest, HttpContext, FutureCallback)}. The
72-
* returning {@link Publisher} completes with exactly 1 element.
67+
* equivalent to {@link CloseableHttpAsyncClient#execute(SimpleHttpRequest, HttpContext,
68+
* FutureCallback)}. The returning {@link Publisher} completes with exactly 1 element.
7369
*/
7470
default Publisher<SimpleHttpResponse> execute(@Nonnull SimpleHttpRequest request,
7571
@Nullable HttpContext context) {
7672
return execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), context);
7773
}
7874

7975
/**
80-
* Convenience method for {@link #execute(SimpleHttpRequest, HttpContext)}, equivalent to
81-
* {@link CloseableHttpAsyncClient#execute(SimpleHttpRequest, FutureCallback)}.
76+
* Convenience method for {@link #execute(SimpleHttpRequest, HttpContext)}, equivalent to {@link
77+
* CloseableHttpAsyncClient#execute(SimpleHttpRequest, FutureCallback)}.
8278
*/
8379
default Publisher<SimpleHttpResponse> execute(@Nonnull SimpleHttpRequest request) {
8480
return execute(request, null);
8581
}
8682

8783
/**
88-
* Execute the given request and get a streaming response body as a {@link Publisher} of
89-
* {@link ByteBuffer}s. The returning {@link Publisher} completes with exactly 1 element. The
90-
* {@link Publisher} within the returning {@link Publisher} may contain 0 to n elements.
84+
* Execute the given request and get a streaming response body as a {@link Publisher} of {@link
85+
* ByteBuffer}s. The returning {@link Publisher} completes with exactly 1 element. The {@link
86+
* Publisher} within the returning {@link Publisher} may contain 0 to n elements.
9187
*/
9288
Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute(
9389
@Nonnull AsyncRequestProducer requestProducer,
9490
@Nullable HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
9591
@Nullable HttpContext context);
9692

9793
/**
98-
* Convenience method for
99-
* {@link #streamingExecute(AsyncRequestProducer, HandlerFactory, HttpContext)}
94+
* Convenience method for {@link #streamingExecute(AsyncRequestProducer, HandlerFactory,
95+
* HttpContext)}
10096
*/
10197
default Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute(
10298
@Nonnull AsyncRequestProducer requestProducer, @Nullable HttpContext context) {
10399
return streamingExecute(requestProducer, null, context);
104100
}
105101

106102
/**
107-
* Convenience method for
108-
* {@link #streamingExecute(AsyncRequestProducer, HandlerFactory, HttpContext)}
103+
* Convenience method for {@link #streamingExecute(AsyncRequestProducer, HandlerFactory,
104+
* HttpContext)}
109105
*/
110106
default Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute(
111107
@Nonnull AsyncRequestProducer requestProducer) {
112108
return streamingExecute(requestProducer, null);
113109
}
114110

115111
/**
116-
* Execute a simple in-memory request and get a streaming response. Convenience method for
117-
* {@link #streamingExecute(AsyncRequestProducer, HandlerFactory, HttpContext)}
112+
* Execute a simple in-memory request and get a streaming response. Convenience method for {@link
113+
* #streamingExecute(AsyncRequestProducer, HandlerFactory, HttpContext)}
118114
*/
119115
default Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute(
120116
@Nonnull SimpleHttpRequest request, @Nullable HttpContext context) {
121117
return streamingExecute(SimpleRequestProducer.create(request), context);
122118
}
123119

124120
/**
125-
* Convenience method for
126-
* {@link #streamingExecute(AsyncRequestProducer, HandlerFactory, HttpContext)}
121+
* Convenience method for {@link #streamingExecute(AsyncRequestProducer, HandlerFactory,
122+
* HttpContext)}
127123
*/
128124
default Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute(
129125
@Nonnull SimpleHttpRequest request) {

src/main/java/com/saasquatch/client5reactive/HttpReactiveClientImpl.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.nio.ByteBuffer;
44
import java.util.Objects;
5+
import javax.annotation.Nullable;
56
import org.apache.hc.client5.http.async.HttpAsyncClient;
67
import org.apache.hc.core5.http.HttpResponse;
78
import org.apache.hc.core5.http.Message;
@@ -14,6 +15,8 @@
1415
import org.reactivestreams.Publisher;
1516
import io.reactivex.rxjava3.core.Maybe;
1617

18+
import javax.annotation.Nonnull;
19+
1720
/**
1821
* Concrete implementation of {@link HttpReactiveClient}.
1922
*
@@ -28,11 +31,13 @@ final class HttpReactiveClientImpl implements HttpReactiveClient {
2831
}
2932

3033
@Override
31-
public <T> Publisher<T> execute(AsyncRequestProducer requestProducer,
32-
AsyncResponseConsumer<T> responseConsumer,
33-
HandlerFactory<AsyncPushConsumer> pushHandlerFactory, HttpContext context) {
34+
public <T> Publisher<T> execute(@Nonnull AsyncRequestProducer requestProducer,
35+
@Nonnull AsyncResponseConsumer<T> responseConsumer,
36+
@Nullable HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
37+
@Nullable HttpContext context) {
3438
Objects.requireNonNull(requestProducer);
3539
Objects.requireNonNull(responseConsumer);
40+
//noinspection CodeBlock2Expr
3641
return Maybe.<T>create(emitter -> {
3742
httpAsyncClient.execute(requestProducer, responseConsumer, pushHandlerFactory, context,
3843
FutureCallbacks.maybeEmitter(emitter));
@@ -41,8 +46,9 @@ public <T> Publisher<T> execute(AsyncRequestProducer requestProducer,
4146

4247
@Override
4348
public Publisher<Message<HttpResponse, Publisher<ByteBuffer>>> streamingExecute(
44-
AsyncRequestProducer requestProducer, HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
45-
HttpContext context) {
49+
@Nonnull AsyncRequestProducer requestProducer,
50+
@Nullable HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
51+
@Nullable HttpContext context) {
4652
Objects.requireNonNull(requestProducer);
4753
/*
4854
* Semantically this should be a Single instead of a Maybe, but using Single here requires an

src/main/java/com/saasquatch/client5reactive/HttpReactiveClients.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ private HttpReactiveClients() {}
1919
/**
2020
* Create a {@link HttpReactiveClient} from a given {@link HttpAsyncClient}. Note that the created
2121
* {@link HttpReactiveClient} is simply a wrapper of the {@link HttpAsyncClient} and does not
22-
* support state management, so you'll need to manage the state of the given
23-
* {@link HttpAsyncClient} yourself by calling {@link CloseableHttpAsyncClient#start()},
24-
* {@link CloseableHttpAsyncClient#close()}, etc.
22+
* support state management, so you'll need to manage the state of the given {@link
23+
* HttpAsyncClient} yourself by calling {@link CloseableHttpAsyncClient#start()}, {@link
24+
* CloseableHttpAsyncClient#close()}, etc.
2525
*/
2626
@Nonnull
2727
public static HttpReactiveClient create(@Nonnull HttpAsyncClient httpAsyncClient) {

src/test/java/com/saasquatch/client5reactive/LifecycleTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.saasquatch.client5reactive;
22

3-
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
3+
import io.reactivex.rxjava3.core.Flowable;
4+
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
45
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
56
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
67
import org.junit.jupiter.api.Test;
7-
import io.reactivex.rxjava3.core.Flowable;
88

99
public class LifecycleTests {
1010

@@ -15,8 +15,8 @@ public void testNonStarted() throws Exception {
1515
try (CloseableHttpAsyncClient asyncClient = HttpAsyncClients.createDefault()) {
1616
// Not started
1717
final HttpReactiveClient reactiveClient = HttpReactiveClients.create(asyncClient);
18-
Flowable.fromPublisher(reactiveClient.execute(SimpleHttpRequests.get(EXAMPLE_URL))).test()
19-
.assertError(IllegalStateException.class);
18+
Flowable.fromPublisher(reactiveClient.execute(SimpleRequestBuilder.get(EXAMPLE_URL).build()))
19+
.test().assertError(IllegalStateException.class);
2020
}
2121
}
2222

@@ -28,8 +28,8 @@ public void testClosed() throws Exception {
2828
reactiveClient = HttpReactiveClients.create(asyncClient);
2929
}
3030
// Closed
31-
Flowable.fromPublisher(reactiveClient.execute(SimpleHttpRequests.get(EXAMPLE_URL))).test()
32-
.assertError(IllegalStateException.class);
31+
Flowable.fromPublisher(reactiveClient.execute(SimpleRequestBuilder.get(EXAMPLE_URL).build()))
32+
.test().assertError(IllegalStateException.class);
3333
}
3434

3535
}

src/test/java/com/saasquatch/client5reactive/NullabilityTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import io.reactivex.rxjava3.core.Flowable;
57
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
6-
import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
8+
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
79
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
810
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
911
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
@@ -13,7 +15,6 @@
1315
import org.junit.jupiter.api.BeforeAll;
1416
import org.junit.jupiter.api.Test;
1517
import org.reactivestreams.Publisher;
16-
import io.reactivex.rxjava3.core.Flowable;
1718

1819
public class NullabilityTests {
1920

@@ -32,6 +33,7 @@ public static void afterAll() throws Exception {
3233
asyncClient.close();
3334
}
3435

36+
@SuppressWarnings({"ConstantConditions", "RedundantCast"})
3537
@Test
3638
public void testNullability() {
3739
assertThrows(NullPointerException.class,
@@ -59,7 +61,7 @@ public void testNullability() {
5961
@Test
6062
public void testVoidPublisher() {
6163
final Publisher<Void> voidResultPublisher = reactiveClient.execute(
62-
SimpleRequestProducer.create(SimpleHttpRequests.get("https://example.com")),
64+
SimpleRequestProducer.create(SimpleRequestBuilder.get("https://example.com").build()),
6365
new ReactiveResponseConsumer());
6466
assertDoesNotThrow(() -> Flowable.fromPublisher(voidResultPublisher).blockingSubscribe());
6567
}

0 commit comments

Comments
 (0)