https://github.com/saasquatch/apache-client5-reactive
Thin wrapper around Apache HttpAsyncClient 5.x to expose Reactive Streams interfaces.
https://github.com/saasquatch/apache-client5-reactive
apache apache-httpclient apache-httpcomponents async flowable http http-client httpcomponents httpcomponents-client java reactive-streams reactivex rxjava
Last synced: 2 months ago
JSON representation
Thin wrapper around Apache HttpAsyncClient 5.x to expose Reactive Streams interfaces.
- Host: GitHub
- URL: https://github.com/saasquatch/apache-client5-reactive
- Owner: saasquatch
- License: apache-2.0
- Created: 2020-02-27T21:20:28.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-07-04T19:12:11.000Z (11 months ago)
- Last Synced: 2025-01-18T00:25:53.158Z (4 months ago)
- Topics: apache, apache-httpclient, apache-httpcomponents, async, flowable, http, http-client, httpcomponents, httpcomponents-client, java, reactive-streams, reactivex, rxjava
- Language: Java
- Homepage:
- Size: 146 KB
- Stars: 1
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# apache-client5-reactive
[](https://opensource.org/licenses/Apache-2.0)
[](https://github.com/saasquatch/saasquatch-java-sdk/actions/workflows/JavaCI.yml)
[](https://jitpack.io/#saasquatch/apache-client5-reactive)Thin wrapper around [Apache HttpComponents](https://hc.apache.org/) HttpAsyncClient 5.x to expose [Reactive Streams](https://www.reactive-streams.org) interfaces.
## Sample usage
```java
import static java.nio.charset.StandardCharsets.UTF_8;import com.saasquatch.client5reactive.HttpReactiveClient;
import com.saasquatch.client5reactive.HttpReactiveClients;
import io.reactivex.rxjava3.core.Single;
import java.nio.ByteBuffer;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;public class Example {
public static void main(String[] args) throws Exception {
// Create a CloseableHttpAsyncClient first
try (CloseableHttpAsyncClient asyncClient = HttpAsyncClients.createDefault()) {
/*
* HttpReactiveClient does not support lifecycle management, so the underlying
* CloseableHttpAsyncClient needs to be started and closed.
*/
asyncClient.start();
// HttpReactiveClient is just a thin wrapper
final HttpReactiveClient reactiveClient = HttpReactiveClients.create(asyncClient);
// Execute a simple in-memory request
Single.fromPublisher(
reactiveClient.execute(SimpleRequestBuilder.get("https://www.example.com").build()))
.doOnSuccess(response -> {
// Get the response status and body in memory
System.out.println(response.getCode());
System.out.println(response.getBodyText());
})
.blockingSubscribe();
System.out.println("----------");
// Execute a streaming request
// In this case, the request is a simple in-memory request without a request body
Single.fromPublisher(reactiveClient.streamingExecute(
SimpleRequestBuilder.get("https://www.example.com").build()))
.flatMapPublisher(message -> {
// Get the status before subscribing to the streaming body
System.out.println(message.getHead().getCode());
return message.getBody();
})
// Collect the streaming body chunks into a List
.toList()
.map(byteBuffers -> {
// Concatenate the body chunks and decode with UTF-8
final int totalLength = byteBuffers.stream().mapToInt(ByteBuffer::remaining).sum();
final ByteBuffer combined = ByteBuffer.allocate(totalLength);
byteBuffers.forEach(combined::put);
combined.flip();
return UTF_8.decode(combined).toString();
})
.doOnSuccess(System.out::println)
.blockingSubscribe();
}
}}
```The source code is in package [`com.saasquatch.client5reactive.examples`](https://github.com/saasquatch/apache-client5-reactive/tree/master/src/test/java/com/saasquatch/client5reactive/examples).
## Adding it to your project
### Add the repository
Maven
```xml
jitpack.io
https://jitpack.io
```
Gradle
```gradle
repositories {
maven { url 'https://jitpack.io' }
}
```### Add the dependency
Maven
```xml
com.github.saasquatch
apache-client5-reactive
0.0.7```
Gradle
```gradle
implementation 'com.github.saasquatch:apache-client5-reactive:0.0.7'
```## License
Unless explicitly stated otherwise all files in this repository are licensed under the Apache License 2.0.
License boilerplate:
```
Copyright 2023 ReferralSaaSquatch.com, Inc.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 athttp://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.
```